fixed soundbar turning on

This commit is contained in:
John Lancaster
2024-03-03 10:50:09 -06:00
parent 8383ff5cf1
commit 5db2976ce6
2 changed files with 24 additions and 38 deletions

View File

@@ -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():