SpeakerCraft MZC Control

It’s working :grin:… Very cool… Had to tweak the py for the different arguments to the call back and there was a small issue with byte vs string but it’s mostly working. I have a bug with zone power off that I think I know what’s happening but just need a bit of time to fiddle. Quite keen to add bass & trebble support but also looks like a different or maybe custom card design would be nice as the default media player one seems rather limited… Not sure what’s involved there though… haven’t looked yet tbh.
Thanks again!!
Charles

@Sjeffrey - found the issue - turning off a Zone from HA was issuing both a power off and a volume set command and the callback for the volume set was overriding the power off command before it had been sent to the Speakercraft - I changed it to ignore any new command while an unissued command is waiting and it’s working great now

ah yes, i set the default volume when the zone is powered off, so it turns back on at the right volume.

this should of already been in the code, where it doesnt get another command until it has had the previous one confirmed. although if youve re-written the mqtt part then that might be why. You maybe using callbacks on mqtt subscribe which would mean multiple could fire simultaneously i guess.

Hi Stu,

I’ve created a new repository here based on your code.

Since you are new to GitHub I’ve done this so you can get a feel for it, but if you wish to I’d be happy to transfer the repo back to you for you to be the main maintainer for it. If not, I’m happy to do so, it’s your call and we can decide this later too.

The reasons for having a separate repo is 2-fold. Firstly I needed to change the structure to make it compatible with HACS (which is my proposed journey for making install easy for others). Secondly, I think it is better approach for maintenance. Specifically rather than having detailed conversations on this forum about issues, we should use the issue list in GitHub as it is better to thread each discussion.

My immediate plan is to get the HA component working well and I’ve made initial steps to do that for me (and I hope for others as follows);

  1. I’ve changed the code to use the native xonxoff (software flow handshake). This tidies up the code and the treatment of 0x11 and 0x13 messages.
  2. Move to using SerialReader/SerialWriter from pyserial-asyncio to improve how to deal with split messages (before I was getting lots of messages split across data_received(), so I think the new approach is easier)
  3. I’ve replaced many of the print() statement with logger to debug messages so they can be seen in the HA log.
  4. I’ve removed the hexlify to improve the performance of the parsing and am processing the binary bytes arrays as opposed to strings. (I’m not sure this was necessary for performance reasons, but since you had issues with processing speed I felt it may help).
  5. I’ve added version to manifest.json which I think fixes one of the issues previously mentioned about it “not working with latest home assistant”

Overall the performance of this for me is very good (RPi 3B+)

There’s still more work to do here, which I will response on other thread here, and also link to issues that I will create to track on GitHub.

@charlesc I’ve not experienced this issue myself, but I wonder if my new code will have addressed it for you anyway. I’d be happy to accept any proposed changes you have to the shared code I propose we maintain via GitHub PullRequest (PR) if you are game

@CharlesC I have the same issue with masterpoweroff as you I think, I’ve raised a specific issue on it here to track masterpoweroff doesn't function · Issue #1 · mattsaxon/HASS-SpeakercraftMediaPlayer · GitHub

On the tone support, can I suggest you take a look here which may give some pointers Mini media player card layout. However before we get too many users on this, can I query the use of term “trebble”, should it not be “treble” as a name change is harder the more people are using it.

@mattsaxon this is great to see. I am currently away so won’t be able to look properly until later next week.

I started work on this component again last week looking for a way to handle the split messages but your way is much better. I’ll test it out when I’m back.

I agree with most your proposed work and can help when I’m back.

I’m sure the master off used to work. I don’t use party mode was so to turn off all zones at once. I don’t think there is a need to turn all on.

I was also using a smart power switch to turn on off the whole unit.

I’ve updated the GitHub issues about masterpoweroff, I’ve fixed the issue in the code which looks like you were in the middle of fixing a move from sync to async. It now will update the “external” switch entity, but I can’t see how you intended to use an external switch to do the power off. What was your external entity and did you have some automation attached to it, I’m using an input_boolean switch, which is just passive and reports the status of amplifiers zones at the moment.

