Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor sensor cleanup #308

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading