Open
Description
Describe the bug
command_callback
do not work reliably when a device has multiple entities.
To Reproduce
Here a minimal example script.
It creates one device and add a Sensor and a Switch:
import time
from ha_mqtt_discoverable import DeviceInfo, Settings
from ha_mqtt_discoverable.sensors import Sensor, SensorInfo, Switch, SwitchInfo
mqtt_settings = Settings.MQTT(host="localhost")
device_info = DeviceInfo(name="My device", identifiers="device_id")
def get_temperature_sensor():
temperature_sensor = Sensor(
Settings(
mqtt=mqtt_settings,
entity=SensorInfo(
name='Chip Temperature',
unit_of_measurement='°C',
state_class='measurement',
device_class='temperature',
unique_id="temperature",
device=device_info,
),
)
)
temperature_sensor.set_state(state=random.randint(0, 100))
return temperature_sensor
class Relay:
def __init__(self):
self.relay_switch = Switch(
Settings(
mqtt=mqtt_settings,
entity=SwitchInfo(
name='Relay',
unique_id="relay",
device=device_info,
payload_on='ON',
payload_off='OFF',
),
),
command_callback=self.relay_callback,
)
self.relay_switch.on()
def relay_callback(self, client, user_data, message):
payload = message.payload.decode()
print(f'\n**** Switch Callback: {client=} {user_data=} {payload=}')
if payload == 'ON':
self.relay_switch.on()
else:
self.relay_switch.off()
relay = Relay()
temperature_sensor = get_temperature_sensor() # <<< comment this out, then it works fine
while True:
time.sleep(1)
print('.', end='', flush=True)
It works, if the "temperature sensor" will be not initialized. Then you can switch the "Relay" ON and OFF in Home Assistant and the callback will be call every time.
But if the "temperature sensor" is also added, then the callback will be sometimes called and sometimes not.
Metadata
Metadata
Assignees
Labels
No labels