diff --git a/homeassistant/components/fritz/binary_sensor.py b/homeassistant/components/fritz/binary_sensor.py index b416e0cfb11..db1aac99c47 100644 --- a/homeassistant/components/fritz/binary_sensor.py +++ b/homeassistant/components/fritz/binary_sensor.py @@ -1,6 +1,7 @@ """AVM FRITZ!Box connectivity sensor.""" from __future__ import annotations +from collections.abc import Callable from dataclasses import dataclass import logging @@ -14,8 +15,8 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .common import AvmWrapper, FritzBoxBaseEntity -from .const import DOMAIN, MeshRoles +from .common import AvmWrapper, ConnectionInfo, FritzBoxBaseEntity +from .const import DOMAIN _LOGGER = logging.getLogger(__name__) @@ -24,7 +25,7 @@ _LOGGER = logging.getLogger(__name__) class FritzBinarySensorEntityDescription(BinarySensorEntityDescription): """Describes Fritz sensor entity.""" - exclude_mesh_role: MeshRoles = MeshRoles.SLAVE + is_suitable: Callable[[ConnectionInfo], bool] = lambda info: info.wan_enabled SENSOR_TYPES: tuple[FritzBinarySensorEntityDescription, ...] = ( @@ -45,7 +46,7 @@ SENSOR_TYPES: tuple[FritzBinarySensorEntityDescription, ...] = ( name="Firmware Update", device_class=BinarySensorDeviceClass.UPDATE, 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") avm_wrapper: AvmWrapper = hass.data[DOMAIN][entry.entry_id] + connection_info = await avm_wrapper.async_get_connection_info() + entities = [ FritzBoxBinarySensor(avm_wrapper, entry.title, description) 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) diff --git a/homeassistant/components/fritz/common.py b/homeassistant/components/fritz/common.py index 2fc28433e56..4c307c126cd 100644 --- a/homeassistant/components/fritz/common.py +++ b/homeassistant/components/fritz/common.py @@ -642,6 +642,22 @@ class AvmWrapper(FritzBoxTools): 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]: """Call GetGenericPortMappingEntry action.""" @@ -970,3 +986,12 @@ class FritzBoxBaseEntity: name=self._device_name, sw_version=self._avm_wrapper.current_firmware, ) + + +@dataclass +class ConnectionInfo: + """Fritz sensor connection information class.""" + + connection: str + mesh_role: MeshRoles + wan_enabled: bool diff --git a/homeassistant/components/fritz/sensor.py b/homeassistant/components/fritz/sensor.py index f01966d7114..9811adf6829 100644 --- a/homeassistant/components/fritz/sensor.py +++ b/homeassistant/components/fritz/sensor.py @@ -28,8 +28,8 @@ from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util.dt import utcnow -from .common import AvmWrapper, FritzBoxBaseEntity -from .const import DOMAIN, DSL_CONNECTION, UPTIME_DEVIATION, MeshRoles +from .common import AvmWrapper, ConnectionInfo, FritzBoxBaseEntity +from .const import DOMAIN, DSL_CONNECTION, UPTIME_DEVIATION _LOGGER = logging.getLogger(__name__) @@ -134,15 +134,6 @@ def _retrieve_link_attenuation_received_state( 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 class FritzRequireKeysMixin: """Fritz sensor data class.""" @@ -283,18 +274,7 @@ async def async_setup_entry( _LOGGER.debug("Setting up FRITZ!Box sensors") avm_wrapper: AvmWrapper = hass.data[DOMAIN][entry.entry_id] - link_properties = await avm_wrapper.async_get_wan_link_properties() - 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, - ) + connection_info = await avm_wrapper.async_get_connection_info() entities = [ FritzBoxSensor(avm_wrapper, entry.title, description)