Description
Describe the bug
After I ran into #194 I tried using the own MQTT client approach and handle connecting the MQTT broker myself but this doesn't seem to work with the button entity.
The script stops with a bogus DNS error message:
socket.gaierror: [Errno -3] Temporary failure in name resolution
The network is fine though and Home Assistant is reachable via the hostname (I tried using the IP address directly too). If I use a sensor entity instead of the button the code runs as expected.
It looks like the button entity is trying to connect the already connected client again, but I'm at a loss on why this is happening.
To Reproduce
Here's a minimal example to reproduce:
import logging
from paho.mqtt import client as MQTTClient
from ha_mqtt_discoverable import Settings, DeviceInfo
from ha_mqtt_discoverable.sensors import Button, ButtonInfo
# Entity instances
button1: Button
def connect_mqtt() -> MQTTClient.Client:
# For paho-mqtt 2.0.0, you need to add the properties parameter.
def on_connect(client, userdata, flags, rc, properties):
if rc == 0:
logging.info("Connected to MQTT Broker!")
else:
logging.error("Failed to connect, return code %d\n", rc)
# For paho-mqtt 2.0.0, you need to set callback_api_version.
client = MQTTClient.Client(client_id="ButtonTestClient", callback_api_version=MQTTClient.CallbackAPIVersion.VERSION2)
client.username_pw_set("mqtt", "hamqtt")
client.on_connect = on_connect
client.connect(host="homeassistant.local", port=1883)
return client
# Main
if __name__ == "__main__":
# Connect to MQTT broker
client = connect_mqtt()
client.loop_start()
# Define the device. At least one of `identifiers` or `connections` must be supplied
device_info = DeviceInfo(name="ButtonTestDevice", identifiers="buttontestdevice1")
# Configure the required parameters for the MQTT broker
mqtt_settings = Settings.MQTT(client=client)
# Information about the entities
button_info = ButtonInfo(name="Button", unique_id="button12345", device=device_info)
# Create entity settings
button_settings = Settings(mqtt=mqtt_settings, entity=button_info)
# Instantiate the entities
button1 = Button(button_settings, None, None)
# Publish the button's discoverability message to let HA automatically notice it
button1.write_config()
while(True):
pass
And the full output:
~/Code/boobox/software/BooBox_Audio/src$ python Button.py
Traceback (most recent call last):
File "/home/simon/Code/boobox/software/BooBox_Audio/src/Button.py", line 52, in <module>
button1 = Button(button_settings, None, None)
File "/home/simon/Code/boobox/.venv/lib/python3.10/site-packages/ha_mqtt_discoverable/__init__.py", line 889, in __init__
self._connect_client()
File "/home/simon/Code/boobox/.venv/lib/python3.10/site-packages/ha_mqtt_discoverable/__init__.py", line 719, in _connect_client
result = self.mqtt_client.connect(self._settings.mqtt.host, self._settings.mqtt.port or 1883)
File "/home/simon/Code/boobox/.venv/lib/python3.10/site-packages/paho/mqtt/client.py", line 1435, in connect
return self.reconnect()
File "/home/simon/Code/boobox/.venv/lib/python3.10/site-packages/paho/mqtt/client.py", line 1598, in reconnect
self._sock = self._create_socket()
File "/home/simon/Code/boobox/.venv/lib/python3.10/site-packages/paho/mqtt/client.py", line 4609, in _create_socket
sock = self._create_socket_connection()
File "/home/simon/Code/boobox/.venv/lib/python3.10/site-packages/paho/mqtt/client.py", line 4640, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
File "/usr/lib/python3.10/socket.py", line 824, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.10/socket.py", line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution
Expected behavior
The button entity should be advertised to Home Assistant via the existing MQTT client connection
Environment:
- Ubuntu 20.04
- Python 3.10.12
- Packages
ha-mqtt-discoverable 0.18.0
paho-mqtt 2.1.0