From 52f1b710cfeb9cc464cd1af511898fecba933671 Mon Sep 17 00:00:00 2001 From: jziolkowski Date: Sun, 15 Sep 2024 14:18:16 +0200 Subject: [PATCH] refactor is_online property --- tdmgr/GUI/dialogs/main.py | 6 +++--- tdmgr/models/devices.py | 2 +- tdmgr/mqtt.py | 2 +- tdmgr/run.py | 1 - tdmgr/tasmota/device.py | 13 ++++++++++--- tdmgr/util/__init__.py | 0 6 files changed, 15 insertions(+), 9 deletions(-) delete mode 100644 tdmgr/util/__init__.py diff --git a/tdmgr/GUI/dialogs/main.py b/tdmgr/GUI/dialogs/main.py index dfceac6..06450d4 100644 --- a/tdmgr/GUI/dialogs/main.py +++ b/tdmgr/GUI/dialogs/main.py @@ -374,15 +374,15 @@ def mqtt_message(self, msg: Message): if device := self.env.find_device(msg): if msg.is_lwt: log.debug("MQTT: LWT message for %s: %s", device.p["Topic"], msg.payload) - device.update_property("LWT", msg.payload) + device.online = msg.payload - if msg.payload == device.p["Online"]: + if device.online: # known device came online, query initial state self.initial_query(device, True) else: # forward the message for processing - device.update_property("LWT", device.p["Online"]) + device.online = True device.process_message(msg) # TODO: ditto diff --git a/tdmgr/models/devices.py b/tdmgr/models/devices.py index 5999f90..8c6deea 100644 --- a/tdmgr/models/devices.py +++ b/tdmgr/models/devices.py @@ -128,7 +128,7 @@ def data(self, idx, role=Qt.DisplayRole): return val if role == DeviceRoles.LWTRole: - return d.is_online + return d.online if role == DeviceRoles.RestartReasonRole: return d.p.get("RestartReason") diff --git a/tdmgr/mqtt.py b/tdmgr/mqtt.py index 56cd56b..31cfbfa 100644 --- a/tdmgr/mqtt.py +++ b/tdmgr/mqtt.py @@ -40,7 +40,7 @@ def initial_commands(): commands = [(command, "") for command in commands] commands += [("status", "0"), ("gpios", "255")] - for sht in range(4): + for sht in range(8): commands.append([f"shutterrelay{sht + 1}", ""]) commands.append([f"shutterposition{sht + 1}", ""]) diff --git a/tdmgr/run.py b/tdmgr/run.py index 8549455..26ca1b2 100644 --- a/tdmgr/run.py +++ b/tdmgr/run.py @@ -26,7 +26,6 @@ def configure_logging(args) -> None: elif args.log_location: log_path = os.path.join(args.log_location, "tdm.log") - logging.basicConfig( level="DEBUG" if args.debug else "INFO", datefmt="%Y-%m-%d %H:%M:%S", diff --git a/tdmgr/tasmota/device.py b/tdmgr/tasmota/device.py index 486a05f..2990eac 100644 --- a/tdmgr/tasmota/device.py +++ b/tdmgr/tasmota/device.py @@ -270,12 +270,12 @@ def power(self): def shutters(self) -> dict: return { k: self.p[f"ShutterRelay{k}"] - for k in range(1, 5) + for k in range(1, 9) if f"ShutterRelay{k}" in self.p and self.p[f"ShutterRelay{k}"] != 0 } def shutter_positions(self) -> dict: - x = {k: self.p[f"Shutter{k}"] for k in range(1, 5) if f"Shutter{k}" in self.p} + x = {k: self.p[f"Shutter{k}"] for k in range(1, 9) if f"Shutter{k}" in self.p} return x def pwm(self): @@ -335,9 +335,16 @@ def name(self): return self.p["Topic"] @property - def is_online(self): + def online(self): return self.p.get("LWT", self.p["Offline"]) == self.p["Online"] + @online.setter + def online(self, val: Union[bool, dict]): + if isinstance(val, bool): + self.update_property("LWT", self.p["Online"]) + else: + self.update_property("LWT", val) + @property def url(self) -> Optional[str]: if self.ip_address != "0.0.0.0": diff --git a/tdmgr/util/__init__.py b/tdmgr/util/__init__.py deleted file mode 100644 index e69de29..0000000