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

@@ -5,9 +5,10 @@ hello_world:
living_room_tv: living_room_tv:
module: tv module: tv
class: LivingRoomTV class: LivingRoomTV
media_entity: media_player.living_room_tv
hardware_entity: media_player.sony_kd_65x85k
remote_entity: remote.broadlink_remote 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: scene_detect:
module: scene_detect module: scene_detect

View File

@@ -7,28 +7,38 @@ from appdaemon.plugins.hass.hassapi import Hass
@dataclass(init=False) @dataclass(init=False)
class LivingRoomTV(Hass): class LivingRoomTV(Hass):
media_entity: str remote_entity: str = None
hardware_entity: str playing_entity: str = None
remote_entity: str # media_entity: str = None
# hardware_entity: str = None
def initialize(self): def initialize(self):
self.listen_state(self.turn_on_soundbar, self.args['playing_entity'], new='playing') self.log(self.args)
for f in fields(self):
for entity in self.args['track_entities']: if f.name in self.args:
self.listen_state(self.state_change, entity, attribute='all') entity_id = self.args[f.name]
self.log(f'Tracking state changes: {entity}') 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): 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.log('Turning on remote')
self.turn_on(self.args['remote_entity']) self.turn_on(self.remote_entity)
# if self.remote_entity.state != 'on': # if self.remote_entity.state != 'on':
# self.log('Turning on remote') # self.log('Turning on remote')
# self.remote_entity.turn_on() # self.remote_entity.turn_on()
self.log('Turning on soundbar') self.log('Turning on soundbar')
self.call_service(service='remote/send_command', self.call_service(service='remote/send_command',
entity_id=self.args['remote_entity'], entity_id=self.remote_entity,
device='BoseTV', command='TV') device='BoseTV', command='TV')
def state_change(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):
@@ -43,31 +53,6 @@ class LivingRoomTV(Hass):
except KeyError as e: except KeyError as e:
self.log(f'Failed getting attribute {e} from {entity}') 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): 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)):
for handle, info in callbacks.items(): for handle, info in callbacks.items():