updated TV for reset

This commit is contained in:
John Lancaster
2024-01-28 09:02:06 -06:00
parent 0b96430f7a
commit 2a66ca4521

View File

@@ -12,64 +12,61 @@ class LivingRoomTV(Hass):
remote_entity: str remote_entity: str
def initialize(self): def initialize(self):
self.log(f'Media Player Entity: {self.media_entity.friendly_name} ({self.media_entity.entity_id})') self.listen_state(self.turn_on_soundbar, self.args['playing_entity'], new='playing')
self.log(f'Remote Entity: {self.remote_entity.friendly_name} ({self.remote_entity.entity_id})')
for entity in self.args['track_entities']:
self.listen_state(self.turn_on_soundbar, self.media_entity.entity_id, new='playing') self.listen_state(self.state_change, entity, attribute='all')
self.listen_state(self.debug_state, self.media_entity.entity_id) self.log(f'Tracking state changes: {entity}')
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 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:
return self.get_entity(self.args['remote_entity'])
def debug_state(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
self.log(f'{old} -> {new}')
def turn_on_soundbar(self, entity=None, attribute=None, old=None, new=None, kwargs=None): def turn_on_soundbar(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
if self.remote_entity.state != 'on': if self.get_state(self.args['remote_entity']) != 'on':
self.log('Turning on remote') self.log('Turning on remote')
self.remote_entity.turn_on() self.turn_on(self.args['remote_entity'])
# if self.remote_entity.state != 'on':
# self.log('Turning on remote')
# self.remote_entity.turn_on()
self.log('Turning on soundbar') self.log('Turning on soundbar')
# self.remote_entity.call_service('remote/send_command', device='BoseTV', command='TV')
self.call_service(service='remote/send_command', self.call_service(service='remote/send_command',
entity_id=self.remote_entity.entity_id, entity_id=self.args['remote_entity'],
device='BoseTV', command='TV') device='BoseTV', command='TV')
def recast_lovelace(self, entity=None, attribute=None, old=None, new=None, kwargs=None): def state_change(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
self.cancel_callbacks() try:
if new['state'] == 'playing': if entity.endswith('2') or entity.endswith('3'):
if 'lovelace' in (app_name := new['attributes']['app_name']).lower(): name = new['attributes']['entity_id']
delay = timedelta(minutes=9, seconds=30) app_name = new['attributes']['app_name']
self.log(f'Recasting Lovelace in {delay}') state = new['state']
self.run_in( self.log(f'{name}: {app_name} {state}')
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: else:
self.log(f'Playing {app_name}') self.log(f'{entity}: {new}')
except KeyError as e:
self.log(f'Failed getting attribute {e} from {entity}')
def cast_lovelace(self, kwargs=None): # def recast_lovelace(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
self.call_service( # self.cancel_callbacks()
'cast/show_lovelace_view', # if new['state'] == 'playing':
view_path='tv', # if 'lovelace' in (app_name := new['attributes']['app_name']).lower():
dashboard_path='lovelace', # delay = timedelta(minutes=9, seconds=30)
entity_id=self.media_entity.entity_id, # 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): def cancel_callbacks(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
if (callbacks := self.get_scheduler_entries().get(self.name)): if (callbacks := self.get_scheduler_entries().get(self.name)):