From a824313e9f8498bc64430b548f62c2d23a1dbc2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Hjelseth=20H=C3=B8yer?= Date: Sun, 27 Jun 2021 17:25:54 +0200 Subject: [PATCH] Clean up Surepetcare sensor (#52219) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Hjelseth Høyer --- .../components/surepetcare/sensor.py | 62 ++++++++----------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/homeassistant/components/surepetcare/sensor.py b/homeassistant/components/surepetcare/sensor.py index 681ef5b067c..fbc8222f292 100644 --- a/homeassistant/components/surepetcare/sensor.py +++ b/homeassistant/components/surepetcare/sensor.py @@ -2,7 +2,6 @@ from __future__ import annotations import logging -from typing import Any from surepy.entities import SurepyEntity from surepy.enums import EntityType @@ -57,27 +56,41 @@ class SureBattery(SensorEntity): self._id = _id self._spc: SurePetcareAPI = spc - self._surepy_entity: SurepyEntity = self._spc.states[_id] - self._state: dict[str, Any] = {} + surepy_entity: SurepyEntity = self._spc.states[_id] 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_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 def _async_update(self) -> None: """Get the latest data and update the state.""" - self._surepy_entity = self._spc.states[self._id] - self._state = self._surepy_entity.raw_data()["status"] - _LOGGER.debug("%s -> self._state: %s", self.name, self._state) + surepy_entity = self._spc.states[self._id] + state = surepy_entity.raw_data()["status"] + + 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: """Register callbacks.""" @@ -85,26 +98,3 @@ class SureBattery(SensorEntity): async_dispatcher_connect(self.hass, TOPIC_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