pulled out some variables

This commit is contained in:
John Lancaster
2023-05-07 17:05:50 -05:00
parent 206f943e1a
commit a9f37c5e03
2 changed files with 29 additions and 12 deletions

View File

@@ -5,3 +5,5 @@ hello_world:
living_room_tv: living_room_tv:
module: tv module: tv
class: LivingRoomTV class: LivingRoomTV
tv_entity: media_player.living_room_tv
remote_entity: remote.broadlink_remote

View File

@@ -1,28 +1,43 @@
import asyncio from dataclasses import dataclass, field, fields
from datetime import timedelta from datetime import timedelta
from appdaemon.entity import Entity
from appdaemon.plugins.hass.hassapi import Hass from appdaemon.plugins.hass.hassapi import Hass
@dataclass(init=False)
class LivingRoomTV(Hass): class LivingRoomTV(Hass):
tv_entity: str
remote_entity: str
def initialize(self): def initialize(self):
self.listen_state(self.turn_on_soundbar, 'media_player.living_room_tv', new='playing') self.log(f'Media Player Entity: {self.tv_entity.friendly_name} ({self.tv_entity.entity_id})')
self.listen_state(self.debug_state, 'media_player.living_room_tv') self.log(f'Remote Entity: {self.remote_entity.friendly_name} ({self.remote_entity.entity_id})')
self.listen_state(self.recast_lovelace, 'media_player.living_room_tv', attribute='all')
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.cancel_callbacks, 'media_player.sony_kd_65x85k', new='off')
# self.cancel_callbacks()
@property
def tv_entity(self) -> Entity:
return self.get_entity(self.args['tv_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): def debug_state(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
self.log(f'{old} -> {new}') 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.get_state('remote.broadlink_remote') != 'on': if self.remote_entity.state != 'on':
self.log('Turning on remote') self.log('Turning on remote')
self.turn_on('remote.broadlink_remote') 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='remote.broadlink_remote', entity_id=self.remote_entity.entity_id,
device='BoseTV', command='TV') device='BoseTV', command='TV')
def recast_lovelace(self, entity=None, attribute=None, old=None, new=None, kwargs=None): def recast_lovelace(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
@@ -30,10 +45,10 @@ class LivingRoomTV(Hass):
self.cancel_callbacks() self.cancel_callbacks()
if new['state'] == 'playing': if new['state'] == 'playing':
if 'lovelace' in (app_name := new['attributes']['app_name']).lower(): if 'lovelace' in (app_name := new['attributes']['app_name']).lower():
delay = timedelta(minutes=10) delay = timedelta(minutes=9, seconds=30)
self.log(f'Recasting Lovelace in {delay}') self.log(f'Recasting Lovelace in {delay}')
self.run_in( self.run_in(
callback=lambda kwargs: self.turn_off('media_player.living_room_tv'), callback=lambda kwargs: self.tv_entity.turn_off(),
delay=delay.total_seconds() delay=delay.total_seconds()
) )
self.run_in( self.run_in(
@@ -48,7 +63,7 @@ class LivingRoomTV(Hass):
'cast/show_lovelace_view', 'cast/show_lovelace_view',
view_path='tv', view_path='tv',
dashboard_path='lovelace', dashboard_path='lovelace',
entity_id='media_player.living_room_tv', entity_id=self.tv_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):