From 0850828bda7b382ad98788051f96d91f40f674aa Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Sun, 7 May 2023 17:13:25 -0500 Subject: [PATCH] broke out the hardware entity --- apps/apps.yaml | 3 ++- apps/tv.py | 32 ++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/apps/apps.yaml b/apps/apps.yaml index 72ef817..dcd8f88 100644 --- a/apps/apps.yaml +++ b/apps/apps.yaml @@ -5,5 +5,6 @@ hello_world: living_room_tv: module: tv class: LivingRoomTV - tv_entity: media_player.living_room_tv + media_entity: media_player.living_room_tv + hardware_entity: media_player.sony_kd_65x85k remote_entity: remote.broadlink_remote diff --git a/apps/tv.py b/apps/tv.py index 5eb58c6..018e797 100644 --- a/apps/tv.py +++ b/apps/tv.py @@ -7,21 +7,26 @@ from appdaemon.plugins.hass.hassapi import Hass @dataclass(init=False) class LivingRoomTV(Hass): - tv_entity: str + media_entity: str + hardware_entity: str remote_entity: str def initialize(self): - self.log(f'Media Player Entity: {self.tv_entity.friendly_name} ({self.tv_entity.entity_id})') + self.log(f'Media Player Entity: {self.media_entity.friendly_name} ({self.media_entity.entity_id})') self.log(f'Remote Entity: {self.remote_entity.friendly_name} ({self.remote_entity.entity_id})') - self.listen_state(self.turn_on_soundbar, self.tv_entity.entity_id, new='playing') - self.listen_state(self.debug_state, self.tv_entity.entity_id) - self.listen_state(self.recast_lovelace, self.tv_entity.entity_id, attribute='all') - # self.listen_state(self.cancel_callbacks, 'media_player.sony_kd_65x85k', new='off') + self.listen_state(self.turn_on_soundbar, self.media_entity.entity_id, new='playing') + self.listen_state(self.debug_state, self.media_entity.entity_id) + self.listen_state(self.recast_lovelace, self.media_entity.entity_id, attribute='all') + self.listen_state(self.hardware_off_callback, self.hardware_entity.entity_id, new='off') @property - def tv_entity(self) -> Entity: - return self.get_entity(self.args['tv_entity']) + def media_entity(self) -> Entity: + return self.get_entity(self.args['media_entity']) + + @property + def hardware_entity(self) -> Entity: + return self.get_entity(self.args['hardware_entity']) @property def remote_entity(self) -> Entity: @@ -36,19 +41,19 @@ class LivingRoomTV(Hass): self.remote_entity.turn_on() self.log('Turning on soundbar') + # self.remote_entity.call_service('remote/send_command', device='BoseTV', command='TV') self.call_service(service='remote/send_command', entity_id=self.remote_entity.entity_id, device='BoseTV', command='TV') def recast_lovelace(self, entity=None, attribute=None, old=None, new=None, kwargs=None): - # self.log(f'{new}') 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.tv_entity.turn_off(), + callback=lambda kwargs: self.media_entity.turn_off(), delay=delay.total_seconds() ) self.run_in( @@ -63,12 +68,15 @@ class LivingRoomTV(Hass): 'cast/show_lovelace_view', view_path='tv', dashboard_path='lovelace', - entity_id=self.tv_entity.entity_id, + entity_id=self.media_entity.entity_id, ) def cancel_callbacks(self, entity=None, attribute=None, old=None, new=None, kwargs=None): - # self.log(f'Cancelling scheduled callbacks') if (callbacks := self.get_scheduler_entries().get(self.name)): for handle, info in callbacks.items(): self.cancel_timer(handle) self.log(f'Cancelled {handle}') + + def hardware_off_callback(self, entity=None, attribute=None, old=None, new=None, kwargs=None): + self.log('Hardware turned off') + self.media_entity.turn_off()