diff --git a/homeassistant/components/fritz/common.py b/homeassistant/components/fritz/common.py index 6364ada9fb2..5af4f0c2239 100644 --- a/homeassistant/components/fritz/common.py +++ b/homeassistant/components/fritz/common.py @@ -663,6 +663,14 @@ class AvmWrapper(FritzBoxTools): partial(self.get_wan_link_properties) ) + async def async_ipv6_active(self) -> bool: + """Check ip an ipv6 is active on the WAn interface.""" + + def wrap_external_ipv6() -> str: + return str(self.fritz_status.external_ipv6) + + return bool(await self.hass.async_add_executor_job(wrap_external_ipv6)) + async def async_get_connection_info(self) -> ConnectionInfo: """Return ConnectionInfo data.""" @@ -671,6 +679,7 @@ class AvmWrapper(FritzBoxTools): connection=link_properties.get("NewWANAccessType", "").lower(), mesh_role=self.mesh_role, wan_enabled=self.device_is_router, + ipv6_active=await self.async_ipv6_active(), ) _LOGGER.debug( "ConnectionInfo for FritzBox %s: %s", @@ -1011,3 +1020,4 @@ class ConnectionInfo: connection: str mesh_role: MeshRoles wan_enabled: bool + ipv6_active: bool diff --git a/homeassistant/components/fritz/sensor.py b/homeassistant/components/fritz/sensor.py index 0f3d8cb1ae0..821a0000a5e 100644 --- a/homeassistant/components/fritz/sensor.py +++ b/homeassistant/components/fritz/sensor.py @@ -66,6 +66,11 @@ def _retrieve_external_ip_state(status: FritzStatus, last_value: str) -> str: return status.external_ip # type: ignore[no-any-return] +def _retrieve_external_ipv6_state(status: FritzStatus, last_value: str) -> str: + """Return external ipv6 from device.""" + return str(status.external_ipv6) + + def _retrieve_kb_s_sent_state(status: FritzStatus, last_value: str) -> float: """Return upload transmission rate.""" return round(status.transmission_rate[0] / 1000, 1) # type: ignore[no-any-return] @@ -155,6 +160,13 @@ SENSOR_TYPES: tuple[FritzSensorEntityDescription, ...] = ( icon="mdi:earth", value_fn=_retrieve_external_ip_state, ), + FritzSensorEntityDescription( + key="external_ipv6", + name="External IPv6", + icon="mdi:earth", + value_fn=_retrieve_external_ipv6_state, + is_suitable=lambda info: info.ipv6_active, + ), FritzSensorEntityDescription( key="device_uptime", name="Device Uptime", diff --git a/tests/components/fritz/const.py b/tests/components/fritz/const.py index 32f2211d16b..fb62e14bc6f 100644 --- a/tests/components/fritz/const.py +++ b/tests/components/fritz/const.py @@ -138,6 +138,7 @@ MOCK_FB_SERVICES: dict[str, dict] = { "NewUptime": 35307, }, "GetExternalIPAddress": {"NewExternalIPAddress": "1.2.3.4"}, + "X_AVM_DE_GetExternalIPv6Address": {"NewExternalIPv6Address": "fec0::1"}, }, "WANPPPConnection1": { "GetInfo": { diff --git a/tests/components/fritz/test_sensor.py b/tests/components/fritz/test_sensor.py index 2a3435210e7..117e9d31967 100644 --- a/tests/components/fritz/test_sensor.py +++ b/tests/components/fritz/test_sensor.py @@ -35,6 +35,10 @@ SENSOR_STATES: dict[str, dict[str, Any]] = { ATTR_STATE: "1.2.3.4", ATTR_ICON: "mdi:earth", }, + "sensor.mock_title_external_ipv6": { + ATTR_STATE: "fec0::1", + ATTR_ICON: "mdi:earth", + }, "sensor.mock_title_device_uptime": { # ATTR_STATE: "2022-02-05T17:46:04+00:00", ATTR_DEVICE_CLASS: SensorDeviceClass.TIMESTAMP,