Skip to content

Commit aa3522b

Browse files
authored
Get ZoneInfo asynchronously using aiozoneinfo (#214)
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.
1 parent 47d8da0 commit aa3522b

File tree

6 files changed

+50
-34
lines changed

6 files changed

+50
-34
lines changed

poetry.lock

Lines changed: 27 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ example = "examples.energy:start"
2727

2828
[tool.poetry.dependencies]
2929
aiohttp = ">=3.0.0"
30+
aiozoneinfo = "^0.2.0"
3031
async-timeout = "^4.0.3"
3132
python = "^3.10"
3233
yarl = ">=1.6.0"

spothinta_api/const.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"""Constants for SpotHinta API client."""
2+
23
from __future__ import annotations
34

45
from enum import Enum, unique
56
from typing import Final
67

7-
from zoneinfo import ZoneInfo
8-
98
API_HOST: Final = "api.spot-hinta.fi"
109

1110

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

3231

33-
REGION_TO_TIMEZONE: dict[Region, ZoneInfo] = {
34-
Region.DK1: ZoneInfo("Europe/Copenhagen"),
35-
Region.DK2: ZoneInfo("Europe/Copenhagen"),
36-
Region.FI: ZoneInfo("Europe/Helsinki"),
37-
Region.EE: ZoneInfo("Europe/Tallinn"),
38-
Region.LT: ZoneInfo("Europe/Vilnius"),
39-
Region.LV: ZoneInfo("Europe/Riga"),
40-
Region.NO1: ZoneInfo("Europe/Oslo"),
41-
Region.NO2: ZoneInfo("Europe/Oslo"),
42-
Region.NO3: ZoneInfo("Europe/Oslo"),
43-
Region.NO4: ZoneInfo("Europe/Oslo"),
44-
Region.NO5: ZoneInfo("Europe/Oslo"),
45-
Region.SE1: ZoneInfo("Europe/Stockholm"),
46-
Region.SE2: ZoneInfo("Europe/Stockholm"),
47-
Region.SE3: ZoneInfo("Europe/Stockholm"),
48-
Region.SE4: ZoneInfo("Europe/Stockholm"),
32+
REGION_TO_TIMEZONE: dict[Region, str] = {
33+
Region.DK1: "Europe/Copenhagen",
34+
Region.DK2: "Europe/Copenhagen",
35+
Region.FI: "Europe/Helsinki",
36+
Region.EE: "Europe/Tallinn",
37+
Region.LT: "Europe/Vilnius",
38+
Region.LV: "Europe/Riga",
39+
Region.NO1: "Europe/Oslo",
40+
Region.NO2: "Europe/Oslo",
41+
Region.NO3: "Europe/Oslo",
42+
Region.NO4: "Europe/Oslo",
43+
Region.NO5: "Europe/Oslo",
44+
Region.SE1: "Europe/Stockholm",
45+
Region.SE2: "Europe/Stockholm",
46+
Region.SE3: "Europe/Stockholm",
47+
Region.SE4: "Europe/Stockholm",
4948
}

spothinta_api/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Data models for the spot-hinta.fi API."""
2+
23
from __future__ import annotations
34

45
from dataclasses import dataclass

spothinta_api/spothinta.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Asynchronous Python client for the spot-hinta.fi API."""
2+
23
from __future__ import annotations
34

45
import asyncio
@@ -11,6 +12,7 @@
1112
from aiohttp import ClientResponseError
1213
from aiohttp.client import ClientError, ClientSession
1314
from aiohttp.hdrs import METH_GET
15+
from aiozoneinfo import async_get_time_zone
1416
from yarl import URL
1517

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

146-
time_zone = REGION_TO_TIMEZONE[region]
148+
time_zone = await async_get_time_zone(REGION_TO_TIMEZONE[region])
147149
return Electricity.from_dict(data, time_zone=time_zone)
148150

149151
async def close(self) -> None:

tests/test_regions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""Tests for regions."""
2-
from zoneinfo import ZoneInfo
32

43
from spothinta_api.const import REGION_TO_TIMEZONE, Region
54

65

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

0 commit comments

Comments
 (0)