diff --git a/miio/miioprotocol.py b/miio/miioprotocol.py index dca3f3b0e..6fe8eea09 100644 --- a/miio/miioprotocol.py +++ b/miio/miioprotocol.py @@ -7,7 +7,7 @@ import codecs import logging import socket -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from pprint import pformat as pf from typing import Any, Dict, List, Optional @@ -48,7 +48,7 @@ def __init__( self._discovered = False # these come from the device, but we initialize them here to make mypy happy - self._device_ts: datetime = datetime.utcnow() + self._device_ts: datetime = datetime.now(tz=timezone.utc) self._device_id = b"" def send_handshake(self, *, retry_count=3) -> Message: diff --git a/miio/miot_cloud.py b/miio/miot_cloud.py index 3076326b6..a596b3e10 100644 --- a/miio/miot_cloud.py +++ b/miio/miot_cloud.py @@ -1,7 +1,7 @@ """Module implementing handling of miot schema files.""" import json import logging -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from operator import attrgetter from pathlib import Path from typing import Dict, List, Optional @@ -114,10 +114,9 @@ def _write_to_cache(self, file: Path, data: Dict): def _file_from_cache(self, file, cache_hours=6) -> Dict: def _valid_cache(): expiration = timedelta(hours=cache_hours) - if ( - datetime.fromtimestamp(file.stat().st_mtime) + expiration - > datetime.utcnow() - ): + if datetime.fromtimestamp( + file.stat().st_mtime, tz=timezone.utc + ) + expiration > datetime.now(tz=timezone.utc): return True return False diff --git a/miio/protocol.py b/miio/protocol.py index c680c0308..3902d35b6 100644 --- a/miio/protocol.py +++ b/miio/protocol.py @@ -143,7 +143,7 @@ def _encode(self, obj, context, path): return calendar.timegm(obj.timetuple()) def _decode(self, obj, context, path): - return datetime.datetime.utcfromtimestamp(obj) + return datetime.datetime.fromtimestamp(obj, tz=datetime.timezone.utc) class EncryptionAdapter(Adapter): @@ -214,7 +214,13 @@ def _decode(self, obj, context, path) -> Union[Dict, bytes]: "length" / Rebuild(Int16ub, Utils.get_length), "unknown" / Default(Int32ub, 0x00000000), "device_id" / Hex(Bytes(4)), - "ts" / TimeAdapter(Default(Int32ub, datetime.datetime.utcnow())), + "ts" + / TimeAdapter( + Default( + Int32ub, + datetime.datetime.now(tz=datetime.timezone.utc), + ) + ), ) ), "checksum"