Skip to content

Commit

Permalink
Get ZoneInfo asynchronously using aiozoneinfo (#214)
Browse files Browse the repository at this point in the history
Home Assistant is getting more picky about blocking operations done in the
event loop, so we need to make sure that reading timezone information from disk
is done asynchronously here.
  • Loading branch information
slovdahl authored Jun 18, 2024
1 parent 47d8da0 commit aa3522b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 34 deletions.
40 changes: 27 additions & 13 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ example = "examples.energy:start"

[tool.poetry.dependencies]
aiohttp = ">=3.0.0"
aiozoneinfo = "^0.2.0"
async-timeout = "^4.0.3"
python = "^3.10"
yarl = ">=1.6.0"
Expand Down
35 changes: 17 additions & 18 deletions spothinta_api/const.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Constants for SpotHinta API client."""

from __future__ import annotations

from enum import Enum, unique
from typing import Final

from zoneinfo import ZoneInfo

API_HOST: Final = "api.spot-hinta.fi"


Expand All @@ -30,20 +29,20 @@ class Region(Enum):
SE4 = 14


REGION_TO_TIMEZONE: dict[Region, ZoneInfo] = {
Region.DK1: ZoneInfo("Europe/Copenhagen"),
Region.DK2: ZoneInfo("Europe/Copenhagen"),
Region.FI: ZoneInfo("Europe/Helsinki"),
Region.EE: ZoneInfo("Europe/Tallinn"),
Region.LT: ZoneInfo("Europe/Vilnius"),
Region.LV: ZoneInfo("Europe/Riga"),
Region.NO1: ZoneInfo("Europe/Oslo"),
Region.NO2: ZoneInfo("Europe/Oslo"),
Region.NO3: ZoneInfo("Europe/Oslo"),
Region.NO4: ZoneInfo("Europe/Oslo"),
Region.NO5: ZoneInfo("Europe/Oslo"),
Region.SE1: ZoneInfo("Europe/Stockholm"),
Region.SE2: ZoneInfo("Europe/Stockholm"),
Region.SE3: ZoneInfo("Europe/Stockholm"),
Region.SE4: ZoneInfo("Europe/Stockholm"),
REGION_TO_TIMEZONE: dict[Region, str] = {
Region.DK1: "Europe/Copenhagen",
Region.DK2: "Europe/Copenhagen",
Region.FI: "Europe/Helsinki",
Region.EE: "Europe/Tallinn",
Region.LT: "Europe/Vilnius",
Region.LV: "Europe/Riga",
Region.NO1: "Europe/Oslo",
Region.NO2: "Europe/Oslo",
Region.NO3: "Europe/Oslo",
Region.NO4: "Europe/Oslo",
Region.NO5: "Europe/Oslo",
Region.SE1: "Europe/Stockholm",
Region.SE2: "Europe/Stockholm",
Region.SE3: "Europe/Stockholm",
Region.SE4: "Europe/Stockholm",
}
1 change: 1 addition & 0 deletions spothinta_api/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Data models for the spot-hinta.fi API."""

from __future__ import annotations

from dataclasses import dataclass
Expand Down
4 changes: 3 additions & 1 deletion spothinta_api/spothinta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Asynchronous Python client for the spot-hinta.fi API."""

from __future__ import annotations

import asyncio
Expand All @@ -11,6 +12,7 @@
from aiohttp import ClientResponseError
from aiohttp.client import ClientError, ClientSession
from aiohttp.hdrs import METH_GET
from aiozoneinfo import async_get_time_zone
from yarl import URL

from .const import API_HOST, REGION_TO_TIMEZONE, Region
Expand Down Expand Up @@ -143,7 +145,7 @@ async def energy_prices(self, region: Region = Region.FI) -> Electricity:
msg = "No energy prices found."
raise SpotHintaNoDataError(msg)

time_zone = REGION_TO_TIMEZONE[region]
time_zone = await async_get_time_zone(REGION_TO_TIMEZONE[region])
return Electricity.from_dict(data, time_zone=time_zone)

async def close(self) -> None:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_regions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Tests for regions."""
from zoneinfo import ZoneInfo

from spothinta_api.const import REGION_TO_TIMEZONE, Region


async def test_each_region_has_a_time_zone() -> None:
"""Tests that each region has a time zone set."""
for region in Region:
assert isinstance(REGION_TO_TIMEZONE[region], ZoneInfo)
assert isinstance(REGION_TO_TIMEZONE[region], str)

0 comments on commit aa3522b

Please sign in to comment.