Sure, this is the portion of code in question. I got most of it from SmartishHome.com, but was able to make a power switch, and add output as well, but I decided to break it out into different zones (rooms) for the sake of ease on my family. Coming from Control4 has been quite a learning curve (since it comes pretty much standard out of the box). Hopefully the formatting shows correctly.
Thanks for any insight/help.
configuration.yaml
input_select:
radio_stations:
name: Radio Station
icon: mdi:radio
options:
- 103.5
- 105.5
- 106.3
- 107.3
amp_input:
name: Audio Input
icon: mdi:arrow-right-bold
options:
- 1
- 2
- 3
- 4
automations.yaml
- id: C4 AMP Input
alias: Amp Input
trigger:
platform: state
entity_id: input_select.amp_input
action:
service: control4_services.handle_amp_input_select
/custom_components/control4_services.py
import logging
import socket
import random
_LOGGER = logging.getLogger(name)
DOMAIN = ‘control4_services’
def send_udp_command(command, host, port):
COUNTER = “0s2a” + str(random.randint(10, 99))
COMMAND = COUNTER + " " + command + " \r\n"
_LOGGER.warn(“Sending command: %s”, COMMAND)
HOST = host
PORT = port
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto( bytes(COMMAND, "utf-8"), (HOST, PORT))
received = str(sock.recv(1024), "utf-8")
_LOGGER.warn("Command sent. Response: %s", str(received))
def setup(hass, config):
def handle_tuner_channel_select(call):
"""Select input source."""
_LOGGER.warn("Changing tuner channel...")
amp_input = hass.states.get("input_select.amp_input").state[:1]
tuner_station = hass.states.get("input_select.radio_stations").state
tuner_station = float(tuner_station.partition(" ")[0]) * 100
tuner_station = hex(int(tuner_station))[2:]
# tafreq2 = Tuner 1 | tbfreq2 = Tuner 2
send_udp_command("c4.mt.tafreq2 00 " + str(tuner_station), "192.168.5.14", 8750)
_LOGGER.warn("Changed tuner channel...")
def handle_amp_volume_select(call):
_LOGGER.warn("Changing amp volume...")
volume_select = hass.states.get("input_number.amp_volume").state
volume_select = int(float(volume_select) * 60) + 160
volume_select = hex(volume_select)[2:]
send_udp_command("c4.amp.chvol 01 " + volume_select, "192.168.5.11", 8750)
_LOGGER.warn("Volume set...")
def handle_amp_input_select(call):
_LOGGER.warn("Changing amp input...")
amp_input = hass.states.get("input_select.amp_input").state[:1]
amp_input = hex(int(amp_input))[2:]
send_udp_command("c4.amp.out 01 0" + amp_input, "192.168.5.11", 8750)
_LOGGER.warn("Amp input changed...")
hass.services.register(DOMAIN, 'handle_tuner_channel_select', handle_tuner_channel_select)
hass.services.register(DOMAIN, 'handle_amp_volume_select', handle_amp_volume_select)
hass.services.register(DOMAIN, 'handle_amp_input_select', handle_amp_input_select)
# Return boolean to indicate that initialization was successfull.
return True