Just looking at code. Looks good. Not had chance to test.

On sending the command are you waiting for the command window on the serial hex 11 and sending before the window closes hex 13. If you send outside the window it is ignored. Maybe I’ve missed that bit.

Master off maybe a waste of time as can be done via script/automation

I can’t remember what is in this code vs mocropython.

I think the commands need to be queued in a fifo queue. And then only cleared when a confirmation is received so if the window is missed on thst cycle it’s able to reattempt… it needs queueing so then you can wait for 11 without locking system and receive multiple command quickly eg vol up.

I want to retain power target which controls a snart plug connected to device as the standby on speakercraft I don’t trust. So if all zones are off then it should wait a set time then power off smart plug. Also if the system is powered off it won’t recieve the command window so this is when to check if power target is powered on and if not seotch on

Hope that makes sense.

Treble should be renamed If I spelt it wrong

Sorry wish I was home to test properly and communicate with you.

Thanks for your big contribution. Hopefully we can get this to be a descent integration.

On 0x11 and 0x13, the reason that you can’t see that in the code is that implementing this is as simple as selecting xonoff when you open the serial connection (see line 304). See Software flow control - Wikipedia for the details of the protocol

In terms of the confirmations, the code you sent originally recognised them, but didn’t do anything special with them, I was wondering if we needed to do anything, but in testing so far, it hasn’t proved necessary. It may do under further testing, we shall see.

Your use of the plug makes sense, I’ll ponder how PartyMode switch can be created in a consistent way.

I’ll rename trebble->treble now.

Don’t sweat not being there, I’m away for next couple of weeks too.

Oh my I wish I had of known flow control earlier. Makes things so much easier. I’ve code a few integrations for speaker craft and always done it manually. Including in .net for home seer

Probably much more resource efficient this way. And less likely to miss the window as its much closer to hardware layer.

Feel stupid lol.

With regards to the power target probably best to drop it. But if its possible to have some way of triggering an automation when a command is received then I can do automation which is triggered by this component and then condition can be switch is off and then it can turn off the smart plug. When all zones are off an automation can turn the plug off. If that makes sense.

It’s speakercraft’s fault IMO, their docs describe the detail of the protocol without saying it’s a standard. I can’t recall how I worked this out.

On the power target, I think the way you envisage it is the right way to do it, I’ve updated the readme to describe it now. Let’s leave it in there as is and I will add Party Mode implementation as another switch that belongs to the speakercraft platform

hi not sure why that would be happening. most odd.

I’d suggest that the HA solution (in matts github) is probably the best way to go. Especially if your controller is located close to the HA. Just expect alot of changes, as we get it working efficiently.

Stu

I’m a few steps behind you (the TTL DB9 took forever to arrive) and am starting to look at how to upload the files to the ESP32. I’ve been using Arduino ide for other projects but it looks like I need something else for the micro python. What did you end up using? Any tips or details would be a big help. The HA Solution won’t work for me and I’d love to get the ESP32 route working.

sorry i havent given more instructions. I have literally only done it once and that was about 2 years ago.

I think i would of followeded these instructions. here

once its uploaded you can connect to a REPL prompt over serial and setup WebREPL which allows you to upload the 2 files over the web interface.

just out of interest why wont the HA solution work for you, is it due to location?

I was able to install Python, PIP and esptool then put micro python on the ESP32. There was some trial and error as some of the sites weren’t that clear and missing some steps. They probably figure if you’re trying to do this, you have a little bit of a clue. (they are wrong)

My next step is to load the boot.py and main.py files. I’m going to try ampy tonight but that may be hit or miss. I’m trying to take good notes and will post once I get it figured out. I like trying to figure this stuff out but some days I just want it to work the first time.

Regarding the HA Solution, my HA Pi is at the other end of the house so I’d need to relocate it to the MZC to make that work. I also have another MZC I was going to deploy for the garage/other side of the house so using another ESP32 (if that’s even possible with this setup) would be better.

