2021-05-03 10:52:22 +00:00
|
|
|
"""Support for AVM FRITZ!SmartHome devices."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-11-18 16:37:56 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
2024-05-01 18:51:39 +00:00
|
|
|
from pyfritzhome import FritzhomeDevice
|
2022-11-18 16:37:56 +00:00
|
|
|
from pyfritzhome.devicetypes.fritzhomeentitybase import FritzhomeEntityBase
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2022-01-07 13:46:17 +00:00
|
|
|
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
2024-05-01 18:51:39 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, UnitOfTemperature
|
2021-05-15 05:54:11 +00:00
|
|
|
from homeassistant.core import Event, HomeAssistant
|
2023-09-26 16:01:01 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntry, DeviceInfo
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.entity import EntityDescription
|
2021-07-22 16:19:39 +00:00
|
|
|
from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries
|
2021-10-31 19:12:25 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2024-05-01 18:51:39 +00:00
|
|
|
from .const import DOMAIN, LOGGER, PLATFORMS
|
|
|
|
from .coordinator import FritzboxConfigEntry, FritzboxDataUpdateCoordinator
|
2018-04-17 10:40:36 +00:00
|
|
|
|
|
|
|
|
2024-05-01 18:51:39 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: FritzboxConfigEntry) -> bool:
|
2021-05-03 10:52:22 +00:00
|
|
|
"""Set up the AVM FRITZ!SmartHome platforms."""
|
2022-12-12 21:11:57 +00:00
|
|
|
|
2021-07-22 16:19:39 +00:00
|
|
|
def _update_unique_id(entry: RegistryEntry) -> dict[str, str] | None:
|
|
|
|
"""Update unique ID of entity entry."""
|
|
|
|
if (
|
2022-12-20 17:29:16 +00:00
|
|
|
entry.unit_of_measurement == UnitOfTemperature.CELSIUS
|
2021-07-22 16:19:39 +00:00
|
|
|
and "_temperature" not in entry.unique_id
|
|
|
|
):
|
|
|
|
new_unique_id = f"{entry.unique_id}_temperature"
|
|
|
|
LOGGER.info(
|
|
|
|
"Migrating unique_id [%s] to [%s]", entry.unique_id, new_unique_id
|
|
|
|
)
|
|
|
|
return {"new_unique_id": new_unique_id}
|
2022-01-07 13:46:17 +00:00
|
|
|
|
|
|
|
if entry.domain == BINARY_SENSOR_DOMAIN and "_" not in entry.unique_id:
|
|
|
|
new_unique_id = f"{entry.unique_id}_alarm"
|
|
|
|
LOGGER.info(
|
|
|
|
"Migrating unique_id [%s] to [%s]", entry.unique_id, new_unique_id
|
|
|
|
)
|
|
|
|
return {"new_unique_id": new_unique_id}
|
2021-07-22 16:19:39 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
await async_migrate_entries(hass, entry.entry_id, _update_unique_id)
|
|
|
|
|
2024-05-01 18:51:39 +00:00
|
|
|
coordinator = FritzboxDataUpdateCoordinator(hass, entry.entry_id)
|
2024-04-20 10:13:56 +00:00
|
|
|
await coordinator.async_setup()
|
2024-05-01 18:51:39 +00:00
|
|
|
|
|
|
|
entry.runtime_data = coordinator
|
2024-04-20 10:13:56 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
def logout_fritzbox(event: Event) -> None:
|
2020-04-20 13:00:07 +00:00
|
|
|
"""Close connections to this fritzbox."""
|
2024-05-01 18:51:39 +00:00
|
|
|
coordinator.fritz.logout()
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2021-04-20 16:12:54 +00:00
|
|
|
entry.async_on_unload(
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, logout_fritzbox)
|
|
|
|
)
|
2018-04-17 10:40:36 +00:00
|
|
|
|
|
|
|
return True
|
2020-04-20 13:00:07 +00:00
|
|
|
|
|
|
|
|
2024-05-01 18:51:39 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: FritzboxConfigEntry) -> bool:
|
2021-05-03 10:52:22 +00:00
|
|
|
"""Unloading the AVM FRITZ!SmartHome platforms."""
|
2024-05-01 18:51:39 +00:00
|
|
|
await hass.async_add_executor_job(entry.runtime_data.fritz.logout)
|
2020-04-20 13:00:07 +00:00
|
|
|
|
2024-05-01 18:51:39 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2021-04-25 00:40:12 +00:00
|
|
|
|
|
|
|
|
2023-09-26 16:01:01 +00:00
|
|
|
async def async_remove_config_entry_device(
|
2024-05-01 18:51:39 +00:00
|
|
|
hass: HomeAssistant, entry: FritzboxConfigEntry, device: DeviceEntry
|
2023-09-26 16:01:01 +00:00
|
|
|
) -> bool:
|
|
|
|
"""Remove Fritzbox config entry from a device."""
|
2024-05-01 18:51:39 +00:00
|
|
|
coordinator = entry.runtime_data
|
2023-09-26 16:01:01 +00:00
|
|
|
|
|
|
|
for identifier in device.identifiers:
|
|
|
|
if identifier[0] == DOMAIN and (
|
|
|
|
identifier[1] in coordinator.data.devices
|
|
|
|
or identifier[1] in coordinator.data.templates
|
|
|
|
):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-11-18 16:37:56 +00:00
|
|
|
class FritzBoxEntity(CoordinatorEntity[FritzboxDataUpdateCoordinator], ABC):
|
2021-04-25 00:40:12 +00:00
|
|
|
"""Basis FritzBox entity."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2021-10-31 19:12:25 +00:00
|
|
|
coordinator: FritzboxDataUpdateCoordinator,
|
2021-04-25 00:40:12 +00:00
|
|
|
ain: str,
|
2021-08-27 15:09:34 +00:00
|
|
|
entity_description: EntityDescription | None = None,
|
2021-05-15 05:54:11 +00:00
|
|
|
) -> None:
|
2021-04-25 00:40:12 +00:00
|
|
|
"""Initialize the FritzBox entity."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
|
|
|
|
self.ain = ain
|
2021-08-27 15:09:34 +00:00
|
|
|
if entity_description is not None:
|
2023-04-03 17:04:09 +00:00
|
|
|
self._attr_has_entity_name = True
|
2021-08-27 15:09:34 +00:00
|
|
|
self.entity_description = entity_description
|
|
|
|
self._attr_unique_id = f"{ain}_{entity_description.key}"
|
|
|
|
else:
|
2022-11-19 14:12:24 +00:00
|
|
|
self._attr_name = self.data.name
|
2021-08-27 15:09:34 +00:00
|
|
|
self._attr_unique_id = ain
|
2021-04-25 00:40:12 +00:00
|
|
|
|
2022-11-18 16:37:56 +00:00
|
|
|
@property
|
|
|
|
@abstractmethod
|
2022-11-19 14:12:24 +00:00
|
|
|
def data(self) -> FritzhomeEntityBase:
|
|
|
|
"""Return data object from coordinator."""
|
2022-11-18 16:37:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FritzBoxDeviceEntity(FritzBoxEntity):
|
|
|
|
"""Reflects FritzhomeDevice and uses its attributes to construct FritzBoxDeviceEntity."""
|
|
|
|
|
2021-08-02 14:13:54 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if entity is available."""
|
2022-11-19 14:12:24 +00:00
|
|
|
return super().available and self.data.present
|
2021-08-02 14:13:54 +00:00
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
@property
|
2022-11-19 14:12:24 +00:00
|
|
|
def data(self) -> FritzhomeDevice:
|
|
|
|
"""Return device data object from coordinator."""
|
2022-11-18 16:37:56 +00:00
|
|
|
return self.coordinator.data.devices[self.ain]
|
2021-04-25 00:40:12 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-15 05:54:11 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2021-04-25 00:40:12 +00:00
|
|
|
"""Return device specific attributes."""
|
2021-10-23 10:01:21 +00:00
|
|
|
return DeviceInfo(
|
2022-11-19 14:12:24 +00:00
|
|
|
name=self.data.name,
|
2021-10-23 10:01:21 +00:00
|
|
|
identifiers={(DOMAIN, self.ain)},
|
2022-11-19 14:12:24 +00:00
|
|
|
manufacturer=self.data.manufacturer,
|
|
|
|
model=self.data.productname,
|
|
|
|
sw_version=self.data.fw_version,
|
2021-10-31 19:12:25 +00:00
|
|
|
configuration_url=self.coordinator.configuration_url,
|
2021-10-23 10:01:21 +00:00
|
|
|
)
|