Skip to content

Commit

Permalink
Merge pull request #21 from bieniu/fix-uptime
Browse files Browse the repository at this point in the history
Fix uptime sensor
  • Loading branch information
bieniu authored Nov 25, 2020
2 parents f0f0888 + f416420 commit 385b7fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
21 changes: 19 additions & 2 deletions brother/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Python wrapper for getting data from Brother laser and inkjet printers via SNMP. Uses
the method of parsing data from: https://github.com/saper-2/BRN-Printer-sCounters-Info
"""
import datetime
import logging
import re

Expand Down Expand Up @@ -56,13 +57,16 @@ def __init__(self, host, port=161, kind="laser"):
self.serial = None
self._host = host
self._port = port
self._last_uptime = None

self._snmp_engine = None
self._oids = tuple(self._iterate_oids(OIDS.values()))

_LOGGER.debug("Using host: %s", host)

async def async_update(self): # pylint:disable=too-many-branches
async def async_update(
self,
): # pylint:disable=too-many-branches,too-many-statements
"""Update data from printer."""
raw_data = await self._get_data()

Expand Down Expand Up @@ -105,9 +109,22 @@ async def async_update(self): # pylint:disable=too-many-branches
except (AttributeError, KeyError, TypeError):
_LOGGER.debug("Incomplete data from printer.")
try:
data[ATTR_UPTIME] = int(raw_data.get(OIDS[ATTR_UPTIME])) / 100
uptime = int(raw_data.get(OIDS[ATTR_UPTIME])) / 100
except TypeError:
pass
else:
if not self._last_uptime:
data[ATTR_UPTIME] = self._last_uptime = (
datetime.datetime.utcnow() - datetime.timedelta(seconds=uptime)
).replace(microsecond=0)
else:
new_uptime = (
datetime.datetime.utcnow() - datetime.timedelta(seconds=uptime)
).replace(microsecond=0)
if abs((new_uptime - self._last_uptime).total_seconds()) > 5:
data[ATTR_UPTIME] = self._last_uptime = new_uptime
else:
data[ATTR_UPTIME] = self._last_uptime
if self._legacy:
if self._kind == "laser":
data.update(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="brother",
version="0.1.18",
version="0.1.19",
author="Maciej Bieniek",
author_email="[email protected]",
description="Python wrapper for getting data from Brother laser and inkjet \
Expand Down

0 comments on commit 385b7fe

Please sign in to comment.