A question for those that know more, once this is all dialed in, would it be possible to add to esphome so it would be easier for others to roll out? I’m not as savvy as most of the people here but it looks like they are using Python and C++. The first sensors I made were the Bru style and took me longer to deploy but after using ESPHome, it was way easier to build and even create new styles from the available components.

https://esphome.io/guides/contributing.html#contributing-to-esphome

let us know how you get on with the ESP32.

I think actually there is an error on it as now MQTT server requires a timeout, whereas the code i wrote at the time didnt. it will work with an older version of mosquitto.

ESPHome, would make things alot easier to deploy to be honest. But it was easy for me to write the code in python, rather then C.

I think at the moment, i will be mainly concentrating on the HA integration, as it doesnt require additional hardware. Eventually may integrate a new ESP version.

The ESP32 route isn’t going so well. I’ve uploaded the files to the ESP32 and put the mqtt_media folder with files in the custom_components folder in the HA config folder but even after multiple restarts, I’m not seeing mqtt_media anywhere which is preventing me from adding the zones in the configuration.yaml file. I’m running Mosquitto broker version: 6.0.1, could this not showing up be due to the “MQTT server requires a timeout” as @Sjeffrey mentioned? I looked through the logs and I’m not seeing anything about mqtt_media. Any thoughts? I really can’t check out the hardware side of things until it’s running in HA.

Some notes for others regarding my ESP32 experience:

In the boot.py file, make sure you change the file to put in your wifi details in line 13 before uploading.

sta_if.connect('Wifi', 'Pass')

In the main.py file, you may need to update MQTT Client address at line 106 before uploading.

client = MQTTClient("speakercraft", "192.168.0.100", 1883)

Another tip that took me a little bit to figure out, I DID NOT update the main.py file like I said above but once it was loaded, I wasn’t able to access the ESP32 with ampy. The put command should overwrite the file but it wasn’t working for me. Maybe the ESP32 was in some loop preventing it? Since I had trouble my first time, I’ll post what worked for me in case it will help others.

Launch a CMD window from the folder where the boot.py and main.py files are stored.
From the CMD window, use the following command to copy the main.py file to the ESP32:

ampy --port COM3 put main.py

I was on port 3 but change to whatever you are on. You can find this in the Device Manager>Ports.

I needed to remove the main.py file so I could upload the new version. I used this command:

ampy --port COM3 rm main.py

I hope that helps someone. Let me know if you need any other details, I’ll share what little I know.

Hi,

Sorry i have not given very good instructions. Really is because it was so long ago i did this, and just left it working.

have you added the configuration to configuration.yaml

That should allow the media player components to create in HA. Clicking on any buttons will then send a command to MQTT topic eg speakercraft/command/zone1

Its worth installing a MQTT client in windows so you can see what its doing. Obviously the device wont turn on, unless the ESP32 is turning it on and then reporting back that it is on.

If the ESP32 is reporting status it should be visible in the MQTT client. I think that the last version of mosquitto the current code works with is 2.0.11. Version 2.0.12 required a timeout parameter. Mosquitto is only up to version 2.0.14 so not sure what version 6 is?

I dont know about ampy.

I used WEBRPL MicroPython WebREPL to connect and this also shows the output of the ESP32 so you can view any errors.

Are you able to see what the ESP32 is outputting?

Im also wondering if the version of micropython installed might make a difference… I did it a long time ago, so it might be that something has changed here.

Clearly installing MP is not easy. - we need to come up with a better route for the ESP32.

I can’t get as far as having the configuration.yaml edits because it’s not seeing the custom integration when I try. Has anyone else got this running in HA?

Without this running, I can’t really troubleshoot anything ESP32 or even know if that part is working.

No worries on the level of the documentation. I do the same thing, make a project work and then forget about it. In fact, I’m trying to find my EZ Tools programs for the MZC to edit! If I can get the ESP32 up and running, I’ll send you install notes to add to the github. I do appreciate the work you’ve done on the project though.