2020-01-06 14:00:01 +00:00
|
|
|
"""Support for Sure PetCare Flaps/Pets sensors."""
|
2021-03-18 13:31:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-01-06 14:00:01 +00:00
|
|
|
import logging
|
|
|
|
|
2021-04-27 18:58:52 +00:00
|
|
|
from surepy.entities import SurepyEntity
|
|
|
|
from surepy.enums import EntityType
|
2020-01-06 14:00:01 +00:00
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2021-04-27 18:58:52 +00:00
|
|
|
from homeassistant.const import ATTR_VOLTAGE, DEVICE_CLASS_BATTERY, PERCENTAGE
|
2020-01-06 14:00:01 +00:00
|
|
|
from homeassistant.core import callback
|
2021-09-10 06:37:00 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
2020-01-06 14:00:01 +00:00
|
|
|
)
|
|
|
|
|
2021-09-10 06:37:00 +00:00
|
|
|
from .const import DOMAIN, SURE_BATT_VOLTAGE_DIFF, SURE_BATT_VOLTAGE_LOW
|
|
|
|
|
2020-01-06 14:00:01 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
|
|
|
"""Set up Sure PetCare Flaps sensors."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2021-04-27 18:58:52 +00:00
|
|
|
entities: list[SurepyEntity] = []
|
2020-02-09 16:46:00 +00:00
|
|
|
|
2021-09-10 06:37:00 +00:00
|
|
|
coordinator: DataUpdateCoordinator = hass.data[DOMAIN]
|
2020-01-06 14:00:01 +00:00
|
|
|
|
2021-09-10 06:37:00 +00:00
|
|
|
for surepy_entity in coordinator.data.values():
|
2020-01-06 14:00:01 +00:00
|
|
|
|
2021-04-27 18:58:52 +00:00
|
|
|
if surepy_entity.type in [
|
|
|
|
EntityType.CAT_FLAP,
|
|
|
|
EntityType.PET_FLAP,
|
|
|
|
EntityType.FEEDER,
|
|
|
|
EntityType.FELAQUA,
|
2020-02-09 16:46:00 +00:00
|
|
|
]:
|
2021-09-10 06:37:00 +00:00
|
|
|
entities.append(SureBattery(surepy_entity.id, coordinator))
|
2020-02-09 16:46:00 +00:00
|
|
|
|
2021-04-27 18:58:52 +00:00
|
|
|
async_add_entities(entities)
|
2020-02-09 16:46:00 +00:00
|
|
|
|
|
|
|
|
2021-09-10 14:49:42 +00:00
|
|
|
class SureBattery(CoordinatorEntity, SensorEntity):
|
2021-06-26 22:40:40 +00:00
|
|
|
"""A sensor implementation for Sure Petcare Entities."""
|
|
|
|
|
2021-09-10 06:37:00 +00:00
|
|
|
def __init__(self, _id: int, coordinator: DataUpdateCoordinator) -> None:
|
2020-02-09 16:46:00 +00:00
|
|
|
"""Initialize a Sure Petcare sensor."""
|
2021-09-10 06:37:00 +00:00
|
|
|
super().__init__(coordinator)
|
2020-01-06 14:00:01 +00:00
|
|
|
|
|
|
|
self._id = _id
|
2020-02-09 16:46:00 +00:00
|
|
|
|
2021-09-10 06:37:00 +00:00
|
|
|
surepy_entity: SurepyEntity = coordinator.data[_id]
|
2021-06-26 22:40:40 +00:00
|
|
|
|
|
|
|
self._attr_device_class = DEVICE_CLASS_BATTERY
|
2021-08-24 09:21:57 +00:00
|
|
|
if surepy_entity.name:
|
|
|
|
self._attr_name = f"{surepy_entity.type.name.capitalize()} {surepy_entity.name.capitalize()} Battery Level"
|
|
|
|
else:
|
|
|
|
self._attr_name = f"{surepy_entity.type.name.capitalize()} Battery Level"
|
2021-08-11 16:57:50 +00:00
|
|
|
self._attr_native_unit_of_measurement = PERCENTAGE
|
2021-06-26 22:40:40 +00:00
|
|
|
self._attr_unique_id = (
|
2021-06-27 15:25:54 +00:00
|
|
|
f"{surepy_entity.household_id}-{surepy_entity.id}-battery"
|
2020-02-09 16:46:00 +00:00
|
|
|
)
|
2021-09-10 06:37:00 +00:00
|
|
|
self._update_attr()
|
2020-01-06 14:00:01 +00:00
|
|
|
|
2021-04-27 18:58:52 +00:00
|
|
|
@callback
|
2021-09-10 06:37:00 +00:00
|
|
|
def _handle_coordinator_update(self) -> None:
|
2020-02-09 16:46:00 +00:00
|
|
|
"""Get the latest data and update the state."""
|
2021-09-10 06:37:00 +00:00
|
|
|
self._update_attr()
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_attr(self) -> None:
|
|
|
|
"""Update the state and attributes."""
|
|
|
|
surepy_entity = self.coordinator.data[self._id]
|
2021-06-27 15:25:54 +00:00
|
|
|
state = surepy_entity.raw_data()["status"]
|
2020-02-09 16:46:00 +00:00
|
|
|
|
2020-01-06 14:00:01 +00:00
|
|
|
try:
|
2021-06-27 15:25:54 +00:00
|
|
|
per_battery_voltage = state["battery"] / 4
|
2020-01-06 14:00:01 +00:00
|
|
|
voltage_diff = per_battery_voltage - SURE_BATT_VOLTAGE_LOW
|
2021-08-11 16:57:50 +00:00
|
|
|
self._attr_native_value = min(
|
2021-06-27 15:25:54 +00:00
|
|
|
int(voltage_diff / SURE_BATT_VOLTAGE_DIFF * 100), 100
|
|
|
|
)
|
2020-01-06 14:00:01 +00:00
|
|
|
except (KeyError, TypeError):
|
2021-08-11 16:57:50 +00:00
|
|
|
self._attr_native_value = None
|
2021-06-27 15:25:54 +00:00
|
|
|
|
|
|
|
if state:
|
|
|
|
voltage_per_battery = float(state["battery"]) / 4
|
|
|
|
self._attr_extra_state_attributes = {
|
|
|
|
ATTR_VOLTAGE: f"{float(state['battery']):.2f}",
|
2020-02-09 16:46:00 +00:00
|
|
|
f"{ATTR_VOLTAGE}_per_battery": f"{voltage_per_battery:.2f}",
|
|
|
|
}
|
2021-06-27 15:25:54 +00:00
|
|
|
else:
|
2021-08-24 09:21:57 +00:00
|
|
|
self._attr_extra_state_attributes = {}
|
2021-06-27 15:25:54 +00:00
|
|
|
_LOGGER.debug("%s -> state: %s", self.name, state)
|