Home Assistant direct control of Control4 AMP and Tuner (no plugin needed)

At the risk of sounding arrogant…:wink:

I’m far from a coder. I am a novice when it comes to coding, and when I look at code, my mind goes blank…it’s Chinese to me it’ll take me a lifetime to learn Chinese… that’s why I have Google translate…

frankly I’m surprised I got this far… :rofl:

My goal isn’t to understand how to code/how the code works…

My goal is to copy, paste, and hope for the best…

If it works as intended, that’s a plus…

I’m setting this up for a friend who has less patience than me… I almost have no hair left trying to figure out how to do this…

Do you think grok or ChatGPT would work to compile the code that I need?

In short: Any and all assistance would be most welcomed and appreciated…

Thank you,

Edit* this is what my init.py looks like…

Then the bulk of the code is in control4Amp.py. You can display that.

import socket
import random
import select

def send_udp_command(command, host, port):
    COUNTER = "0s2a" + str(random.randint(10, 99))
    COMMAND = COUNTER + " " + command + " \r\n"

    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.settimeout(1)
        sock.setblocking(0)
        sock.sendto(bytes(COMMAND, "utf-8"), (host, port))
        data, _ = sock.recvfrom(1024)
        received = str(data, "utf-8")
    except socket.timeout:
        received = "Timeout occurred during data reception"
    except Exception as e:
        received = {e}
    finally:
        sock.close()

    return received




class control4AmpChannel(object):
# Represents a channel of a Control 4 Matrix Amp

    def __init__(self, host, port, channel):
        self._host = host
        self._port = port
        self._channel = channel
        self._source = 1
        self._volume = 0

    @property
    def host(self):
        return self._host

    @property
    def port(self):
        return self._port

    @property
    def channel(self):
        return self._channel

    @property
    def source(self):
        return self._source

    @source.setter
    def source(self,value):
        self._source = value
        return send_udp_command("c4.amp.out 0" + str(self._channel) + " 0" + str(self._source), self._host, self._port)

    @source.deleter
    def source(self):
        del self._source

    @property
    def volume(self):
        return self._volume

    @volume.setter
    def volume(self,value):
        self._volume = value
        new_volume = int(float(self._volume) * 100) + 160
        new_volume = hex(new_volume)[2:]
        send_udp_command("c4.amp.chvol 0" + str(self._channel) + " " + new_volume, self._host, self._port)

    @volume.deleter
    def volume(self):
        del self._volume

    def turn_on(self):
        send_udp_command("c4.amp.psave 00 00", self._host, self._port)
        return send_udp_command("c4.amp.out 0" + str(self._channel) + " 0" + str(self._source), self._host, self._port)

    def turn_off(self):
        return send_udp_command("c4.amp.out 0" + str(self._channel) + " 00", self._host, self._port)

@Mr.Robot, I’m a little lost as to what the issue or issues are or what you’re trying to accomplish. Can you summarize where you’re at?

My sliders work, and the volume changes between zero and 100%. Mute is programmed to set the volume to zero. When you say your sliders are out of sync, where are you observing this? Are you talking about a single output or your master slider?

I don’t know much about music assistant, but from a quick read, it streams music to a device, so you would need a device connected to C4 to stream to. I didn’t see anything about outputting directly from home assistant. I use a Sonos Play connected to my C4.

This should work, but you’ll need to create a button for the mute. Not sure where the rest of the code is.

Add this under @volume.setter

@volume.mute
def mute(self,value):
    send_udp_command("c4.amp.mute 0" + str(self._channel) + " " + new_volume, self._host, self._port)

Thank you,:pray:t3:

More code is needed than what @bigmac posted, and I don’t understand my his mute code has + new_volume. The mute function in my media_player.py is coded to set the volume to zero because I didn’t know the command to mute when writing it.

If you want help, please summarize where you are at and what you’re trying to solve.

I’m just shooting in the dark as I have no idea where the remainder of the code is, but it’s your stuff, and now you know the mute command.

What other code is needed for it to work?

Also why is this not supported?

Thank you,

type: custom:mini-media-player
name: All Zones
entity: media_player.zone_1
hide:
  play_pause: true
  volume_level: false
  source: false
  mute: true
speaker_group:
  platform: media_player
  show_group_count: true
  entities:
    - entity_id: media_player.zone_1
      name: Zone 1
    - entity_id: media_player.zone_2
      name: Zone 2
    - entity_id: media_player.zone_3
      name: Zone 3
    - entity_id: media_player.zone_4
      name: Zone 4
    - entity_id: media_player.zone_5
      name: Zone 5
    - entity_id: media_player.zone_6
      name: Zone 6
    - entity_id: media_player.zone_7
      name: Zone 7
    - entity_id: media_player.zone_8
      name: Zone 8
group: true
toggle_power: true
volume_stateless: false
source: icon

To tell you why this is not supported, I’d need to know what “this” is.

I assume “this” means wanting the C4 custom component to be able to join all channels together. The mini media player group function joins different devices, like multiple Sonos amps, using the Sonos media players’ join function. This was never contemplated when I wrote the C4 component.

I’d have to wrap my head around adding a join function to the C4 media player, as I don’t know of a native “join channels” command. It’s more than I can take on right now.

I handle this type of situation with scripts. I have a script to turn off all channels, and a few scripts to turn on multiple channels for specific situations. e.g., “turn on outside” turns on all three levels of my deck speakers. “turn on bedroom” turns on both the bedroom and bedroom bathroom speakers.

Hi Folks,

Anyone else not working after updating to Core 2024.10.3?

I get these warnings anytime I try to change inputs, and get no audio out no matter what source I choose:

This error originated from a custom integration.

Logger: custom_components.control4-media-player.media_player
Source: custom_components/control4-media-player/media_player.py:149
integration: control4-media-player
First occurred: 3:38:35 PM (34 occurrences)
Last logged: 3:42:17 PM

Source set to Home Assistant
Source set to Gym Bridge
Source set to Bridge
Source set to Server

image

It’s working for me on 2024.10.3

Thanks @Hansen8601! I ended up power cycling the amplifier, and it is working again…very strange. I was listening to it right before the upgrade. I will have to keep an eye on that.