From 842d597407165d93dd58cd2260ca79185b6116f6 Mon Sep 17 00:00:00 2001 From: Snuffy2 Date: Fri, 25 Oct 2024 22:57:54 -0400 Subject: [PATCH] Change from tzinfo to utcoffset --- custom_components/opnsense/device_tracker.py | 12 ++++------ .../opnsense/pyopnsense/__init__.py | 24 ++++++++++++------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/custom_components/opnsense/device_tracker.py b/custom_components/opnsense/device_tracker.py index e4e0886..372004a 100644 --- a/custom_components/opnsense/device_tracker.py +++ b/custom_components/opnsense/device_tracker.py @@ -1,7 +1,7 @@ """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 @@ -9,12 +9,8 @@ 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 @@ -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 diff --git a/custom_components/opnsense/pyopnsense/__init__.py b/custom_components/opnsense/pyopnsense/__init__.py index dfd17c5..7543c1e 100644 --- a/custom_components/opnsense/pyopnsense/__init__.py +++ b/custom_components/opnsense/pyopnsense/__init__.py @@ -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 @@ -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() @@ -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 @@ -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: @@ -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: @@ -1425,7 +1429,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()), ) } ) @@ -1893,7 +1897,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") @@ -1941,7 +1947,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")