Clean up Surepetcare binary sensor (#56070)

This commit is contained in:
Daniel Hjelseth Høyer 2021-09-14 08:44:20 +02:00 committed by GitHub
parent fe1311ba34
commit dba2998e8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,14 +55,13 @@ class SurePetcareBinarySensor(CoordinatorEntity, BinarySensorEntity):
self,
_id: int,
coordinator: DataUpdateCoordinator,
device_class: str,
) -> None:
"""Initialize a Sure Petcare binary sensor."""
super().__init__(coordinator)
self._id = _id
surepy_entity: SurepyEntity = coordinator.data[self._id]
surepy_entity: SurepyEntity = coordinator.data[_id]
# cover special case where a device has no name set
if surepy_entity.name:
@ -70,29 +69,26 @@ class SurePetcareBinarySensor(CoordinatorEntity, BinarySensorEntity):
else:
name = f"Unnamed {surepy_entity.type.name.capitalize()}"
self._attr_device_class = device_class
self._attr_name = f"{surepy_entity.type.name.capitalize()} {name.capitalize()}"
self._attr_unique_id = f"{surepy_entity.household_id}-{self._id}"
self._update_attr()
self._attr_unique_id = f"{surepy_entity.household_id}-{_id}"
self._update_attr(coordinator.data[_id])
@abstractmethod
@callback
def _update_attr(self) -> None:
def _update_attr(self, surepy_entity) -> 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._update_attr(self.coordinator.data[self._id])
self.async_write_ha_state()
class Hub(SurePetcareBinarySensor):
"""Sure Petcare Hub."""
def __init__(self, _id: int, coordinator: DataUpdateCoordinator) -> None:
"""Initialize a Sure Petcare Hub."""
super().__init__(_id, coordinator, DEVICE_CLASS_CONNECTIVITY)
_attr_device_class = DEVICE_CLASS_CONNECTIVITY
@property
def available(self) -> bool:
@ -100,9 +96,8 @@ class Hub(SurePetcareBinarySensor):
return super().available and bool(self._attr_is_on)
@callback
def _update_attr(self) -> None:
def _update_attr(self, surepy_entity) -> None:
"""Get the latest data and update the state."""
surepy_entity = self.coordinator.data[self._id]
state = surepy_entity.raw_data()["status"]
self._attr_is_on = self._attr_available = bool(state["online"])
if surepy_entity.raw_data():
@ -120,14 +115,11 @@ class Hub(SurePetcareBinarySensor):
class Pet(SurePetcareBinarySensor):
"""Sure Petcare Pet."""
def __init__(self, _id: int, coordinator: DataUpdateCoordinator) -> None:
"""Initialize a Sure Petcare Pet."""
super().__init__(_id, coordinator, DEVICE_CLASS_PRESENCE)
_attr_device_class = DEVICE_CLASS_PRESENCE
@callback
def _update_attr(self) -> None:
def _update_attr(self, surepy_entity) -> None:
"""Get the latest data and update the state."""
surepy_entity = self.coordinator.data[self._id]
state = surepy_entity.location
try:
self._attr_is_on = bool(Location(state.where) == Location.INSIDE)
@ -146,21 +138,22 @@ class Pet(SurePetcareBinarySensor):
class DeviceConnectivity(SurePetcareBinarySensor):
"""Sure Petcare Device."""
_attr_device_class = DEVICE_CLASS_CONNECTIVITY
def __init__(
self,
_id: int,
coordinator: DataUpdateCoordinator,
) -> None:
"""Initialize a Sure Petcare Device."""
super().__init__(_id, coordinator, DEVICE_CLASS_CONNECTIVITY)
super().__init__(_id, coordinator)
self._attr_name = f"{self.name}_connectivity"
self._attr_unique_id = (
f"{self.coordinator.data[self._id].household_id}-{self._id}-connectivity"
)
@callback
def _update_attr(self):
surepy_entity = self.coordinator.data[self._id]
def _update_attr(self, surepy_entity):
state = surepy_entity.raw_data()["status"]
self._attr_is_on = bool(state)
if state: