moved off_duration to roomconfig

This commit is contained in:
John Lancaster
2024-01-22 18:42:13 -06:00
parent 6ef45830fb
commit 826e866a52

View File

@@ -91,18 +91,32 @@ class RoomConfig:
assert all(isinstance(state.time, time) for state in self.states), 'Times have not all been resolved yet' 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) 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" time_fmt = "%I:%M:%S %p"
print(current_time.strftime(time_fmt)) print(now.strftime(time_fmt))
self.sort_states() self.sort_states()
for state in self.states: for state in self.states:
if state.time <= current_time: if state.time <= now:
return state return state
else: else:
# self.log(f'Defaulting to first state') # self.log(f'Defaulting to first state')
return self.states[0] 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 RoomController(Hass, Mqtt):
"""Class for linking an light with a motion sensor. """Class for linking an light with a motion sensor.
@@ -254,7 +268,7 @@ class RoomController(Hass, Mqtt):
# else: # else:
# raise ValueError('Sleep variable is undefined') # 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 """Determines the time that the motion sensor has to be clear before deactivating
Priority: Priority:
@@ -264,11 +278,12 @@ class RoomController(Hass, Mqtt):
- Sleep - 0 - Sleep - 0
""" """
current_state = await self.current_state() sleep_mode_active = await self.sleep_bool()
if isinstance(current_state.off_duration, timedelta): if sleep_mode_active:
return current_state.off_duration
else:
return timedelta() return timedelta()
else:
now = now or (await self.get_now()).time()
return self._room_config.current_off_duration(now)
@utils.sync_wrapper @utils.sync_wrapper
async def activate(self, entity = None, attribute = None, old = None, new = None, kwargs = None): async def activate(self, entity = None, attribute = None, old = None, new = None, kwargs = None):