Add IPv6 sensor to fritz component (#75708)

* Add IPv6 sensor to fritz component

* Cast return type to string

* Make ipv6 sensor suitable

* simplify cast to str

* use extisting property

Co-authored-by: chemelli74 <simone.chemelli@gmail.com>
Co-authored-by: mib1185 <mail@mib85.de>
This commit is contained in:
Stephan Singer 2022-12-05 00:48:41 +01:00 committed by GitHub
parent 0f83db82d9
commit 64a72daa27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 0 deletions

View file

@ -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

View file

@ -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",

View file

@ -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": {

View file

@ -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,