diff --git a/room_control.py b/room_control.py index 5c8d125..a94cd5c 100755 --- a/room_control.py +++ b/room_control.py @@ -91,17 +91,31 @@ class RoomConfig: assert all(isinstance(state.time, time) for state in self.states), 'Times have not all been resolved yet' self.states = sorted(self.states, key=lambda s: s.time, reverse=True) - def current_state(self, current_time: time) -> RoomState: + def current_state(self, now: time) -> RoomState: time_fmt = "%I:%M:%S %p" - print(current_time.strftime(time_fmt)) + print(now.strftime(time_fmt)) self.sort_states() for state in self.states: - if state.time <= current_time: + if state.time <= now: return state else: # self.log(f'Defaulting to first state') return self.states[0] + + def current_scene(self, now: time) -> Dict: + state = self.current_state(now) + return state.scene + + def current_off_duration(self, now: time) -> timedelta: + state = self.current_state(now) + if state.off_duration is None: + if self.off_duration is None: + raise ValueError(f'Need an off duration') + else: + return self.off_duration + else: + return state.off_duration class RoomController(Hass, Mqtt): """Class for linking an light with a motion sensor. @@ -254,7 +268,7 @@ class RoomController(Hass, Mqtt): # else: # raise ValueError('Sleep variable is undefined') - async def off_duration(self) -> timedelta: + async def off_duration(self, now: time = None) -> timedelta: """Determines the time that the motion sensor has to be clear before deactivating Priority: @@ -264,11 +278,12 @@ class RoomController(Hass, Mqtt): - Sleep - 0 """ - current_state = await self.current_state() - if isinstance(current_state.off_duration, timedelta): - return current_state.off_duration - else: + sleep_mode_active = await self.sleep_bool() + if sleep_mode_active: return timedelta() + else: + now = now or (await self.get_now()).time() + return self._room_config.current_off_duration(now) @utils.sync_wrapper async def activate(self, entity = None, attribute = None, old = None, new = None, kwargs = None):