Skip to content

Commit

Permalink
use ciso8601 for datetime parsing (#34)
Browse files Browse the repository at this point in the history
* use ciso8601 for datetime parsing

* loosen
  • Loading branch information
tkdrob authored Sep 7, 2023
1 parent e97ae0e commit 0caf6c9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
12 changes: 4 additions & 8 deletions aioskybell/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import TYPE_CHECKING, Any, cast

import aiofiles
from ciso8601 import parse_datetime # pylint:disable=no-name-in-module

from . import utils as UTILS
from .exceptions import SkybellAuthenticationException, SkybellException
Expand Down Expand Up @@ -55,7 +56,7 @@ async def _async_avatar_request(self) -> AvatarDict:
async def _async_info_request(self) -> InfoDict:
url = str.replace(CONST.DEVICE_INFO_URL, "$DEVID$", self.device_id)
if data := await self._skybell.async_send_request(url):
data[CONST.CHECK_IN] = convert_date(data.get(CONST.CHECK_IN, ""))
data[CONST.CHECK_IN] = parse_datetime(data.get(CONST.CHECK_IN, ""))
return data

async def _async_settings_request(
Expand Down Expand Up @@ -155,13 +156,13 @@ def latest(self, event: str | None = None) -> EventDict:
if (_evt := cast(EventDict, events.get(f"device:sensor:{event}"))) is None:
_default = {CONST.CREATED_AT: "1970-01-01T00:00:00.000Z"}
_evt = events.get(f"application:on-{event}", _default)
_entry = {CONST.CREATED_AT: convert_date(_evt[CONST.CREATED_AT])}
_entry = {CONST.CREATED_AT: parse_datetime(_evt[CONST.CREATED_AT])}
return cast(EventDict, _evt | _entry)

latest: EventDict = EventDict()
latest_date = None
for evt in events.values():
date = convert_date(evt[CONST.CREATED_AT])
date = parse_datetime(evt[CONST.CREATED_AT])
if len(latest) == 0 or latest_date is None or latest_date < date:
latest = evt
latest_date = date
Expand Down Expand Up @@ -427,8 +428,3 @@ def _validate_setting( # pylint:disable=too-many-branches
if setting == CONST.BRIGHTNESS:
if not CONST.BRIGHTNESS_VALUES[0] <= int(value) <= CONST.BRIGHTNESS_VALUES[1]:
raise SkybellException(ERROR.INVALID_SETTING_VALUE, (setting, value))


def convert_date(string: str) -> datetime:
"""Convert string to datetime."""
return datetime.strptime(f"{string}+00:00", "%Y-%m-%dT%H:%M:%S.%fZ%z")
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ aiofiles>=0.3.0
aiohttp>=3.6.1,<4.0
aresponses>=2.1.4
black>=21.11b1
ciso8601>=1.0.1
isort>=5.10.1
flake8>=4.0.1
flake8-docstrings>=1.6.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
url="https://github.com/tkdrob/aioskybell",
package_data={"aioskybell": ["py.typed"]},
packages=find_packages(include=["aioskybell", "aioskybell*"]),
install_requires=["aiohttp>=3.6.1,<4.0", "aiofiles>=0.3.0"],
install_requires=["aiohttp>=3.6.1,<4.0", "aiofiles>=0.3.0", "ciso8601>=1.0.1"],
keywords=["aioskybell", "skybell"],
license="MIT license",
classifiers=[
Expand Down

0 comments on commit 0caf6c9

Please sign in to comment.