|
|
|
@@ -37,6 +37,7 @@ class Device: |
|
|
|
state_topic = f"light/{id}/state" |
|
|
|
command_topic = f"light/{id}/cmd" |
|
|
|
fx_state_topic = f"light/{id}/fx" |
|
|
|
ha_topic = 'homeassistant/status' |
|
|
|
|
|
|
|
ready = False |
|
|
|
changed = False |
|
|
|
@@ -45,33 +46,40 @@ class Device: |
|
|
|
power_on = True |
|
|
|
effect = None |
|
|
|
|
|
|
|
def advertise_locked(client, device): |
|
|
|
client.publish( |
|
|
|
f"homeassistant/light/{device.id}/config", |
|
|
|
json.dumps({ |
|
|
|
'unique_id': device.id, |
|
|
|
'device': { |
|
|
|
'name': "blinky", |
|
|
|
'identifiers': [device.id], |
|
|
|
}, |
|
|
|
|
|
|
|
'state_topic': device.state_topic, |
|
|
|
'command_topic': device.command_topic, |
|
|
|
'effect_state_topic': device.fx_state_topic, |
|
|
|
|
|
|
|
'schema': "json", |
|
|
|
|
|
|
|
'effect': True, |
|
|
|
'effect_list': device.effects, |
|
|
|
}) |
|
|
|
) |
|
|
|
|
|
|
|
device.publish_state_locked() |
|
|
|
|
|
|
|
def on_mqtt_connect(client, device, flags, rc): |
|
|
|
if rc == 0: |
|
|
|
with device.lock: |
|
|
|
print("Connected to MQTT") |
|
|
|
client.publish( |
|
|
|
f"homeassistant/light/{device.id}/config", |
|
|
|
json.dumps({ |
|
|
|
'unique_id': device.id, |
|
|
|
'device': { |
|
|
|
'name': "blinky", |
|
|
|
'identifiers': [device.id], |
|
|
|
}, |
|
|
|
|
|
|
|
'state_topic': device.state_topic, |
|
|
|
'command_topic': device.command_topic, |
|
|
|
'effect_state_topic': device.fx_state_topic, |
|
|
|
|
|
|
|
'schema': "json", |
|
|
|
|
|
|
|
'effect': True, |
|
|
|
'effect_list': device.effects, |
|
|
|
}) |
|
|
|
) |
|
|
|
client.subscribe(device.command_topic) |
|
|
|
|
|
|
|
device.ready = True |
|
|
|
if device.should_publish: |
|
|
|
device.publish_state_locked() |
|
|
|
Device.advertise_locked(client, device) |
|
|
|
|
|
|
|
client.subscribe(device.command_topic) |
|
|
|
client.subscribe(device.ha_topic) |
|
|
|
|
|
|
|
device.cond.notify_all() |
|
|
|
else: |
|
|
|
print(f"MQTT connection failed: {rc}") |
|
|
|
@@ -92,6 +100,9 @@ class Device: |
|
|
|
def on_mqtt_message(client, device, msg): |
|
|
|
if msg.topic == device.command_topic: |
|
|
|
device.set_mqtt_config(json.loads(msg.payload)) |
|
|
|
elif msg.topic == device.ha_topic: |
|
|
|
with device.lock: |
|
|
|
Device.advertise_locked(client, device) |
|
|
|
|
|
|
|
def __init__(self, effects=[]): |
|
|
|
print(f"Creating device with ID {self.id}") |
|
|
|
@@ -268,4 +279,4 @@ with device.lock: |
|
|
|
#while not file_changed and not device.is_changed(): |
|
|
|
# device.cond.wait(sleeptime) |
|
|
|
|
|
|
|
device.lock.acquire() |
|
|
|
device.lock.acquire() |