Skip to content

Commit

Permalink
refactor, add features
Browse files Browse the repository at this point in the history
split up some actions into multiple functions, rename functions for clarity, add TV settings, display modes, input modes, add macro processing.
  • Loading branch information
mefranklin6 authored Oct 20, 2024
1 parent ec79271 commit b265aa8
Showing 1 changed file with 116 additions and 32 deletions.
148 changes: 116 additions & 32 deletions rti.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
from media_player_helper import MediaPlayerHelper
from light_helper import LightHelper
import variables
from constants import (
ALARM_SWITCH,
AVR,
DOORS,
KITCHEN_MAIN_LIGHTS,
LIVINGROOM_FAN,
THERMOSTAT,
SHELF_STRIP,
LIVINGROOM_LAMP,
KITCHEN_MAIN_LIGHTS,
LIVINGROOM_TV,
ROKU,
SHELF_STRIP,
THERMOSTAT,
TV_BRIGHTNESS,
WINDOWS,
DOORS,
ALARM_SWITCH,
)
from light_helper import LightHelper
from media_player_helper import MediaPlayerHelper

# Home Assistant event type that our Node Red listener is subscribed to
# Home Assistant event type that our Node Red listener is subscribed to
RTI_EVENT_KEYWORD = "rti_sync"

# The TV input that the AVR output is connected to
TV_AVR_INPUT = "HDMI-2"

TV_ANTENNA_SOURCE_NAME = "TV"

# RTI command mapped to AVR input name (Denon AVR Dynamically names inputs based on CEC)
avr_input = {"bluray": "UBP-X800", "streamer": "Roku Ultra", "pc": "8K"}
AVR_INPUT = {"bluray": "UBP-X800", "streamer": "Roku Ultra", "pc": "8K"}

# RTI command device names mapped to HA entity_id's
# State change updates for these lights are handled in Node-RED
Expand All @@ -37,7 +45,7 @@ def fire_rti_event(payload, topic):
task.sleep(0.5)


def thermostat_info():
def _thermostat_info():
mode = state.get(THERMOSTAT)
data = state.getattr(THERMOSTAT)
current_temp = data["current_temperature"]
Expand All @@ -46,7 +54,7 @@ def thermostat_info():


def send_thermostat_states():
mode, current_temp, desired_temp = thermostat_info()
mode, current_temp, desired_temp = _thermostat_info()
fire_rti_event(payload=mode, topic="thermostat_mode")
fire_rti_event(payload=current_temp, topic="thermostat_current_temp")
fire_rti_event(payload=desired_temp, topic="thermostat_desired_temp")
Expand Down Expand Up @@ -93,14 +101,14 @@ def send_all_states():
send_security_states()


def handle_fan_call():
def handle_fan_toggle():
fan_state = state.get(LIVINGROOM_FAN)
if fan_state == "on":
fan.turn_off(entity_id=LIVINGROOM_FAN)
return

else:
indoor_temp = thermostat_info()[1]
indoor_temp = _thermostat_info()[1]
indoor_temp = int(indoor_temp)
if indoor_temp > 80:
speed = 100
Expand All @@ -120,44 +128,105 @@ def handle_toggle_command(device):
elif ha_entity.startswith("switch"):
switch.toggle(entity_id=rti_tracked_lights[device])

if device == "fan":
handle_fan_call()
elif device == "fan":
handle_fan_toggle()


def _update_tv_picture_mode(mode):
if mode == "pc":
setting_name = "picture_mode"
new_value = "Game"

else:
setting_name = "picture_mode"
new_value = "Calibrated Dark"

# the tv will execute the command even if the value is the same
# so this checks the assumed value before sending the command
if variables.tv_picture_mode == new_value:
return

vizio.update_setting(
entity_id=LIVINGROOM_TV,
setting_type="picture",
setting_name=setting_name,
new_value=new_value,
)
# pyvizio does not provide picture mode feedback, so we need to track assumed state
variables.tv_picture_mode = new_value


