1 """The ViCare integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
6 from contextlib
import suppress
11 from PyViCare.PyViCare
import PyViCare
12 from PyViCare.PyViCareDeviceConfig
import PyViCareDeviceConfig
13 from PyViCare.PyViCareUtils
import (
14 PyViCareInvalidConfigurationError,
15 PyViCareInvalidCredentialsError,
27 DEFAULT_CACHE_DURATION,
33 from .types
import ViCareDevice
34 from .utils
import get_device, get_device_serial
36 _LOGGER = logging.getLogger(__name__)
37 _TOKEN_FILENAME =
"vicare_token.save"
41 """Set up from config entry."""
42 _LOGGER.debug(
"Setting up ViCare component")
44 hass.data[DOMAIN] = {}
45 hass.data[DOMAIN][entry.entry_id] = {}
48 await hass.async_add_executor_job(setup_vicare_api, hass, entry)
49 except (PyViCareInvalidConfigurationError, PyViCareInvalidCredentialsError)
as err:
52 for device
in hass.data[DOMAIN][entry.entry_id][DEVICE_LIST]:
56 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
63 entry_data: Mapping[str, Any],
64 cache_duration=DEFAULT_CACHE_DURATION,
66 """Login via PyVicare API."""
67 vicare_api = PyViCare()
68 vicare_api.setCacheDuration(cache_duration)
69 vicare_api.initWithCredentials(
70 entry_data[CONF_USERNAME],
71 entry_data[CONF_PASSWORD],
72 entry_data[CONF_CLIENT_ID],
73 hass.config.path(STORAGE_DIR, _TOKEN_FILENAME),
79 """Set up PyVicare API."""
83 if (number_of_devices := len(device_config_list)) > 1:
84 cache_duration = DEFAULT_CACHE_DURATION * number_of_devices
86 "Found %s devices, adjusting cache duration to %s",
90 vicare_api =
vicare_login(hass, entry.data, cache_duration)
93 for device
in device_config_list:
95 "Found device: %s (online: %s)", device.getModel(),
str(device.isOnline())
98 hass.data[DOMAIN][entry.entry_id][DEVICE_LIST] = [
100 for device_config
in device_config_list
105 """Unload ViCare config entry."""
106 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
108 hass.data[DOMAIN].pop(entry.entry_id)
110 with suppress(FileNotFoundError):
111 await hass.async_add_executor_job(
112 os.remove, hass.config.path(STORAGE_DIR, _TOKEN_FILENAME)
119 hass: HomeAssistant, entry: ConfigEntry, device: ViCareDevice
121 """Migrate old entry."""
122 device_registry = dr.async_get(hass)
123 entity_registry = er.async_get(hass)
125 gateway_serial: str = device.config.getConfig().serial
126 device_id = device.config.getId()
127 device_serial: str |
None = await hass.async_add_executor_job(
128 get_device_serial, device.api
130 device_model = device.config.getModel()
132 old_identifier = gateway_serial
134 f
"{gateway_serial}_{device_serial if device_serial is not None else device_id}"
138 for device_entry
in dr.async_entries_for_config_entry(
139 device_registry, entry.entry_id
142 device_entry.identifiers == {(DOMAIN, old_identifier)}
143 and device_entry.model == device_model
146 "Migrating device %s to new identifier %s",
150 device_registry.async_update_device(
152 serial_number=device_serial,
153 new_identifiers={(DOMAIN, new_identifier)},
157 for entity_entry
in er.async_entries_for_device(
158 entity_registry, device_entry.id,
True
160 if entity_entry.unique_id.startswith(new_identifier):
163 unique_id_parts = entity_entry.unique_id.split(
"-")
166 unique_id_parts[0] = new_identifier
170 if entity_entry.domain == DOMAIN_CLIMATE:
171 unique_id_parts[len(unique_id_parts) - 1] = (
172 f
"{entity_entry.translation_key}-{unique_id_parts[len(unique_id_parts)-1]}"
174 entity_new_unique_id =
"-".join(unique_id_parts)
177 "Migrating entity %s to new unique id %s",
179 entity_new_unique_id,
181 entity_registry.async_update_entity(
182 entity_id=entity_entry.entity_id, new_unique_id=entity_new_unique_id
187 devices: list[PyViCareDeviceConfig],
188 ) -> list[PyViCareDeviceConfig]:
189 """Remove unsupported devices from the list."""
192 for device_config
in devices
193 if device_config.getModel()
not in UNSUPPORTED_DEVICES
DeviceEntry get_device(HomeAssistant hass, str unique_id)
PyViCare vicare_login(HomeAssistant hass, Mapping[str, Any] entry_data, cache_duration=DEFAULT_CACHE_DURATION)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
None setup_vicare_api(HomeAssistant hass, ConfigEntry entry)
None async_migrate_devices_and_entities(HomeAssistant hass, ConfigEntry entry, ViCareDevice device)
list[PyViCareDeviceConfig] get_supported_devices(list[PyViCareDeviceConfig] devices)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)