mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Allign logic for Fritz sensors and binary_sensors (#67623)
This commit is contained in:
parent
e4221336dc
commit
53543f15a5
@ -1,6 +1,7 @@
|
|||||||
"""AVM FRITZ!Box connectivity sensor."""
|
"""AVM FRITZ!Box connectivity sensor."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -14,8 +15,8 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity import EntityCategory
|
from homeassistant.helpers.entity import EntityCategory
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .common import AvmWrapper, FritzBoxBaseEntity
|
from .common import AvmWrapper, ConnectionInfo, FritzBoxBaseEntity
|
||||||
from .const import DOMAIN, MeshRoles
|
from .const import DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -24,7 +25,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
class FritzBinarySensorEntityDescription(BinarySensorEntityDescription):
|
class FritzBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||||
"""Describes Fritz sensor entity."""
|
"""Describes Fritz sensor entity."""
|
||||||
|
|
||||||
exclude_mesh_role: MeshRoles = MeshRoles.SLAVE
|
is_suitable: Callable[[ConnectionInfo], bool] = lambda info: info.wan_enabled
|
||||||
|
|
||||||
|
|
||||||
SENSOR_TYPES: tuple[FritzBinarySensorEntityDescription, ...] = (
|
SENSOR_TYPES: tuple[FritzBinarySensorEntityDescription, ...] = (
|
||||||
@ -45,7 +46,7 @@ SENSOR_TYPES: tuple[FritzBinarySensorEntityDescription, ...] = (
|
|||||||
name="Firmware Update",
|
name="Firmware Update",
|
||||||
device_class=BinarySensorDeviceClass.UPDATE,
|
device_class=BinarySensorDeviceClass.UPDATE,
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
exclude_mesh_role=MeshRoles.NONE,
|
is_suitable=lambda info: True,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -57,10 +58,12 @@ async def async_setup_entry(
|
|||||||
_LOGGER.debug("Setting up FRITZ!Box binary sensors")
|
_LOGGER.debug("Setting up FRITZ!Box binary sensors")
|
||||||
avm_wrapper: AvmWrapper = hass.data[DOMAIN][entry.entry_id]
|
avm_wrapper: AvmWrapper = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
|
connection_info = await avm_wrapper.async_get_connection_info()
|
||||||
|
|
||||||
entities = [
|
entities = [
|
||||||
FritzBoxBinarySensor(avm_wrapper, entry.title, description)
|
FritzBoxBinarySensor(avm_wrapper, entry.title, description)
|
||||||
for description in SENSOR_TYPES
|
for description in SENSOR_TYPES
|
||||||
if (description.exclude_mesh_role != avm_wrapper.mesh_role)
|
if description.is_suitable(connection_info)
|
||||||
]
|
]
|
||||||
|
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
@ -642,6 +642,22 @@ class AvmWrapper(FritzBoxTools):
|
|||||||
partial(self.get_wan_link_properties)
|
partial(self.get_wan_link_properties)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def async_get_connection_info(self) -> ConnectionInfo:
|
||||||
|
"""Return ConnectionInfo data."""
|
||||||
|
|
||||||
|
link_properties = await self.async_get_wan_link_properties()
|
||||||
|
connection_info = ConnectionInfo(
|
||||||
|
connection=link_properties.get("NewWANAccessType", "").lower(),
|
||||||
|
mesh_role=self.mesh_role,
|
||||||
|
wan_enabled=self.device_is_router,
|
||||||
|
)
|
||||||
|
_LOGGER.debug(
|
||||||
|
"ConnectionInfo for FritzBox %s: %s",
|
||||||
|
self.host,
|
||||||
|
connection_info,
|
||||||
|
)
|
||||||
|
return connection_info
|
||||||
|
|
||||||
async def async_get_port_mapping(self, con_type: str, index: int) -> dict[str, Any]:
|
async def async_get_port_mapping(self, con_type: str, index: int) -> dict[str, Any]:
|
||||||
"""Call GetGenericPortMappingEntry action."""
|
"""Call GetGenericPortMappingEntry action."""
|
||||||
|
|
||||||
@ -970,3 +986,12 @@ class FritzBoxBaseEntity:
|
|||||||
name=self._device_name,
|
name=self._device_name,
|
||||||
sw_version=self._avm_wrapper.current_firmware,
|
sw_version=self._avm_wrapper.current_firmware,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ConnectionInfo:
|
||||||
|
"""Fritz sensor connection information class."""
|
||||||
|
|
||||||
|
connection: str
|
||||||
|
mesh_role: MeshRoles
|
||||||
|
wan_enabled: bool
|
||||||
|
@ -28,8 +28,8 @@ from homeassistant.helpers.entity import EntityCategory
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
from .common import AvmWrapper, FritzBoxBaseEntity
|
from .common import AvmWrapper, ConnectionInfo, FritzBoxBaseEntity
|
||||||
from .const import DOMAIN, DSL_CONNECTION, UPTIME_DEVIATION, MeshRoles
|
from .const import DOMAIN, DSL_CONNECTION, UPTIME_DEVIATION
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -134,15 +134,6 @@ def _retrieve_link_attenuation_received_state(
|
|||||||
return status.attenuation[1] / 10 # type: ignore[no-any-return]
|
return status.attenuation[1] / 10 # type: ignore[no-any-return]
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class ConnectionInfo:
|
|
||||||
"""Fritz sensor connection information class."""
|
|
||||||
|
|
||||||
connection: str
|
|
||||||
mesh_role: MeshRoles
|
|
||||||
wan_enabled: bool
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class FritzRequireKeysMixin:
|
class FritzRequireKeysMixin:
|
||||||
"""Fritz sensor data class."""
|
"""Fritz sensor data class."""
|
||||||
@ -283,18 +274,7 @@ async def async_setup_entry(
|
|||||||
_LOGGER.debug("Setting up FRITZ!Box sensors")
|
_LOGGER.debug("Setting up FRITZ!Box sensors")
|
||||||
avm_wrapper: AvmWrapper = hass.data[DOMAIN][entry.entry_id]
|
avm_wrapper: AvmWrapper = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
link_properties = await avm_wrapper.async_get_wan_link_properties()
|
connection_info = await avm_wrapper.async_get_connection_info()
|
||||||
connection_info = ConnectionInfo(
|
|
||||||
connection=link_properties.get("NewWANAccessType", "").lower(),
|
|
||||||
mesh_role=avm_wrapper.mesh_role,
|
|
||||||
wan_enabled=avm_wrapper.device_is_router,
|
|
||||||
)
|
|
||||||
|
|
||||||
_LOGGER.debug(
|
|
||||||
"ConnectionInfo for FritzBox %s: %s",
|
|
||||||
avm_wrapper.host,
|
|
||||||
connection_info,
|
|
||||||
)
|
|
||||||
|
|
||||||
entities = [
|
entities = [
|
||||||
FritzBoxSensor(avm_wrapper, entry.title, description)
|
FritzBoxSensor(avm_wrapper, entry.title, description)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user