mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-06-28 19:50:06 +02:00
Convert uptime sensor to new timestamp standard Home Assistant format
Fixes #118
This commit is contained in:
parent
c7c6da7512
commit
d6023abc30
2 changed files with 46 additions and 10 deletions
|
@ -1,11 +1,12 @@
|
|||
"""Mikrotik Controller for Mikrotik Router."""
|
||||
|
||||
import re
|
||||
import asyncio
|
||||
import logging
|
||||
import ipaddress
|
||||
import logging
|
||||
import re
|
||||
import pytz
|
||||
|
||||
from datetime import timedelta
|
||||
from datetime import datetime, timedelta
|
||||
from ipaddress import ip_address, IPv4Network
|
||||
from mac_vendor_lookup import AsyncMacLookup
|
||||
|
||||
|
@ -61,6 +62,8 @@ from .mikrotikapi import MikrotikAPI
|
|||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_TIME_ZONE = None
|
||||
|
||||
|
||||
def is_valid_ip(address):
|
||||
try:
|
||||
|
@ -70,6 +73,21 @@ def is_valid_ip(address):
|
|||
return False
|
||||
|
||||
|
||||
def utc_from_timestamp(timestamp: float) -> datetime:
|
||||
"""Return a UTC time from a timestamp."""
|
||||
return pytz.utc.localize(datetime.utcfromtimestamp(timestamp))
|
||||
|
||||
|
||||
def as_local(dattim: datetime) -> datetime:
|
||||
"""Convert a UTC datetime object to local time zone."""
|
||||
if dattim.tzinfo == DEFAULT_TIME_ZONE:
|
||||
return dattim
|
||||
if dattim.tzinfo is None:
|
||||
dattim = pytz.utc.localize(dattim)
|
||||
|
||||
return dattim.astimezone(DEFAULT_TIME_ZONE)
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# MikrotikControllerData
|
||||
# ---------------------------
|
||||
|
@ -1215,17 +1233,34 @@ class MikrotikControllerData:
|
|||
)
|
||||
|
||||
tmp_uptime = 0
|
||||
tmp = re.split(r"(\d+)[h]", self.data["resource"]["uptime_str"])
|
||||
tmp = re.split(r"(\d+)[s]", self.data["resource"]["uptime_str"])
|
||||
if len(tmp) > 1:
|
||||
tmp_uptime += int(tmp[1])
|
||||
tmp = re.split(r"(\d+)[m]", self.data["resource"]["uptime_str"])
|
||||
if len(tmp) > 1:
|
||||
tmp_uptime += int(tmp[1]) * 60
|
||||
tmp = re.split(r"(\d+)[h]", self.data["resource"]["uptime_str"])
|
||||
if len(tmp) > 1:
|
||||
tmp_uptime += int(tmp[1]) * 3600
|
||||
tmp = re.split(r"(\d+)[d]", self.data["resource"]["uptime_str"])
|
||||
if len(tmp) > 1:
|
||||
tmp_uptime += int(tmp[1]) * 24
|
||||
tmp_uptime += int(tmp[1]) * 86400
|
||||
tmp = re.split(r"(\d+)[w]", self.data["resource"]["uptime_str"])
|
||||
if len(tmp) > 1:
|
||||
tmp_uptime += int(tmp[1]) * 24 * 7
|
||||
tmp_uptime += int(tmp[1]) * 604800
|
||||
|
||||
self.data["resource"]["uptime"] = tmp_uptime
|
||||
now = datetime.now().replace(microsecond=0)
|
||||
uptime_tm = datetime.timestamp(now - timedelta(seconds=tmp_uptime))
|
||||
update_uptime = False;
|
||||
if self.data["resource"]["uptime"] is None or self.data["resource"]["uptime"] == 0:
|
||||
update_uptime = True;
|
||||
else:
|
||||
uptime_old = datetime.timestamp(datetime.fromisoformat(self.data["resource"]["uptime"]))
|
||||
if uptime_tm > uptime_old:
|
||||
update_uptime = True;
|
||||
|
||||
if update_uptime:
|
||||
self.data["resource"]["uptime"] = str(as_local(utc_from_timestamp(uptime_tm)).isoformat())
|
||||
|
||||
if self.data["resource"]["total-memory"] > 0:
|
||||
self.data["resource"]["memory-usage"] = round(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue