Skip to content

Commit

Permalink
Only enable ipv4 sensor if available
Browse files Browse the repository at this point in the history
  • Loading branch information
karlsvenssonn committed Jan 10, 2025
1 parent 15ca408 commit 3d9fd19
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions custom_components/tplink_router/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class TPLinkRouterSensorEntityDescription(SensorEntityDescription, TPLinkRouterS
name="IPv4 Connection Type",
icon="mdi:wan",
value=lambda ipv4_status: "WAN" if ipv4_status.wan_ipv4_conntype == "ipoe_1_d" else "4G",
entity_registry_enabled_default=False,
),
)

Expand Down Expand Up @@ -139,13 +138,20 @@ def _handle_coordinator_update(self) -> None:

# Notify Home Assistant about the state update
self.async_write_ha_state()

@property
def available(self) -> bool:
"""Return True if entity is available."""
if self.entity_description.key.startswith("ipv4"):
data = self.coordinator.ipv4_status
# Check if the router supports `get_ipv4_status` and if ipv4_status data is available
return (
hasattr(self.coordinator.router, "get_ipv4_status") and
self.coordinator.ipv4_status is not None and
self.entity_description.value(self.coordinator.ipv4_status) is not None
)
else:
data = self.coordinator.status

return data is not None and self.entity_description.value(data) is not None
# General availability check for other sensors
return (
self.coordinator.status is not None and
self.entity_description.value(self.coordinator.status) is not None
)

0 comments on commit 3d9fd19

Please sign in to comment.