fixed async all_off/any_on

This commit is contained in:
John Lancaster
2023-12-04 14:17:22 -06:00
parent 145aeca667
commit 0d70649bb8

View File

@@ -1,7 +1,7 @@
import asyncio import asyncio
from copy import deepcopy from copy import deepcopy
from datetime import datetime, time, timedelta from datetime import datetime, time, timedelta
from typing import List from typing import Dict, List
import appdaemon.utils as utils import appdaemon.utils as utils
import astral import astral
@@ -22,7 +22,7 @@ class RoomController(Hass, Mqtt):
async def initialize(self): async def initialize(self):
self.app_entities = await self.gather_app_entities() self.app_entities = await self.gather_app_entities()
self.log(f'entities: {self.app_entities}') # self.log(f'entities: {self.app_entities}')
await self.refresh_state_times() await self.refresh_state_times()
await self.run_daily(callback=self.refresh_state_times, start='00:00:00') await self.run_daily(callback=self.refresh_state_times, start='00:00:00')
@@ -123,23 +123,30 @@ class RoomController(Hass, Mqtt):
if (state := (await self.current_state(time=time))) is not None: if (state := (await self.current_state(time=time))) is not None:
return state['scene'] return state['scene']
@property async def app_entity_states(self) -> Dict[str, str]:
def all_off(self) -> bool: states = {
entity: (await self.get_state(entity))
for entity in self.app_entities
}
return states
async def all_off(self) -> bool:
""""All off" is the logic opposite of "any on" """"All off" is the logic opposite of "any on"
Returns: Returns:
bool: Whether all the lights associated with the app are off bool: Whether all the lights associated with the app are off
""" """
return all(self.get_state(entity) != 'on' for entity in self.app_entities) states = await self.app_entity_states()
return all(state != 'on' for entity, state in states.items())
@property async def any_on(self) -> bool:
def any_on(self) -> bool:
""""Any on" is the logic opposite of "all off" """"Any on" is the logic opposite of "all off"
Returns: Returns:
bool: Whether any of the lights associated with the app are on bool: Whether any of the lights associated with the app are on
""" """
return any(self.get_state(entity) == 'on' for entity in self.app_entities) states = await self.app_entity_states()
return any(state == 'on' for entity, state in states.items())
async def sleep_bool(self) -> bool: async def sleep_bool(self) -> bool:
if (sleep_var := self.args.get('sleep')): if (sleep_var := self.args.get('sleep')):
@@ -207,19 +214,18 @@ class RoomController(Hass, Mqtt):
@utils.sync_wrapper @utils.sync_wrapper
async def activate_all_off(self, *args, **kwargs): async def activate_all_off(self, *args, **kwargs):
"""Activate if all of the entities are off """Activate if all of the entities are off. Args and kwargs are passed directly to self.activate()
""" """
if self.all_off: if (await self.all_off()):
# self.log(f'Activate all off args/kwargs: {kwargs}') self.activate(*args, **kwargs)
self.activate( *args, **kwargs)
else: else:
self.log(f'Skipped activating - everything is not off') self.log(f'Skipped activating - everything is not off')
@utils.sync_wrapper @utils.sync_wrapper
async def activate_any_on(self, *args, **kwargs): async def activate_any_on(self, *args, **kwargs):
"""Activate if any of the entities are on """Activate if any of the entities are on. Args and kwargs are passed directly to self.activate()
""" """
if self.any_on: if (await self.any_on()):
self.activate(*args, **kwargs) self.activate(*args, **kwargs)
else: else:
self.log(f'Skipped activating - everything is off') self.log(f'Skipped activating - everything is off')