-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
111 lines (81 loc) · 3.46 KB
/
__init__.py
File metadata and controls
111 lines (81 loc) · 3.46 KB
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
"""Home Assistant DVS Portal Integration.
This integration allows Home Assistant to interact with the DVS Portal API,
retrieving data such as parking balance, and managing car reservations.
"""
import asyncio
from dataclasses import dataclass
import logging
from dvsportal import DVSPortal, exceptions as dvs_exceptions
from homeassistant import exceptions
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import DOMAIN
from .coordinator import DVSPortalCoordinator
from .services import async_register_services
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
@dataclass
class DVSPortalRuntimeData:
"""Data class for runtime data of DVSPortal."""
coordinator: DVSPortalCoordinator
ha_registered_license_plates: set[
str
] # Store all license places which have already have a DVSCarPortal instance
type DVSPortalConfigEntry = ConfigEntry[DVSPortalRuntimeData]
async def async_setup(hass: HomeAssistant, config: dict) -> bool:
"""Set up integration-wide things."""
await async_register_services(hass)
return True
async def async_setup_entry(hass: HomeAssistant, entry: DVSPortalConfigEntry) -> bool:
"""Set up the dvsportal component from a config entry."""
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
runtime_data: DVSPortalRuntimeData = entry.runtime_data
# Ensure dvs_portal.close() is called to clean up the session
if dvs_portal := runtime_data.coordinator.dvs_portal:
try:
await dvs_portal.close()
except Exception as ex: # noqa: BLE001
_LOGGER.warning("Failed to close DVSPortal session: %s", ex)
unload_ok = await hass.config_entries.async_forward_entry_unload(
entry, "sensor"
)
return unload_ok
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry):
"""Update options."""
await hass.config_entries.async_reload(entry.entry_id)
api_host = entry.data[CONF_HOST]
identifier = entry.data[CONF_USERNAME]
password = entry.data[CONF_PASSWORD]
user_agent = entry.data.get("user_agent", "HomeAssistant")
dvs_portal = DVSPortal(
api_host=api_host,
identifier=identifier,
password=password,
user_agent=user_agent,
)
try:
# Verify login still works
await dvs_portal.token()
except dvs_exceptions.DVSPortalError as ex:
await dvs_portal.close()
raise exceptions.ConfigEntryNotReady("Failed to initialize DVSPortal") from ex
# Setup and init stuff
coordinator = DVSPortalCoordinator(hass, dvs_portal)
entry.runtime_data = DVSPortalRuntimeData(
coordinator=coordinator,
ha_registered_license_plates=set(),
)
await hass.config_entries.async_forward_entry_setups(entry, ["sensor"])
# Check if the first refresh is successful
await coordinator.async_config_entry_first_refresh()
entry.async_on_unload(
async_dispatcher_connect(
hass, f"{DOMAIN}_{entry.entry_id}_unload", async_unload_entry
)
)
entry.add_update_listener(async_update_options)
return True