From dba2998e8c9928aa7847a99be800358b5161f826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Hjelseth=20H=C3=B8yer?= Date: Tue, 14 Sep 2021 08:44:20 +0200 Subject: [PATCH] Clean up Surepetcare binary sensor (#56070) --- .../components/surepetcare/binary_sensor.py | 33 ++++++++----------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/surepetcare/binary_sensor.py b/homeassistant/components/surepetcare/binary_sensor.py index b61eae12a7e..2f411e8c2a9 100644 --- a/homeassistant/components/surepetcare/binary_sensor.py +++ b/homeassistant/components/surepetcare/binary_sensor.py @@ -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: