Skip to content

Commit

Permalink
Merge pull request #298 from Snuffy2/Handle-Time-Zone-errors
Browse files Browse the repository at this point in the history
Change from tzinfo to utcoffset
  • Loading branch information
Snuffy2 authored Oct 26, 2024
2 parents 82482ad + 842d597 commit ca8b60c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
12 changes: 4 additions & 8 deletions custom_components/opnsense/device_tracker.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
"""Support for tracking for OPNsense devices."""

import logging
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Any, Mapping

from homeassistant.components.device_tracker import SourceType
from homeassistant.components.device_tracker.config_entry import ScannerEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_platform
from homeassistant.helpers.device_registry import (
CONNECTION_NETWORK_MAC,
)
from homeassistant.helpers.device_registry import (
async_get as async_get_dev_reg,
)
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.device_registry import async_get as async_get_dev_reg
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.restore_state import RestoreEntity

Expand Down Expand Up @@ -242,7 +238,7 @@ def _handle_coordinator_update(self) -> None:
if isinstance(update_time, float):
self._last_known_connected_time = datetime.fromtimestamp(
int(update_time),
tz=datetime.now().astimezone().tzinfo,
tz=timezone(datetime.now().astimezone().utcoffset()),
)
self._is_connected = True

Expand Down
24 changes: 16 additions & 8 deletions custom_components/opnsense/pyopnsense/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import xmlrpc.client
from abc import ABC
from collections.abc import Mapping
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Any
from urllib.parse import quote_plus, urlparse

Expand Down Expand Up @@ -466,7 +466,7 @@ async def get_firmware_update_info(self):
last_check: datetime = parse(status.get("last_check"))
if last_check.tzinfo is None:
last_check = last_check.replace(
tzinfo=datetime.now().astimezone().tzinfo
tzinfo=timezone(datetime.now().astimezone().utcoffset())
)

last_check_timestamp: float = last_check.timestamp()
Expand Down Expand Up @@ -810,7 +810,7 @@ async def _get_kea_dhcpv4_leases(self) -> list:
if self._try_to_int(lease_info.get("expire", None)):
lease["expires"] = datetime.fromtimestamp(
self._try_to_int(lease_info.get("expire", None)),
tz=datetime.now().astimezone().tzinfo,
tz=timezone(datetime.now().astimezone().utcoffset()),
)
if lease["expires"] < datetime.now().astimezone():
continue
Expand Down Expand Up @@ -874,7 +874,9 @@ async def _get_isc_dhcpv4_leases(self) -> list:
dt: datetime = datetime.strptime(
lease_info.get("ends", None), "%Y/%m/%d %H:%M:%S"
)
lease["expires"] = dt.replace(tzinfo=datetime.now().astimezone().tzinfo)
lease["expires"] = dt.replace(
tzinfo=timezone(datetime.now().astimezone().utcoffset())
)
if lease["expires"] < datetime.now().astimezone():
continue
else:
Expand Down Expand Up @@ -937,7 +939,9 @@ async def _get_isc_dhcpv6_leases(self) -> list:
dt: datetime = datetime.strptime(
lease_info.get("ends", None), "%Y/%m/%d %H:%M:%S"
)
lease["expires"] = dt.replace(tzinfo=datetime.now().astimezone().tzinfo)
lease["expires"] = dt.replace(
tzinfo=timezone(datetime.now().astimezone().utcoffset())
)
if lease["expires"] < datetime.now().astimezone():
continue
else:
Expand Down Expand Up @@ -1437,7 +1441,7 @@ async def get_openvpn(self) -> Mapping[str, Any]:
{
"latest_handshake": datetime.fromtimestamp(
int(session.get("connected_since__time_t_")),
tz=datetime.now().astimezone().tzinfo,
tz=timezone(datetime.now().astimezone().utcoffset()),
)
}
)
Expand Down Expand Up @@ -1936,7 +1940,9 @@ async def get_wireguard(self) -> Mapping[str, Any]:
if entry.get("latest-handshake", None):
srv["latest_handshake"] = datetime.fromtimestamp(
int(entry.get("latest-handshake")),
tz=datetime.now().astimezone().tzinfo,
tz=timezone(
datetime.now().astimezone().utcoffset()
),
)
srv["connected"] = wireguard_is_connected(
srv.get("latest_handshake")
Expand Down Expand Up @@ -1984,7 +1990,9 @@ async def get_wireguard(self) -> Mapping[str, Any]:
if entry.get("latest-handshake", None):
clnt["latest_handshake"] = datetime.fromtimestamp(
int(entry.get("latest-handshake")),
tz=datetime.now().astimezone().tzinfo,
tz=timezone(
datetime.now().astimezone().utcoffset()
),
)
clnt["connected"] = wireguard_is_connected(
clnt.get("latest_handshake")
Expand Down

0 comments on commit ca8b60c

Please sign in to comment.