Allow closing via mqtt

Valentin Ochs 2023-05-17 16:04:53 +02:00
parent 28323ef0ce
commit 6d98849da1
2 changed files with 8 additions and 2 deletions

View File

@ -24,7 +24,8 @@ class Control(util.Loggable):
self.target = mqtt.Value(mqtt_client, "door/state/target",
persistent=True,
translate=state_names)
translate=state_names,
remote_update_callback = lambda _, _, msg: None if msg != b'closed' else self.close())
self.state = mqtt.Value(mqtt_client, "door/state/value",
persistent=True,
translate=state_names)

View File

@ -19,7 +19,8 @@ class Value:
typing.Dict[Any, str],
typing.Callable]
= None,
max_update: float = 0.1):
max_update: float = 0.1,
remote_update_callback = None):
self.client = client
self.topic = topic
self.persistent = persistent
@ -39,6 +40,10 @@ class Value:
if start_value is not None:
self.update(start_value)
if remote_update_callback is not None:
self.client.message_callback_add(self.topic, remote_update_callback)
self.client.subscribe(self.topic, 2)
def update(self, value: Any, *,
force: bool = False,
no_update: bool = False) -> None: