This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
84 lines (58 loc) · 2.38 KB
/
__init__.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
"""The MySkoda Enyaq integration."""
from __future__ import annotations
from datetime import timedelta
import logging
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DATA_COODINATOR, DOMAIN
from .myskoda import MySkodaHub, Vehicle
_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [
Platform.SENSOR,
Platform.DEVICE_TRACKER,
Platform.CLIMATE,
Platform.SWITCH,
Platform.NUMBER,
Platform.BINARY_SENSOR,
]
async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
"""Set up Enyaq integration from a config entry."""
coordinator = MySkodaDataUpdateCoordinator(hass, config)
if not await coordinator.async_login():
return False
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][config.entry_id] = {DATA_COODINATOR: coordinator}
await hass.config_entries.async_forward_entry_setups(config, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
class MySkodaDataUpdateCoordinator(DataUpdateCoordinator):
"""See `DataUpdateCoordinator`.
This class manages all data from the MySkoda API.
"""
def __init__(self, hass: HomeAssistant, config: ConfigEntry) -> None:
"""Create a new coordinator."""
super().__init__(
hass, _LOGGER, name=DOMAIN, update_interval=timedelta(minutes=5)
)
self.hub = MySkodaHub(async_get_clientsession(hass))
self.config = config
async def async_login(self) -> bool:
"""Login to the MySkoda API. Will return `True` if successful."""
return await self.hub.authenticate(
self.config.data["email"], self.config.data["password"]
)
async def _async_update_data(self) -> dict[str, list[Vehicle]]:
return {
"vehicles": await self.hub.get_all_vehicles(),
}
def _unsub_refresh(self):
return