-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathupdater.py
367 lines (320 loc) · 12.5 KB
/
updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""Weather data updater."""
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
import json
import logging
import math
import os
import aiohttp
from homeassistant.components.weather import (
ATTR_FORECAST_CONDITION,
ATTR_FORECAST_IS_DAYTIME,
ATTR_FORECAST_NATIVE_PRECIPITATION,
ATTR_FORECAST_NATIVE_PRESSURE,
ATTR_FORECAST_NATIVE_TEMP,
ATTR_FORECAST_NATIVE_TEMP_LOW,
ATTR_FORECAST_NATIVE_WIND_SPEED,
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
ATTR_FORECAST_TEMP,
ATTR_FORECAST_TEMP_LOW,
ATTR_FORECAST_WIND_BEARING,
Forecast,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.util.dt import get_time_zone
from .const import (
ATTR_API_CONDITION,
ATTR_API_FEELS_LIKE_TEMPERATURE,
ATTR_API_FORECAST_ICONS,
ATTR_API_HUMIDITY,
ATTR_API_IMAGE,
ATTR_API_ORIGINAL_CONDITION,
ATTR_API_PRESSURE,
ATTR_API_PRESSURE_MMHG,
ATTR_API_TEMP_WATER,
ATTR_API_TEMPERATURE,
ATTR_API_WEATHER_TIME,
ATTR_API_WIND_BEARING,
ATTR_API_WIND_GUST,
ATTR_API_WIND_SPEED,
ATTR_API_YA_CONDITION,
ATTR_FORECAST_DATA,
ATTR_MIN_FORECAST_TEMPERATURE,
CONDITION_ICONS,
DOMAIN,
MANUFACTURER,
WEATHER_STATES_CONVERSION,
map_state,
)
API_URL = "https://api.weather.yandex.ru"
API_VERSION = "2"
_LOGGER = logging.getLogger(__name__)
WIND_DIRECTION_MAPPING: dict[str, int | None] = {
"nw": 315,
"n": 360,
"ne": 45,
"e": 90,
"se": 135,
"s": 180,
"sw": 225,
"w": 270,
"c": 0,
}
"""Wind directions mapping."""
@dataclass
class AttributeMapper:
"""Attribute mapper."""
src: str
_dst: str | None = None
mapping: dict | None = None
default: str | float | None = None
should_translate: bool = False
@property
def dst(self) -> str:
"""Destination for mapping."""
return self.src if self._dst is None else self._dst
CURRENT_WEATHER_ATTRIBUTE_TRANSLATION: list[AttributeMapper] = [
AttributeMapper(ATTR_API_WIND_BEARING, mapping=WIND_DIRECTION_MAPPING),
AttributeMapper(ATTR_API_CONDITION, ATTR_API_ORIGINAL_CONDITION),
AttributeMapper(
ATTR_API_CONDITION, f"{ATTR_API_YA_CONDITION}_icon", CONDITION_ICONS
),
AttributeMapper(ATTR_API_CONDITION, ATTR_API_YA_CONDITION, should_translate=True),
AttributeMapper(ATTR_API_CONDITION, mapping=WEATHER_STATES_CONVERSION),
AttributeMapper(ATTR_API_FEELS_LIKE_TEMPERATURE),
AttributeMapper(ATTR_API_HUMIDITY),
AttributeMapper(ATTR_API_IMAGE),
AttributeMapper(ATTR_API_PRESSURE),
AttributeMapper(ATTR_API_PRESSURE_MMHG),
AttributeMapper(ATTR_API_TEMP_WATER),
AttributeMapper(ATTR_API_TEMPERATURE),
AttributeMapper(ATTR_API_WIND_GUST),
AttributeMapper(ATTR_API_WIND_SPEED, default=0),
AttributeMapper("daytime"),
]
FORECAST_ATTRIBUTE_TRANSLATION: list[AttributeMapper] = [
AttributeMapper(
"wind_dir", ATTR_FORECAST_WIND_BEARING, mapping=WIND_DIRECTION_MAPPING
),
AttributeMapper("temp_avg", ATTR_FORECAST_NATIVE_TEMP),
AttributeMapper("temp_avg", ATTR_FORECAST_TEMP),
AttributeMapper("temp_min", ATTR_FORECAST_NATIVE_TEMP_LOW),
AttributeMapper("temp_min", ATTR_FORECAST_TEMP_LOW),
AttributeMapper("pressure_pa", ATTR_FORECAST_NATIVE_PRESSURE),
AttributeMapper("wind_speed", ATTR_FORECAST_NATIVE_WIND_SPEED, default=0),
AttributeMapper("prec_mm", ATTR_FORECAST_NATIVE_PRECIPITATION, default=0),
AttributeMapper("prec_prob", ATTR_FORECAST_PRECIPITATION_PROBABILITY, default=0),
AttributeMapper(
"condition", ATTR_FORECAST_CONDITION, mapping=WEATHER_STATES_CONVERSION
),
]
def translate_condition(value: str, _language: str) -> str:
"""Translate Yandex condition."""
_my_location = os.path.dirname(os.path.realpath(__file__))
_translation_location = f"{_my_location}/translations/{_language.lower()}.json"
try:
with open(_translation_location) as f:
value = json.loads(f.read())["entity"]["sensor"][ATTR_API_YA_CONDITION][
"state"
][value]
except FileNotFoundError:
_LOGGER.debug(f"We have no translation for {_language=} in {_my_location}")
except KeyError:
_LOGGER.debug(f"Have no translation for {value} in {_translation_location}")
return value
class WeatherUpdater(DataUpdateCoordinator):
"""Weather data updater for interaction with Yandex.Weather API."""
def __init__(
self,
latitude: float,
longitude: float,
api_key: str,
hass: HomeAssistant,
device_id: str,
language: str = "EN",
updates_per_day: int = 50,
name="Yandex Weather",
):
"""Initialize updater.
:param latitude: latitude of location for weather data
:param longitude: longitude of location for weather data
:param api_key: Yandex weather API. MUST be weather for site tariff plan
:param hass: Home Assistant object
:param language: Language for yandex_condition
:param updates_per_day: int: how many updates per day we should do?
:param device_id: ID of integration Device in Home Assistant
"""
self.__api_key = api_key
self._lat = latitude
self._lon = longitude
self._device_id = device_id
self._name = name
self._language = language
# Site tariff have 50 free requests per day, but it may be changed
self.update_interval = timedelta(
seconds=math.ceil((24 * 60 * 60) / updates_per_day)
)
if hass is not None:
super().__init__(
hass,
_LOGGER,
name=f"{self._name} updater",
update_interval=self.update_interval,
update_method=self.update,
)
self.data = {}
def process_data(self, dst: dict, src: dict, attributes: list[AttributeMapper]):
"""Convert Yandex API weather state to HA friendly.
:param dst: weather data for HomeAssistant
:param src: weather data form Yandex
:param attributes: how to translate src to dst
"""
for attribute in attributes:
value = src.get(attribute.src, attribute.default)
if attribute.mapping is not None and value is not None:
value = map_state(
src=value,
mapping=attribute.mapping,
is_day=(src["daytime"] == "d"),
)
if attribute.should_translate and value is not None:
value = translate_condition(
value=value,
_language=self._language,
)
dst[attribute.dst] = value
@staticmethod
def get_min_forecast_temperature(forecasts: list[dict]) -> float | None:
"""Get minimum temperature from forecast data."""
low_fc_temperatures: list[float] = []
for f in forecasts:
f_low_temperature: float = f.get(ATTR_FORECAST_NATIVE_TEMP_LOW, None)
if f_low_temperature is not None:
low_fc_temperatures.append(f_low_temperature)
return min(low_fc_temperatures) if len(low_fc_temperatures) > 0 else None
@staticmethod
def get_timezone(nows: str, nowi: int) -> timezone:
"""Get API server timezone based on str and int time values."""
server_utc_time = datetime.strptime(nows, "%Y-%m-%dT%H:%M:%S.%fZ").replace(
microsecond=0
)
server_unix_time = datetime.fromtimestamp(nowi)
return timezone(server_unix_time - server_utc_time)
async def update(self):
"""Update weather information.
:returns: dict with weather data.
"""
result = {}
timeout = aiohttp.ClientTimeout(total=20)
local_tz = get_time_zone(self.hass.config.time_zone)
async with aiohttp.ClientSession(timeout=timeout) as session:
response = await self.request(
session, self.__api_key, self._lat, self._lon, "en_US"
)
r = json.loads(response)
result = {
ATTR_API_WEATHER_TIME: datetime.fromtimestamp(
r["fact"][ATTR_API_WEATHER_TIME],
tz=self.get_timezone(r["now_dt"], r["now"]),
).astimezone(tz=local_tz),
ATTR_API_FORECAST_ICONS: [],
ATTR_FORECAST_DATA: [],
}
self.process_data(result, r["fact"], CURRENT_WEATHER_ATTRIBUTE_TRANSLATION)
f_datetime = datetime.now(tz=local_tz)
for f in r["forecast"]["parts"]:
f_datetime += timedelta(hours=24 / 4)
forecast = Forecast(datetime=f_datetime.isoformat())
self.process_data(forecast, f, FORECAST_ATTRIBUTE_TRANSLATION)
forecast[ATTR_FORECAST_IS_DAYTIME] = f["daytime"] == "d"
result[ATTR_FORECAST_DATA].append(forecast)
result[ATTR_API_FORECAST_ICONS].append(f.get("icon", "no_image"))
result[ATTR_MIN_FORECAST_TEMPERATURE] = self.get_min_forecast_temperature(
result[ATTR_FORECAST_DATA]
)
return result
@staticmethod
async def request(
session: aiohttp.ClientSession,
api_key: str,
lat: float,
lon: float,
lang: str = "en_US",
):
"""
Make request to API endpoint.
:param session: aiohttp.ClientSession: HTTP session for request
:param api_key: str: API key
:param lat: float: latitude of location where we are getting weather data
:param lon: float: longitude of location where we ate getting weather data
:param lang: str: Language for request, defaults to 'en_US'
:returns: dict with response data
:raises AssertionError: when response.status is not 200
"""
headers = {"X-Yandex-API-Key": api_key}
url = f"{API_URL}/v{API_VERSION}/informers?lat={lat}&lon={lon}&lang={lang}"
_LOGGER.info("Sending API request")
async with session.get(url, headers=headers) as response:
try:
assert response.status == 200
_LOGGER.debug(f"{await response.text()}")
except AssertionError as e:
_LOGGER.error(f"Could not get data from API: {response}")
raise aiohttp.ClientError(response.status, await response.text()) from e
return await response.text()
def __str__(self):
"""Show as pretty look data json."""
_d = dict(self.data)
_d[ATTR_API_WEATHER_TIME] = str(_d[ATTR_API_WEATHER_TIME])
return json.dumps(_d, indent=4, sort_keys=True)
@property
def url(self) -> str:
"""Weather URL."""
return f"https://yandex.com/weather/?lat={self._lat}&lon={self._lon}"
@property
def device_info(self):
"""Device info."""
return DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, self.device_id)},
manufacturer=MANUFACTURER,
name=self._name,
configuration_url=self.url,
)
def schedule_refresh(self, offset: timedelta):
"""Schedule refresh."""
if self._unsub_refresh:
self._unsub_refresh()
self._unsub_refresh = None
_LOGGER.debug(f"scheduling next refresh after {offset=}")
next_refresh = (
int(self.hass.loop.time()) + self._microsecond + offset.total_seconds()
)
self._unsub_refresh = self.hass.loop.call_at(
next_refresh, self.__wrap_handle_refresh_interval
).cancel
@callback
def __wrap_handle_refresh_interval(self) -> None:
"""Handle a refresh interval occurrence."""
# We need this private callback from parent class
if self.config_entry:
self.config_entry.async_create_background_task(
self.hass,
self._handle_refresh_interval(),
name=f"{self.name} - {self.config_entry.title} - refresh",
eager_start=True,
)
else:
self.hass.async_create_background_task(
self._handle_refresh_interval(),
name=f"{self.name} - refresh",
eager_start=True,
)
@property
def device_id(self) -> str:
"""Device ID."""
return self._device_id