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