From cf9f0f3244f7b4bb9dcc96e3f119ce4fca5773e6 Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Fri, 8 Dec 2023 08:22:57 -0600 Subject: [PATCH] moved toggle logic --- button.py | 9 ++++++++- motion.py | 2 +- room_control.py | 27 +++++++++++---------------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/button.py b/button.py index be915b4..78d705b 100644 --- a/button.py +++ b/button.py @@ -38,6 +38,13 @@ class Button(Mqtt): async def handle_action(self, action: str): if action == 'single': self.log(f' {action.upper()} '.center(50, '=')) - await self.app.toggle(kwargs={'cause': 'button single click'}) + state = await self.get_state(self.args['ref_entity']) + kwargs = { + 'kwargs': {'cause': f'button single click: toggle while {state}'} + } + if state == 'on': + self.app.deactivate(**kwargs) + else: + await self.app.activate(**kwargs) else: pass \ No newline at end of file diff --git a/motion.py b/motion.py index cf8fd63..1286425 100644 --- a/motion.py +++ b/motion.py @@ -101,7 +101,7 @@ class Motion(Hass): async def cancel_motion_callback(self): callbacks = await self.get_sensor_callbacks() - self.log(f'Found {len(callbacks)} callbacks for {self.sensor.entity_id}') + # self.log(f'Found {len(callbacks)} callbacks for {self.sensor.entity_id}') for handle, info in callbacks.items(): entity = info["entity"] kwargs = info['kwargs'] diff --git a/room_control.py b/room_control.py index d1fddc7..9cf8a41 100755 --- a/room_control.py +++ b/room_control.py @@ -62,15 +62,16 @@ class RoomController(Hass, Mqtt): # schedule the transitions for state in self.states: - dt = str(state['time'])[:8] - self.log(f'Scheduling transition at: {dt}') + t: time = state['time'] try: - await self.run_at(callback=self.activate_any_on, start=dt, cause='scheduled transition') + await self.run_at(callback=self.activate_any_on, start=t.strftime('%H:%M:%S'), cause='scheduled transition') except ValueError: # happens when the callback time is in the past pass except Exception as e: self.log(f'Failed with {type(e)}: {e}') + else: + self.log(f'Scheduled transition at: {t.strftime("%I:%M:%S %p")}') async def parse_states(self): async def gen(): @@ -98,7 +99,7 @@ class RoomController(Hass, Mqtt): yield state states = [s async for s in gen()] - states = sorted(states, key=lambda s: s['time'], reverse=True) + # states = sorted(states, key=lambda s: s['time'], reverse=True) return states async def current_state(self, time: time = None): @@ -114,10 +115,13 @@ class RoomController(Hass, Mqtt): time = time or (await self.get_now()).time() for state in self.states: if state['time'] <= time: - # self.log(f'Selected state from {state["time"].strftime("%I:%M:%S%p")}') - return state + res = state else: - return self.states[-1] + self.log(f'Defaulting to first state') + res = self.states[0] + + self.log(f'Selected state from {res["time"].strftime("%I:%M:%S %p")}') + return res async def current_scene(self, time: time = None): if (state := (await self.current_state(time=time))) is not None: @@ -236,12 +240,3 @@ class RoomController(Hass, Mqtt): for e in self.app_entities: self.turn_off(e) self.log(f'Turned off {e}') - - @utils.sync_wrapper - async def toggle(self, *args, **kwargs): - state = await self.get_state(self.args['ref_entity']) - kwargs['kwargs']['cause'] += f': toggle while {state}' - if state == 'on': - self.deactivate(*args, **kwargs) - else: - await self.activate(*args, **kwargs)