def update_tv_brightness(percentage):
# Sets number helper in GUI
state.set(TV_BRIGHTNESS, percentage)

# Setting the brightness object above for some reason,
# does not trigger @state_changed when called from pyscript,
# so make the actual settings call here
vizio.update_setting(
entity_id=LIVINGROOM_TV,
setting_type="picture",
setting_name="backlight",
new_value=percentage,
)


def avr_mute_toggle():
mute_state = state.getattr(AVR)["is_volume_muted"]
media_player.volume_mute(entity_id=AVR, is_volume_muted=not mute_state)


def tv_antenna_macro():
if state.getattr(LIVINGROOM_TV)["source"] != TV_ANTENNA_SOURCE_NAME:
media_player.select_source(
entity_id=LIVINGROOM_TV, source=TV_ANTENNA_SOURCE_NAME
)
media_player.media_pause(entity_id=ROKU)


def _handle_avr_source_change(command):
source_list = hass.states.get(AVR).attributes.get("source_list")
if AVR_INPUT[command] in source_list:
media_player.select_source(entity_id=AVR, source=AVR_INPUT[command])

else:
log.warning(f"Source not found: {AVR_INPUT[command]}")


def _handle_avr(command):
tv_input = state.getattr(LIVINGROOM_TV)["source"].upper()
if tv_input != TV_AVR_INPUT:
media_player.select_source(entity_id=LIVINGROOM_TV, source=TV_AVR_INPUT)

def handle_avr(command):
if command == "volup":
elif command == "volup":
media_player.volume_up(entity_id=AVR)
return

if command == "voldown":
elif command == "voldown":
media_player.volume_down(entity_id=AVR)
return

if command == "mute":
mute_state = state.getattr(AVR)["is_volume_muted"]
media_player.volume_mute(entity_id=AVR, is_volume_muted=not mute_state)
elif command == "mute":
avr_mute_toggle()
return

if command in ["on", "off"]:
elif command in ["on", "off"]:
tv_helper = MediaPlayerHelper()
if command == "on":
tv_helper.livingroom_system_on()
else:
tv_helper.livingroom_system_off()
return

source_list = hass.states.get(AVR).attributes.get("source_list")
elif command in AVR_INPUT:
_update_tv_picture_mode(command)
_handle_avr_source_change(command)

if command in avr_input:
if avr_input[command] in source_list:
media_player.select_source(entity_id=AVR, source=avr_input[command])
else:
log.warning(f"Source not found: {avr_input[command]}")
else:
log.warning(f"Unknown input command: {command}")
log.warning(f"Unknown AVR command: {command}")


def process_command_received(device, command):
def _process_command_received(device, command):

# Requested by RTI on connection established
if device == "states" and command == "get":
Expand All @@ -170,7 +239,11 @@ def process_command_received(device, command):
send_security_states()

elif device == "avr" or device == "home_theater":
handle_avr(command)
_handle_avr(command)

elif device == "tv" and "bri" in command:
command = command.replace("bri", "")
update_tv_brightness(int(command))

elif command == "light_preset":
if device == "alloff":
Expand All @@ -187,6 +260,13 @@ def process_command_received(device, command):
log.warning(f"Unknown command from RTI: cmd:{device}:{command}")


def _process_macro_received(device, command):
if device == "tv" and command == "tv_antenna":
tv_antenna_macro()
else:
log.warning(f"Unknown macro from RTI: macro:{device}:{command}")


@state_trigger(AVR)
@state_trigger(LIVINGROOM_TV)
def home_theater_state_changed():
Expand All @@ -212,7 +292,11 @@ def rti_message_received(payload=None):
command = split[2]

if mode == "cmd":
process_command_received(device, command)
_process_command_received(device, command)

elif mode == "macro":
_process_macro_received(device, command)

else:
log.warning(f"Unknown mode: {mode}")

Expand Down

0 comments on commit b265aa8

Please sign in to comment.