MQTT
The speaker can announce itself on an MQTT broker the way ESPHome devices do. Home Assistant's MQTT integration then finds it with its controls, and anything else on the broker (Node-RED, openHAB, scripts) can read and set them. It works next to the ESPHome API or instead of it.
It is off until the owner turns it on.
Connecting
- Open Settings on the speaker's web interface. Under MQTT, enter the Broker (host name or IP address) and Port: 1883, or 8883 with Encrypted (TLS).
- Enter User name and Password if the broker wants them. For Home Assistant's Mosquitto add-on, create a Home Assistant user for the speaker and use it.
- Choose Save, then turn on Announce the speaker on an MQTT broker. The part shows Connected once the broker took the speaker.
In Home Assistant the speaker shows up under Settings → Devices & services → MQTT with its entities. Nothing has to be confirmed.
If the speaker cannot connect, the part says why (wrong password, name not found, refused) and the speaker tries again, waiting longer each time, up to a minute between attempts.
What the broker gets
| Topic | Contents |
|---|---|
<prefix>/status | online, or offline when the speaker switches MQTT off or disappears (the broker's last will). Retained. |
<prefix>/<component>/<object_id>/state | The entity's state. Retained. |
<prefix>/<component>/<object_id>/command | Commands, from Home Assistant or anyone allowed to publish there. |
<discovery>/<component>/boombox_<mac>/<object_id>/config | Home Assistant's discovery. Retained. |
<prefix> is the speaker's network name (boombox) unless you choose a Topic prefix; it follows a rename, like ESPHome's. <discovery> is homeassistant unless you changed Home Assistant's discovery prefix.
| Entity | Component | State | Commands |
|---|---|---|---|
| Playback | sensor | playing, paused, announcing, idle | |
| Volume | number | 0–100, the speaker's own volume, capped by the limit | a number |
| Mute | switch | ON, OFF | ON, OFF |
| Play, Pause, Stop, Next track, Previous track | button | PRESS | |
| Cover | sensor | the cover's address on the speaker, None without one | |
| Position, Duration | sensor | seconds | |
| Title, Artist, Album, Playing from | sensor | the text, None without a phone | |
| Audio output, Listen on | select | the option's label | a label |
| Maximum volume | number | 10–100 | a number |
| Screen | switch | ON, OFF | ON, OFF |
Home Assistant has no MQTT media player, so the speaker's player appears as what it is made of: the playback sensor, volume, mute and the buttons. Text to speech, announcements and media URLs stay with the ESPHome API, and so do Restart and Bluetooth pairing: anyone who may publish on the broker could otherwise restart the speaker, or open it to pairing with any device in range. An update clears their old announcements, so Home Assistant drops them from the MQTT device.
Switching MQTT off on the speaker removes the entities from Home Assistant (empty retained discovery messages) and sets the status to offline. Changing the discovery prefix moves them.
Example, from any machine with the Mosquitto clients:
mosquitto_sub -h broker.lan -u user -P pass -v -t 'boombox/#'
mosquitto_pub -h broker.lan -u user -P pass -t boombox/number/volume/command -m 30
mosquitto_pub -h broker.lan -u user -P pass -t boombox/button/pause/command -m PRESSSecurity
- Whoever may publish to
<prefix>/+/+/commandon the broker controls what it plays, its volume and output. Give the speaker a broker user of its own and restrict the topics with the broker's ACLs. - Without TLS the password and every message cross the network in plain text. With TLS the speaker checks the broker's certificate against the system's authorities, so a self-signed certificate is refused.
- The password stays in
/var/lib/boombox/config.json(root only). Pages only learn whether one is saved; Remove password deletes it. - The TV's account neither sees nor changes these settings, at any access level.
- Packets larger than 64 KB end the connection, and a broker that stops reading is left after ten seconds: neither can hold up the music.
How it works
| Part | Where |
|---|---|
| MQTT 3.1.1 packets | packages/boombox-agent/src/Mqtt/Packet.php, PacketReader.php |
| The connection: keep-alive, last will, reconnects | Mqtt/MqttClient.php |
| Discovery, states, commands | Mqtt/Discovery.php; the entities' mqtt*() methods in EspHome/Entity/ |
| Settings and lifecycle | Service/MqttService.php |
States go out at QoS 0 and retained; all of them are sent again after every connect. Commands at QoS 1 are acknowledged.
Development
DDEV runs a Mosquitto broker as mosquitto:1883, anonymous:
ddev boombox-agent client mqtt.settings.set '{"enabled":true,"host":"mosquitto"}'
ddev exec -s mosquitto mosquitto_sub -v -t '#'
ddev exec -s mosquitto mosquitto_pub -t boombox/switch/mute/command -m ONThe Playwright spec mqtt.spec.ts connects to it.
Troubleshooting
| Symptom | Cause and fix |
|---|---|
| "Wrong user name or password" | Check the credentials; with Home Assistant's Mosquitto add-on, a Home Assistant user. |
| "The broker refused the connection" | Nothing listens there: host and port, and whether the broker runs. |
| "TLS failed" | The certificate is self-signed or for another name. Use one from a public authority, or plain MQTT inside the LAN. |
| Connected, but nothing in Home Assistant | Home Assistant's MQTT integration uses another discovery prefix, or another broker. Compare Discovery prefix. |
| Entities show "Unavailable" | <prefix>/status is offline: the speaker lost the broker. The MQTT part on its Settings page says why. |