diff --git a/apps/apps.yaml b/apps/apps.yaml index 57c45a9..07fe884 100644 --- a/apps/apps.yaml +++ b/apps/apps.yaml @@ -5,9 +5,10 @@ hello_world: living_room_tv: module: tv class: LivingRoomTV - media_entity: media_player.living_room_tv - hardware_entity: media_player.sony_kd_65x85k remote_entity: remote.broadlink_remote + playing_entity: media_player.sony_google_cast + # media_entity: media_player.living_room_tv + # hardware_entity: media_player.sony_kd_65x85k scene_detect: module: scene_detect diff --git a/apps/tv.py b/apps/tv.py index 92a9d59..39ddddd 100644 --- a/apps/tv.py +++ b/apps/tv.py @@ -7,28 +7,38 @@ from appdaemon.plugins.hass.hassapi import Hass @dataclass(init=False) class LivingRoomTV(Hass): - media_entity: str - hardware_entity: str - remote_entity: str + remote_entity: str = None + playing_entity: str = None + # media_entity: str = None + # hardware_entity: str = None def initialize(self): - self.listen_state(self.turn_on_soundbar, self.args['playing_entity'], new='playing') - - for entity in self.args['track_entities']: - self.listen_state(self.state_change, entity, attribute='all') - self.log(f'Tracking state changes: {entity}') + self.log(self.args) + for f in fields(self): + if f.name in self.args: + entity_id = self.args[f.name] + if not self.entity_exists(entity_id): + self.log(f'{entity_id} does not exist', level="WARNING") + else: + setattr(self, f.name, entity_id) + else: + self.log(f'{f.name} field is unset', level="WARNING") + self.log(repr(self)) + + self.listen_state(self.turn_on_soundbar, self.playing_entity, new='playing') + self.log(f'Waiting for {self.playing_entity} to go to playing') def turn_on_soundbar(self, entity=None, attribute=None, old=None, new=None, kwargs=None): - if self.get_state(self.args['remote_entity']) != 'on': + if self.get_state(self.remote_entity) != 'on': self.log('Turning on remote') - self.turn_on(self.args['remote_entity']) + self.turn_on(self.remote_entity) # if self.remote_entity.state != 'on': # self.log('Turning on remote') # self.remote_entity.turn_on() self.log('Turning on soundbar') self.call_service(service='remote/send_command', - entity_id=self.args['remote_entity'], + entity_id=self.remote_entity, device='BoseTV', command='TV') def state_change(self, entity=None, attribute=None, old=None, new=None, kwargs=None): @@ -43,31 +53,6 @@ class LivingRoomTV(Hass): except KeyError as e: self.log(f'Failed getting attribute {e} from {entity}') - # def recast_lovelace(self, entity=None, attribute=None, old=None, new=None, kwargs=None): - # self.cancel_callbacks() - # if new['state'] == 'playing': - # if 'lovelace' in (app_name := new['attributes']['app_name']).lower(): - # delay = timedelta(minutes=9, seconds=30) - # self.log(f'Recasting Lovelace in {delay}') - # self.run_in( - # callback=lambda kwargs: self.media_entity.turn_off(), - # delay=delay.total_seconds() - # ) - # self.run_in( - # callback=self.cast_lovelace, - # delay=delay.total_seconds() + 5 - # ) - # else: - # self.log(f'Playing {app_name}') - - # def cast_lovelace(self, kwargs=None): - # self.call_service( - # 'cast/show_lovelace_view', - # view_path='tv', - # dashboard_path='lovelace', - # entity_id=self.media_entity.entity_id, - # ) - def cancel_callbacks(self, entity=None, attribute=None, old=None, new=None, kwargs=None): if (callbacks := self.get_scheduler_entries().get(self.name)): for handle, info in callbacks.items():