From 3ea1266e2ee59d611fcbf5d4b8e22de71a48c0e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sondre=20Gr=C3=B8n=C3=A5s?= <44143748+sondregronas@users.noreply.github.com> Date: Sun, 7 Jul 2024 20:18:47 +0200 Subject: [PATCH] Small rework of online/heartbeat property --- README.md | 6 +++--- .../ankermake/ankermake_mqtt_adapter.py | 12 +++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 074dffc..36ae47a 100644 --- a/README.md +++ b/README.md @@ -40,9 +40,9 @@ There are probably many issues to list... - Config flow will not verify the connection to the ankerctl instance (it will just assume it's correct) - No camera support (but can be worked around using go2rtc, though PPPP crashes a lot - stable when it doesn't crash!) - There are no ways to pause/stop a print -- There are no unit tests :( -- Logging is pretty much non-existent, documentation is lacking -- ankerctl can crash sometimes hindering the integration from working until it's restarted +- There are (almost) no unit tests :( +- Logging is pretty much non-existent, documentation is a bit lacking +- ankerctl can crash sometimes, hindering the integration from working until it's restarted ## Testing diff --git a/custom_components/ankermake/ankermake_mqtt_adapter.py b/custom_components/ankermake/ankermake_mqtt_adapter.py index e2492ad..9a51d8d 100644 --- a/custom_components/ankermake/ankermake_mqtt_adapter.py +++ b/custom_components/ankermake/ankermake_mqtt_adapter.py @@ -26,7 +26,7 @@ @dataclass class AnkerData: _timezone: datetime.tzinfo = None - _last_heartbeat: datetime = datetime.now(tz=_timezone) + _last_heartbeat: datetime = None _status: AnkerStatus = AnkerStatus.OFFLINE _old_status: AnkerStatus = None _old_job_name: str = "" @@ -71,6 +71,11 @@ class AnkerData: bed_temp: float = 0 target_bed_temp: float = 0 + def __post_init__(self): + """Initialize the AnkerData object.""" + # Set the last heartbeat to the epoch (so that the printer is considered offline until the first heartbeat) + self._last_heartbeat = datetime(1970, 1, 1, tzinfo=self._timezone) + def _reset(self): """Reset every value except for those with leading underscores to their default value""" [setattr(self, key, getattr(self.__class__, key)) @@ -84,7 +89,8 @@ def _pulse(self): @property def online(self) -> bool: """Returns True if the printer is online.""" - return self.status != AnkerStatus.OFFLINE.value + # TODO: Make this less taxing on the system (checks n(entities) times per update cycle) + return self._last_heartbeat > datetime.now(tz=self._timezone) - timedelta(seconds=30) @property def printing(self) -> bool: @@ -133,7 +139,7 @@ def status(self) -> str: is_heating_hotend = self.target_hotend_temp - 5 > self.hotend_temp > 30 is_heating_bed = self.target_bed_temp - 2 > self.bed_temp > 30 - if self._last_heartbeat < datetime.now(tz=self._timezone) - timedelta(seconds=30): + if not self.online: status = AnkerStatus.OFFLINE elif self.in_error_state: status = AnkerStatus.ERROR