mirror of
https://github.com/home-assistant/core.git
synced 2026-05-29 19:57:40 +01:00
98 lines
3.4 KiB
Python
98 lines
3.4 KiB
Python
"""The Wolf SmartSet Service integration."""
|
|
|
|
import logging
|
|
|
|
from httpx import RequestError
|
|
from wolf_comm.wolf_client import FetchFailed, WolfClient
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
from homeassistant.helpers import device_registry as dr
|
|
from homeassistant.helpers.httpx_client import create_async_httpx_client
|
|
|
|
from .const import DEVICE_GATEWAY, DEVICE_ID, DEVICE_NAME, DOMAIN
|
|
from .coordinator import WolflinkConfigEntry, WolfLinkCoordinator, fetch_parameters
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
PLATFORMS = [Platform.SENSOR]
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: WolflinkConfigEntry) -> bool:
|
|
"""Set up Wolf SmartSet Service from a config entry."""
|
|
|
|
username = entry.data[CONF_USERNAME]
|
|
password = entry.data[CONF_PASSWORD]
|
|
device_name = entry.data[DEVICE_NAME]
|
|
device_id = entry.data[DEVICE_ID]
|
|
gateway_id = entry.data[DEVICE_GATEWAY]
|
|
_LOGGER.debug(
|
|
"Setting up wolflink integration for device: %s (ID: %s, gateway: %s)",
|
|
device_name,
|
|
device_id,
|
|
gateway_id,
|
|
)
|
|
|
|
wolf_client = WolfClient(
|
|
username,
|
|
password,
|
|
client=create_async_httpx_client(hass=hass, verify_ssl=False, timeout=20),
|
|
)
|
|
|
|
parameters = await fetch_parameters_init(wolf_client, gateway_id, device_id)
|
|
|
|
coordinator = WolfLinkCoordinator(
|
|
hass, entry, wolf_client, parameters, gateway_id, device_id
|
|
)
|
|
|
|
await coordinator.async_refresh()
|
|
|
|
entry.runtime_data = coordinator
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: WolflinkConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
|
|
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Migrate old entry."""
|
|
# convert unique_id to string
|
|
if entry.version == 1 and entry.minor_version == 1:
|
|
if isinstance(entry.unique_id, int):
|
|
hass.config_entries.async_update_entry(
|
|
entry, unique_id=str(entry.unique_id)
|
|
)
|
|
device_registry = dr.async_get(hass)
|
|
for device in dr.async_entries_for_config_entry(
|
|
device_registry, entry.entry_id
|
|
):
|
|
new_identifiers = set()
|
|
for identifier in device.identifiers:
|
|
if identifier[0] == DOMAIN:
|
|
new_identifiers.add((DOMAIN, str(identifier[1])))
|
|
else:
|
|
new_identifiers.add(identifier)
|
|
device_registry.async_update_device(
|
|
device.id, new_identifiers=new_identifiers
|
|
)
|
|
hass.config_entries.async_update_entry(entry, minor_version=2)
|
|
|
|
return True
|
|
|
|
|
|
async def fetch_parameters_init(client: WolfClient, gateway_id: int, device_id: int):
|
|
"""Fetch all parameters via WolfClient, raising ConfigEntryNotReady on error."""
|
|
try:
|
|
return await fetch_parameters(client, gateway_id, device_id)
|
|
except (FetchFailed, RequestError) as exception:
|
|
raise ConfigEntryNotReady(
|
|
f"Error communicating with API: {exception}"
|
|
) from exception
|