-
Notifications
You must be signed in to change notification settings - Fork 5
/
sensor.py
296 lines (236 loc) · 9.69 KB
/
sensor.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
import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from operator import itemgetter
from ukhotides import (
UkhoTides,
ApiError,
InvalidApiKeyError,
TidalEvent,
)
from datetime import datetime, timedelta, timezone
from async_timeout import timeout
from aiohttp.client_exceptions import ClientConnectorError
from typing import Any, Callable, Dict, Optional, Tuple, List
from homeassistant import config_entries, core
from homeassistant.const import ATTR_ATTRIBUTION, CONF_API_KEY
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.helpers.typing import (
ConfigType,
DiscoveryInfoType,
HomeAssistantType,
)
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import (
DOMAIN,
ATTRIBUTION,
ATTR_ICON_RISING,
ATTR_ICON_FALLING,
CONF_API_LEVEL,
CONF_STATIONS,
CONF_STATION_ID,
CONF_STATION_NAME,
CONF_STATION_OFFSET_HIGH,
CONF_STATION_OFFSET_LOW,
)
_LOGGER = logging.getLogger(__name__)
TIDE_STATION_SCHEMA = vol.Schema(
{
vol.Required(CONF_STATION_ID): cv.string,
vol.Optional(CONF_STATION_NAME): cv.string,
vol.Optional(CONF_STATION_OFFSET_HIGH): int,
vol.Optional(CONF_STATION_OFFSET_LOW): int,
}
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_API_KEY): cv.string,
vol.Optional(CONF_API_LEVEL): cv.string,
vol.Required(CONF_STATIONS): vol.All(cv.ensure_list, [TIDE_STATION_SCHEMA]),
}
)
async def async_setup_platform(
hass: HomeAssistantType,
config: ConfigType,
async_add_entities: Callable,
discovery_info: Optional[DiscoveryInfoType] = None,
) -> None:
session = async_get_clientsession(hass)
ukhotides = UkhoTides(session, config[CONF_API_KEY])
sensors = []
for station in config[CONF_STATIONS]:
coordinator = UkhoTidesDataUpdateCoordinator(hass, ukhotides, station)
if CONF_STATION_NAME in coordinator.station:
name = station[CONF_STATION_NAME]
else:
name = (
await ukhotides.async_get_station(coordinator.station[CONF_STATION_ID])
).name
sensors.append(UkhoTidesSensor(coordinator, name))
async_add_entities(sensors, update_before_add=True)
async def async_setup_entry(
hass: core.HomeAssistant, entry: config_entries.ConfigEntry, async_add_entities
):
config = hass.data[DOMAIN][entry.entry_id]
if entry.options:
config.update(entry.options)
session = async_get_clientsession(hass)
ukhotides = UkhoTides(session, config[CONF_API_KEY])
sensors = []
for station in config[CONF_STATIONS]:
coordinator = UkhoTidesDataUpdateCoordinator(hass, ukhotides, station)
if CONF_STATION_NAME in coordinator.station:
name = station[CONF_STATION_NAME]
else:
name = (
await ukhotides.async_get_station(coordinator.station[CONF_STATION_ID])
).name
sensors.append(UkhoTidesSensor(coordinator, name))
async_add_entities(sensors, update_before_add=True)
class UkhoTidesSensor(CoordinatorEntity):
def __init__(self, coordinator, name):
super().__init__(coordinator)
self._name = name + " Tide"
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
@property
def name(self):
if self._name:
return self._name
return "Unknown"
@property
def unique_id(self):
return self.coordinator.station[CONF_STATION_ID]
@property
def state(self):
if self.coordinator.data is None:
return None
next_predictions = self.get_next_predictions()
if next_predictions[0]["tidal_event"].event_type == "HighWater":
return "Rising"
else:
return "Falling"
@property
def icon(self):
if self.coordinator.data is None:
return None
next_predictions = self.get_next_predictions()
if next_predictions[0]["tidal_event"].event_type == "HighWater":
return ATTR_ICON_RISING
else:
return ATTR_ICON_FALLING
@property
def extra_state_attributes(self):
next_predictions = self.get_next_predictions()
if next_predictions is None:
return None
self._attrs[f"predictions"] = []
for p in self.coordinator.data:
self._attrs[f"predictions"].append(
[
p["tidal_event_datetime"].strftime("%Y-%m-%d %H:%M:%S"),
round(p["tidal_event"].height, 1),
]
)
now = datetime.utcnow().replace(tzinfo=timezone.utc)
for i in range(2):
time_to_next_tide = next_predictions[i]["tidal_event_datetime"] - now
next_height = round(next_predictions[i]["tidal_event"].height, 1)
hours, rem = divmod(time_to_next_tide.seconds, 3600)
minutes, seconds = divmod(rem, 60)
if next_predictions[i]["tidal_event"].event_type == "HighWater":
self._attrs[f"next_high_tide_in"] = f"{hours}h {minutes}m"
self._attrs[f"next_high_tide_at"] = next_predictions[i][
"tidal_event_datetime"
].astimezone()
self._attrs[f"next_high_tide_height"] = f"{next_height}m"
else:
self._attrs[f"next_low_tide_in"] = f"{hours}h {minutes}m"
self._attrs[f"next_low_tide_at"] = next_predictions[i][
"tidal_event_datetime"
].astimezone()
self._attrs[f"next_low_tide_height"] = f"{next_height}m"
return self._attrs
def get_next_predictions(self) -> [{datetime, TidalEvent}]:
now = datetime.utcnow().replace(tzinfo=timezone.utc)
next_predictions = []
for tidal_event in self.coordinator.data:
if tidal_event["tidal_event_datetime"] > now:
next_predictions.append(tidal_event)
return next_predictions
class UkhoTidesDataUpdateCoordinator(DataUpdateCoordinator):
def __init__(self, hass, ukhotides, station):
self._ukhotides = ukhotides
self._download_interval = timedelta(minutes=60)
self._last_download_datetime = None
self._data = []
self.station = station
update_interval = timedelta(minutes=1)
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=update_interval)
async def _async_update_data(self) -> List[TidalEvent]:
now = datetime.utcnow().replace(tzinfo=timezone.utc)
# As predictions rarely change, only refresh from the API infrequently
if (
self._last_download_datetime is None
or not self._data
or self._last_download_datetime < now - self._download_interval
):
_LOGGER.debug("Re-downloading tide data")
try:
async with timeout(10):
tidal_events = await self._ukhotides.async_get_tidal_events(
self.station[CONF_STATION_ID]
)
for tidal_event in tidal_events:
tidal_event_datetime = datetime.strptime(
# Get just the stuff before any potential milliseconds
tidal_event.date_time.split(".")[0],
"%Y-%m-%dT%H:%M:%S",
)
# Convert to UTC
tidal_event_datetime = tidal_event_datetime.replace(tzinfo=timezone.utc)
# Add any offsets
if (
tidal_event.event_type == "HighWater"
and CONF_STATION_OFFSET_HIGH in self.station
):
tidal_event_datetime = tidal_event_datetime + timedelta(
minutes=self.station[CONF_STATION_OFFSET_HIGH]
)
if (
tidal_event.event_type == "LowWater"
and CONF_STATION_OFFSET_LOW in self.station
):
tidal_event_datetime = tidal_event_datetime + timedelta(
minutes=self.station[CONF_STATION_OFFSET_LOW]
)
self._data.append(
{
"tidal_event_datetime": tidal_event_datetime,
"tidal_event": tidal_event,
}
)
# Stack Overflow voodoo (comprehension) to get distinct events
self._data = [
i
for n, i in enumerate(self._data)
if i not in self._data[n + 1 :]
]
self._data.sort(key=itemgetter("tidal_event_datetime"))
i = 0
for tidal_event in self._data:
if tidal_event["tidal_event_datetime"] > now:
if i > 1:
self._data = self._data[i - 2 :]
break
i += 1
except (
ApiError,
ClientConnectorError,
InvalidApiKeyError,
# TODO: Other errors
) as error:
raise UpdateFailed(error) from error
self._last_download_datetime = now
return self._data