initial TV control

This commit is contained in:
John Lancaster
2023-05-07 16:50:10 -05:00
parent 8880a409ff
commit 206f943e1a
2 changed files with 64 additions and 1 deletions

59
apps/tv.py Normal file
View File

@@ -0,0 +1,59 @@
import asyncio
from datetime import timedelta
from appdaemon.plugins.hass.hassapi import Hass
class LivingRoomTV(Hass):
def initialize(self):
self.listen_state(self.turn_on_soundbar, 'media_player.living_room_tv', new='playing')
self.listen_state(self.debug_state, 'media_player.living_room_tv')
self.listen_state(self.recast_lovelace, 'media_player.living_room_tv', attribute='all')
# self.listen_state(self.cancel_callbacks, 'media_player.sony_kd_65x85k', new='off')
# self.cancel_callbacks()
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):
if self.get_state('remote.broadlink_remote') != 'on':
self.log('Turning on remote')
self.turn_on('remote.broadlink_remote')
self.log('Turning on soundbar')
self.call_service(service='remote/send_command',
entity_id='remote.broadlink_remote',
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=10)
self.log(f'Recasting Lovelace in {delay}')
self.run_in(
callback=lambda kwargs: self.turn_off('media_player.living_room_tv'),
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='media_player.living_room_tv',
)
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}')