1
mirror of https://github.com/home-assistant/core synced 2024-09-28 03:04:04 +02:00

Add Surepetcare entity class (#56430)

This commit is contained in:
Daniel Hjelseth Høyer 2021-09-23 22:19:46 +02:00 committed by GitHub
parent 4c40d1767a
commit a94514b00d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 34 deletions

View File

@ -1006,6 +1006,7 @@ omit =
homeassistant/components/suez_water/*
homeassistant/components/supervisord/sensor.py
homeassistant/components/surepetcare/__init__.py
homeassistant/components/surepetcare/entity.py
homeassistant/components/surepetcare/binary_sensor.py
homeassistant/components/surepetcare/sensor.py
homeassistant/components/swiss_hydrological_data/sensor.py

View File

@ -0,0 +1,46 @@
"""Entity for Surepetcare."""
from __future__ import annotations
from abc import abstractmethod
from surepy.entities import SurepyEntity
from homeassistant.core import callback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import SurePetcareDataCoordinator
class SurePetcareEntity(CoordinatorEntity):
"""An implementation for Sure Petcare Entities."""
def __init__(
self,
surepetcare_id: int,
coordinator: SurePetcareDataCoordinator,
) -> None:
"""Initialize a Sure Petcare entity."""
super().__init__(coordinator)
self._id = surepetcare_id
surepy_entity: SurepyEntity = coordinator.data[surepetcare_id]
if surepy_entity.name:
self._device_name = surepy_entity.name.capitalize()
else:
self._device_name = surepy_entity.type.name.capitalize().replace("_", " ")
self._device_id = f"{surepy_entity.household_id}-{surepetcare_id}"
self._update_attr(coordinator.data[surepetcare_id])
@abstractmethod
@callback
def _update_attr(self, surepy_entity: SurepyEntity) -> None:
"""Update the state and attributes."""
@callback
def _handle_coordinator_update(self) -> None:
"""Get the latest data and update the state."""
self._update_attr(self.coordinator.data[self._id])
self.async_write_ha_state()

View File

@ -11,12 +11,10 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_VOLTAGE, DEVICE_CLASS_BATTERY, PERCENTAGE
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from . import SurePetcareDataCoordinator
from .const import DOMAIN, SURE_BATT_VOLTAGE_DIFF, SURE_BATT_VOLTAGE_LOW
from .entity import SurePetcareEntity
_LOGGER = logging.getLogger(__name__)
@ -28,7 +26,7 @@ async def async_setup_entry(
entities: list[SureBattery] = []
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
coordinator: SurePetcareDataCoordinator = hass.data[DOMAIN][entry.entry_id]
for surepy_entity in coordinator.data.values():
@ -43,38 +41,24 @@ async def async_setup_entry(
async_add_entities(entities)
class SureBattery(CoordinatorEntity, SensorEntity):
class SureBattery(SurePetcareEntity, SensorEntity):
"""A sensor implementation for Sure Petcare Entities."""
def __init__(self, _id: int, coordinator: DataUpdateCoordinator) -> None:
"""Initialize a Sure Petcare sensor."""
super().__init__(coordinator)
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_native_unit_of_measurement = PERCENTAGE
self._id = _id
def __init__(
self, surepetcare_id: int, coordinator: SurePetcareDataCoordinator
) -> None:
"""Initialize a Sure Petcare battery sensor."""
super().__init__(surepetcare_id, coordinator)
surepy_entity: SurepyEntity = coordinator.data[_id]
self._attr_device_class = DEVICE_CLASS_BATTERY
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"
self._attr_native_unit_of_measurement = PERCENTAGE
self._attr_unique_id = (
f"{surepy_entity.household_id}-{surepy_entity.id}-battery"
)
self._update_attr()
self._attr_name = f"{self._device_name} Battery Level"
self._attr_unique_id = f"{self._device_id}-battery"
@callback
def _handle_coordinator_update(self) -> None:
"""Get the latest data and update the state."""
self._update_attr()
self.async_write_ha_state()
@callback
def _update_attr(self) -> None:
def _update_attr(self, surepy_entity: SurepyEntity) -> None:
"""Update the state and attributes."""
surepy_entity = self.coordinator.data[self._id]
state = surepy_entity.raw_data()["status"]
try:
@ -94,4 +78,3 @@ class SureBattery(CoordinatorEntity, SensorEntity):
}
else:
self._attr_extra_state_attributes = {}
_LOGGER.debug("%s -> state: %s", self.name, state)

View File

@ -6,9 +6,9 @@ from homeassistant.setup import async_setup_component
from . import HOUSEHOLD_ID, MOCK_CONFIG
EXPECTED_ENTITY_IDS = {
"sensor.pet_flap_pet_flap_battery_level": f"{HOUSEHOLD_ID}-13576-battery",
"sensor.cat_flap_cat_flap_battery_level": f"{HOUSEHOLD_ID}-13579-battery",
"sensor.feeder_feeder_battery_level": f"{HOUSEHOLD_ID}-12345-battery",
"sensor.pet_flap_battery_level": f"{HOUSEHOLD_ID}-13576-battery",
"sensor.cat_flap_battery_level": f"{HOUSEHOLD_ID}-13579-battery",
"sensor.feeder_battery_level": f"{HOUSEHOLD_ID}-12345-battery",
}