Skip to content

Commit

Permalink
Merge pull request #308 from Snuffy2/Minor-sensor-cleanup
Browse files Browse the repository at this point in the history
Minor sensor cleanup
  • Loading branch information
alexdelprete authored Oct 30, 2024
2 parents a450c6c + d9a3884 commit 425417e
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions custom_components/opnsense/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,9 @@ def _handle_coordinator_update(self) -> None:
filesystem: Mapping[str, Any] = {}
state: Mapping[str, Any] = self.coordinator.data
if not isinstance(state, Mapping):
return {}
self._available = False
self.async_write_ha_state()
return
for fsystem in state.get("telemetry", {}).get("filesystems", []):
if (
self.entity_description.key
Expand Down Expand Up @@ -611,12 +613,15 @@ def _opnsense_get_interface_property_name(self) -> str:
def _handle_coordinator_update(self) -> None:
state: Mapping[str, Any] = self.coordinator.data
if not isinstance(state, Mapping):
return {}
self._available = False
self.async_write_ha_state()
return
interface_name: str = self.entity_description.key.split(".")[1]
interface: Mapping[str, Any] = {}
for i_interface_name, iface in state.get("interfaces", {}).items():
if i_interface_name == interface_name:
interface = iface
break
if not interface:
self._available = False
self.async_write_ha_state()
Expand Down Expand Up @@ -665,11 +670,14 @@ def _handle_coordinator_update(self) -> None:
carp_interface: Mapping[str, Any] = {}
state: Mapping[str, Any] = self.coordinator.data
if not isinstance(state, Mapping):
return {}
self._available = False
self.async_write_ha_state()
return
carp_interface_name: str = self.entity_description.key.split(".")[2]
for i_interface in state.get("carp_interfaces", []):
if slugify(i_interface["subnet"]) == carp_interface_name:
carp_interface = i_interface
break
if not carp_interface:
self._available = False
self.async_write_ha_state()
Expand Down Expand Up @@ -719,6 +727,7 @@ def _handle_coordinator_update(self) -> None:
for i_gateway_name, gway in state.get("gateways", {}).items():
if i_gateway_name == gateway_name:
gateway = gway
break
if not gateway:
self._available = False
self.async_write_ha_state()
Expand Down Expand Up @@ -880,22 +889,26 @@ def icon(self) -> str:


class OPNsenseTempSensor(OPNsenseSensor):
def _opnsense_get_temp_device(self) -> str:
return self.entity_description.key.split(".")[2]

def _opnsense_get_temp(self) -> Mapping[str, Any]:
@callback
def _handle_coordinator_update(self) -> None:
state: Mapping[str, Any] = self.coordinator.data
if not isinstance(state, Mapping):
return {}
sensor_temp_device: str = self._opnsense_get_temp_device()
for temp_device, temp in state.get("telemetry", {}).get("temps", {}).items():
self._available = False
self.async_write_ha_state()
return
sensor_temp_device: str = self.entity_description.key.split(".")[2]
temp: Mapping[str, Any] = {}
for temp_device, temp_temp in (
state.get("telemetry", {}).get("temps", {}).items()
):
if temp_device == sensor_temp_device:
return temp
return {}
temp = temp_temp
break
if not temp:
self._available = False
self.async_write_ha_state()
return

@callback
def _handle_coordinator_update(self) -> None:
temp: Mapping[str, Any] = self._opnsense_get_temp()
try:
self._attr_native_value = temp["temperature"]
except (TypeError, KeyError, ZeroDivisionError):
Expand All @@ -917,6 +930,8 @@ class OPNsenseDHCPLeasesSensor(OPNsenseSensor):
def _handle_coordinator_update(self) -> None:
state: Mapping[str, Any] = self.coordinator.data
if not isinstance(state, Mapping):
self._available = False
self.async_write_ha_state()
return
if_name: str = self.entity_description.key.split(".")[1].strip()
# _LOGGER.debug(f"[OPNsenseDHCPLeasesSensor handle_coordinator_update] if_name: {if_name}")
Expand Down

0 comments on commit 425417e

Please sign in to comment.