Skip to content

Commit

Permalink
Small rework of online/heartbeat property
Browse files Browse the repository at this point in the history
  • Loading branch information
sondregronas committed Jul 7, 2024
1 parent 58d905c commit 3ea1266
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 9 additions & 3 deletions custom_components/ankermake/ankermake_mqtt_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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))
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3ea1266

Please sign in to comment.