Controlling LG MusicFlow Soundbars in Home Assistant

I wanted to automate most of the bits of playing a vinyl on my record player, and had successfully automated muting/unmuting of the IKEA Symfonisk speakers in my lounge when the record player was being used (power draw spikes to >1W during playback, and <0.3W on idle), however I wanted to automate switching the input on the LG LA855M sound bar that it's connected to as well.

I did some searching and stumbled across https://github.com/mafredri/musicflow, which is a CLI tool written in Go. It works well (although the interface is a bit eh), however I did not want to shave the yak that is getting that hooked up to Home Assistant, nor did I have the time to write a proper integration.

During my searching, I found https://community.home-assistant.io/t/sending-simple-tcp-packets/52070/2 where someone dumped the packets from the MusicFlow app and had success with using nc to send payloads.

I used tcpdump to capture the packets when I sent a command, then used Wireshark to inspect and grab the bytes I needed.

# on machine running mufloctl
sudo tcpdump -vv -XX -w portable.pcap port 9741

# in another shell, send the payload
echo '{"data":{"type":2},"msg":"FUNCTION_SET"}' | mufloctl -addr <IP of soundbar>

# ^C to end the tcpdump

Once I had the bytes, I wrote a quick script to convert the bytes to \xXX format, so I can use echo and nc to send to the soundbar.

# Switch to optical source

echo "\x10\x00\x00\x00\x30\xec\x01\x16\x5d\x79\x2c\xfc\xcd\x89\x02\x77\x39\x2f\x3a\x33\x5f\xb3\xd5\x76\x24\xad\x84\x59\x71\xa1\x8b\xcd\xbe\xd6\xae\x0f\xfa\x14\x9e\x24\xac\x48\x6a\x4f\x18\xc9\xf5\xed\x0b\x45\xd8\x4b\x85" | nc -vv <ip> 9741


# Switch to portable source

echo "\x10\x00\x00\x00\x30\xec\x01\x16\x5d\x79\x2c\xfc\xcd\x89\x02\x77\x39\x2f\x3a\x33\x5f\xac\xcb\x2f\xb1\xeb\x99\xf6\xa4\x47\xb1\xa8\x0a\x01\x13\x64\x0d\x1d\x6f\xe7\xf5\x94\x5e\xec\xc9\x2b\x47\xda\x1d\xc4\x4e\xc8\x6e" | nc -vv <ip> 9741

I was rather stoked that this actually worked! I skimmed the musicflow source code and it appears messages are JSON payloads which are AES encrypted with a static key and IV, which makes this possible.

Unfortunately, Home Assistant does not let you use pipes in shell_command, so you will need to create a script that does so, as per https://community.home-assistant.io/t/sending-simple-tcp-packets/52070/2.

Hope this helps.