2021-06-11 11:35:03 +00:00
|
|
|
"""AVM FRITZ!Box connectivity sensor."""
|
2021-09-30 09:18:04 +00:00
|
|
|
from __future__ import annotations
|
2021-05-04 03:11:21 +00:00
|
|
|
|
2022-01-02 13:59:41 +00:00
|
|
|
from dataclasses import dataclass
|
2021-09-30 09:18:04 +00:00
|
|
|
import logging
|
2021-05-04 03:11:21 +00:00
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-10 13:58:34 +00:00
|
|
|
BinarySensorDeviceClass,
|
2021-05-04 03:11:21 +00:00
|
|
|
BinarySensorEntity,
|
2021-09-30 09:18:04 +00:00
|
|
|
BinarySensorEntityDescription,
|
2021-05-04 03:11:21 +00:00
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-10 13:58:34 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2021-06-13 14:45:35 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-05-04 03:11:21 +00:00
|
|
|
|
|
|
|
from .common import FritzBoxBaseEntity, FritzBoxTools
|
2022-01-02 13:59:41 +00:00
|
|
|
from .const import DOMAIN, MeshRoles
|
2021-05-04 03:11:21 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-01-02 13:59:41 +00:00
|
|
|
@dataclass
|
|
|
|
class FritzBinarySensorEntityDescription(BinarySensorEntityDescription):
|
|
|
|
"""Describes Fritz sensor entity."""
|
|
|
|
|
|
|
|
exclude_mesh_role: MeshRoles = MeshRoles.SLAVE
|
|
|
|
|
|
|
|
|
|
|
|
SENSOR_TYPES: tuple[FritzBinarySensorEntityDescription, ...] = (
|
|
|
|
FritzBinarySensorEntityDescription(
|
2021-09-30 09:18:04 +00:00
|
|
|
key="is_connected",
|
|
|
|
name="Connection",
|
2021-12-10 13:58:34 +00:00
|
|
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-09-30 09:18:04 +00:00
|
|
|
),
|
2022-01-02 13:59:41 +00:00
|
|
|
FritzBinarySensorEntityDescription(
|
2021-09-30 09:18:04 +00:00
|
|
|
key="is_linked",
|
|
|
|
name="Link",
|
2021-12-10 13:58:34 +00:00
|
|
|
device_class=BinarySensorDeviceClass.PLUG,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-09-30 09:18:04 +00:00
|
|
|
),
|
2022-01-02 13:59:41 +00:00
|
|
|
FritzBinarySensorEntityDescription(
|
2021-09-30 09:18:04 +00:00
|
|
|
key="firmware_update",
|
|
|
|
name="Firmware Update",
|
2021-12-10 13:58:34 +00:00
|
|
|
device_class=BinarySensorDeviceClass.UPDATE,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2022-01-02 13:59:41 +00:00
|
|
|
exclude_mesh_role=MeshRoles.NONE,
|
2021-09-30 09:18:04 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-05-04 03:11:21 +00:00
|
|
|
async def async_setup_entry(
|
2021-06-13 14:45:35 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2021-05-04 03:11:21 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up entry."""
|
|
|
|
_LOGGER.debug("Setting up FRITZ!Box binary sensors")
|
2021-05-14 16:46:37 +00:00
|
|
|
fritzbox_tools: FritzBoxTools = hass.data[DOMAIN][entry.entry_id]
|
2021-05-04 03:11:21 +00:00
|
|
|
|
2021-09-30 09:18:04 +00:00
|
|
|
entities = [
|
|
|
|
FritzBoxBinarySensor(fritzbox_tools, entry.title, description)
|
|
|
|
for description in SENSOR_TYPES
|
2022-01-02 13:59:41 +00:00
|
|
|
if (description.exclude_mesh_role != fritzbox_tools.mesh_role)
|
2021-09-30 09:18:04 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
async_add_entities(entities, True)
|
2021-05-04 03:11:21 +00:00
|
|
|
|
|
|
|
|
2021-09-30 09:18:04 +00:00
|
|
|
class FritzBoxBinarySensor(FritzBoxBaseEntity, BinarySensorEntity):
|
2021-05-04 03:11:21 +00:00
|
|
|
"""Define FRITZ!Box connectivity class."""
|
|
|
|
|
2021-05-14 16:46:37 +00:00
|
|
|
def __init__(
|
2021-09-30 09:18:04 +00:00
|
|
|
self,
|
|
|
|
fritzbox_tools: FritzBoxTools,
|
|
|
|
device_friendly_name: str,
|
|
|
|
description: BinarySensorEntityDescription,
|
2021-05-14 16:46:37 +00:00
|
|
|
) -> None:
|
2021-05-04 03:11:21 +00:00
|
|
|
"""Init FRITZ!Box connectivity class."""
|
2021-09-30 09:18:04 +00:00
|
|
|
self.entity_description = description
|
|
|
|
self._attr_name = f"{device_friendly_name} {description.name}"
|
|
|
|
self._attr_unique_id = f"{fritzbox_tools.unique_id}-{description.key}"
|
2021-05-14 16:46:37 +00:00
|
|
|
super().__init__(fritzbox_tools, device_friendly_name)
|
2021-05-04 03:11:21 +00:00
|
|
|
|
|
|
|
def update(self) -> None:
|
|
|
|
"""Update data."""
|
|
|
|
_LOGGER.debug("Updating FRITZ!Box binary sensors")
|
2021-09-30 09:18:04 +00:00
|
|
|
|
2022-01-02 13:59:41 +00:00
|
|
|
if self.entity_description.key == "firmware_update":
|
2021-09-30 09:18:04 +00:00
|
|
|
self._attr_is_on = self._fritzbox_tools.update_available
|
|
|
|
self._attr_extra_state_attributes = {
|
|
|
|
"installed_version": self._fritzbox_tools.current_firmware,
|
2021-11-16 18:30:50 +00:00
|
|
|
"latest_available_version": self._fritzbox_tools.latest_firmware,
|
2021-09-30 09:18:04 +00:00
|
|
|
}
|
2022-01-02 13:59:41 +00:00
|
|
|
if self.entity_description.key == "is_connected":
|
|
|
|
self._attr_is_on = bool(self._fritzbox_tools.fritz_status.is_connected)
|
|
|
|
elif self.entity_description.key == "is_linked":
|
|
|
|
self._attr_is_on = bool(self._fritzbox_tools.fritz_status.is_linked)
|