Clean up Surepetcare sensor (#52219)
Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
This commit is contained in:
parent
3f49cdf9bf
commit
a824313e9f
1 changed files with 26 additions and 36 deletions
|
@ -2,7 +2,6 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from surepy.entities import SurepyEntity
|
from surepy.entities import SurepyEntity
|
||||||
from surepy.enums import EntityType
|
from surepy.enums import EntityType
|
||||||
|
@ -57,27 +56,41 @@ class SureBattery(SensorEntity):
|
||||||
self._id = _id
|
self._id = _id
|
||||||
self._spc: SurePetcareAPI = spc
|
self._spc: SurePetcareAPI = spc
|
||||||
|
|
||||||
self._surepy_entity: SurepyEntity = self._spc.states[_id]
|
surepy_entity: SurepyEntity = self._spc.states[_id]
|
||||||
self._state: dict[str, Any] = {}
|
|
||||||
|
|
||||||
self._attr_device_class = DEVICE_CLASS_BATTERY
|
self._attr_device_class = DEVICE_CLASS_BATTERY
|
||||||
self._attr_name = f"{self._surepy_entity.type.name.capitalize()} {self._surepy_entity.name.capitalize()} Battery Level"
|
self._attr_name = f"{surepy_entity.type.name.capitalize()} {surepy_entity.name.capitalize()} Battery Level"
|
||||||
self._attr_unit_of_measurement = PERCENTAGE
|
self._attr_unit_of_measurement = PERCENTAGE
|
||||||
self._attr_unique_id = (
|
self._attr_unique_id = (
|
||||||
f"{self._surepy_entity.household_id}-{self._surepy_entity.id}-battery"
|
f"{surepy_entity.household_id}-{surepy_entity.id}-battery"
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self) -> bool:
|
|
||||||
"""Return true if entity is available."""
|
|
||||||
return bool(self._state)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_update(self) -> None:
|
def _async_update(self) -> None:
|
||||||
"""Get the latest data and update the state."""
|
"""Get the latest data and update the state."""
|
||||||
self._surepy_entity = self._spc.states[self._id]
|
surepy_entity = self._spc.states[self._id]
|
||||||
self._state = self._surepy_entity.raw_data()["status"]
|
state = surepy_entity.raw_data()["status"]
|
||||||
_LOGGER.debug("%s -> self._state: %s", self.name, self._state)
|
|
||||||
|
self._attr_available = bool(state)
|
||||||
|
try:
|
||||||
|
per_battery_voltage = state["battery"] / 4
|
||||||
|
voltage_diff = per_battery_voltage - SURE_BATT_VOLTAGE_LOW
|
||||||
|
self._attr_state = min(
|
||||||
|
int(voltage_diff / SURE_BATT_VOLTAGE_DIFF * 100), 100
|
||||||
|
)
|
||||||
|
except (KeyError, TypeError):
|
||||||
|
self._attr_state = None
|
||||||
|
|
||||||
|
if state:
|
||||||
|
voltage_per_battery = float(state["battery"]) / 4
|
||||||
|
self._attr_extra_state_attributes = {
|
||||||
|
ATTR_VOLTAGE: f"{float(state['battery']):.2f}",
|
||||||
|
f"{ATTR_VOLTAGE}_per_battery": f"{voltage_per_battery:.2f}",
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
self._attr_extra_state_attributes = None
|
||||||
|
self.async_write_ha_state()
|
||||||
|
_LOGGER.debug("%s -> state: %s", self.name, state)
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
|
@ -85,26 +98,3 @@ class SureBattery(SensorEntity):
|
||||||
async_dispatcher_connect(self.hass, TOPIC_UPDATE, self._async_update)
|
async_dispatcher_connect(self.hass, TOPIC_UPDATE, self._async_update)
|
||||||
)
|
)
|
||||||
self._async_update()
|
self._async_update()
|
||||||
|
|
||||||
@property
|
|
||||||
def state(self) -> int | None:
|
|
||||||
"""Return battery level in percent."""
|
|
||||||
try:
|
|
||||||
per_battery_voltage = self._state["battery"] / 4
|
|
||||||
voltage_diff = per_battery_voltage - SURE_BATT_VOLTAGE_LOW
|
|
||||||
return min(int(voltage_diff / SURE_BATT_VOLTAGE_DIFF * 100), 100)
|
|
||||||
except (KeyError, TypeError):
|
|
||||||
return None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def extra_state_attributes(self) -> dict[str, Any] | None:
|
|
||||||
"""Return state attributes."""
|
|
||||||
attributes = None
|
|
||||||
if self._state:
|
|
||||||
voltage_per_battery = float(self._state["battery"]) / 4
|
|
||||||
attributes = {
|
|
||||||
ATTR_VOLTAGE: f"{float(self._state['battery']):.2f}",
|
|
||||||
f"{ATTR_VOLTAGE}_per_battery": f"{voltage_per_battery:.2f}",
|
|
||||||
}
|
|
||||||
|
|
||||||
return attributes
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue