separated some functions

This commit is contained in:
John Lancaster
2025-12-12 08:20:24 -06:00
parent 25c529da60
commit 3f6688821a
2 changed files with 29 additions and 9 deletions

View File

@@ -56,6 +56,10 @@ class MoesPad(ADBase):
self.log = self.adapi.log
self.event_handle = self.adapi.listen_event(self.handle_moes_event, 'moes_pad')
self.bedroom_offs = ('bedroom_stick', 'bedroom_john', 'bedroom_sydney')
self.main_offs = ('bar', 'server_lamp', 'living_room_stick')
# self.set_many(self.main_offs, state='on')
def handle_moes_event(self, event_name: str, data: dict[str, Any], **kwargs: Any) -> None:
button = MoesButtons(data.get('button'))
action = MoesActions(data.get('action'))
@@ -64,6 +68,8 @@ class MoesPad(ADBase):
match action:
case MoesActions.SINGLE_PRESS:
self.handle_single_press(button)
case MoesActions.DOUBLE_PRESS:
self.handle_double_press(button)
def handle_single_press(self, button: MoesButtons) -> None:
match button:
@@ -74,7 +80,25 @@ class MoesPad(ADBase):
case MoesButtons.BOTTOM_LEFT:
self.adapi.call_service('light/toggle', entity_id='light.bedroom_stick')
case MoesButtons.BOTTOM_RIGHT:
for e in ('bedroom_stick', 'bedroom_john', 'bedroom_sydney'):
eid = f'light.{e}'
if self.adapi.get_state(eid) == 'on':
self.adapi.call_service('light/toggle', entity_id=eid)
any_on = any(self.adapi.get_state(f'light.{e}') == 'on' for e in self.bedroom_offs)
scene = {'entities': {f'light.{e}': {'state': 'off' if any_on else 'on'}} for e in self.bedroom_offs}
self.adapi.call_service('scene/apply', **scene)
def handle_double_press(self, button: MoesButtons) -> None:
match button:
case MoesButtons.BOTTOM_RIGHT:
all_lights = [*self.bedroom_offs, *self.main_offs]
any_on = self.check_many(all_lights, state='on')
new_state = 'off' if any_on else 'on'
self.log(f'New state from double press: {new_state}')
self.set_many(self.bedroom_offs, state=new_state)
self.set_many(self.main_offs, state=new_state)
def set_many(self, lights: set[str], state: str = 'off'):
entity_ids = [f'light.{e}' if not e.startswith('light') else e for e in lights]
scene = {'entities': {eid: {'state': state} for eid in entity_ids}}
return self.adapi.call_service('scene/apply', **scene)
def check_many(self, lights: set[str], state: str = 'off') -> bool:
entity_ids = [f'light.{e}' if not e.startswith('light') else e for e in lights]
return any(self.adapi.get_state(eid) == state for eid in entity_ids)