From 30384501af8ea6d71a6a4565ea3bb97fb147e55a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 17 Feb 2023 12:15:56 -0600 Subject: [PATCH] Cleanup august binary sensors (#88332) --- .../components/august/binary_sensor.py | 31 +++++++++++-------- homeassistant/components/august/entity.py | 13 ++++++-- homeassistant/components/august/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/august/binary_sensor.py b/homeassistant/components/august/binary_sensor.py index 52b297cc6c1..1ab2369934c 100644 --- a/homeassistant/components/august/binary_sensor.py +++ b/homeassistant/components/august/binary_sensor.py @@ -13,8 +13,8 @@ from yalexs.activity import ( Activity, ActivityType, ) -from yalexs.doorbell import DoorbellDetail -from yalexs.lock import LockDoorStatus +from yalexs.doorbell import Doorbell, DoorbellDetail +from yalexs.lock import Lock, LockDoorStatus from yalexs.util import update_lock_detail_from_activity from homeassistant.components.binary_sensor import ( @@ -196,7 +196,7 @@ class AugustDoorBinarySensor(AugustEntityMixin, BinarySensorEntity): _attr_device_class = BinarySensorDeviceClass.DOOR def __init__( - self, data, device, description: BinarySensorEntityDescription + self, data: AugustData, device: Lock, description: BinarySensorEntityDescription ) -> None: """Initialize the sensor.""" super().__init__(data, device) @@ -207,7 +207,6 @@ class AugustDoorBinarySensor(AugustEntityMixin, BinarySensorEntity): self._attr_unique_id = ( f"{self._device_id}_{cast(str, description.name).lower()}" ) - self._update_from_data() @callback def _update_from_data(self): @@ -231,6 +230,11 @@ class AugustDoorBinarySensor(AugustEntityMixin, BinarySensorEntity): self._attr_available = self._detail.bridge_is_online self._attr_is_on = self._detail.door_state == LockDoorStatus.OPEN + async def async_added_to_hass(self) -> None: + """Set the initial state when adding to hass.""" + self._update_from_data() + await super().async_added_to_hass() + class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity): """Representation of an August binary sensor.""" @@ -238,7 +242,10 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity): entity_description: AugustBinarySensorEntityDescription def __init__( - self, data, device, description: AugustBinarySensorEntityDescription + self, + data: AugustData, + device: Doorbell, + description: AugustBinarySensorEntityDescription, ) -> None: """Initialize the sensor.""" super().__init__(data, device) @@ -249,7 +256,6 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity): self._attr_unique_id = ( f"{self._device_id}_{cast(str, description.name).lower()}" ) - self._update_from_data() @callback def _update_from_data(self): @@ -265,16 +271,10 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity): def _schedule_update_to_recheck_turn_off_sensor(self): """Schedule an update to recheck the sensor to see if it is ready to turn off.""" - # If the sensor is already off there is nothing to do if not self.is_on: return - # self.hass is only available after setup is completed - # and we will recheck in async_added_to_hass - if not self.hass: - return - @callback def _scheduled_update(now): """Timer callback for sensor update.""" @@ -297,5 +297,10 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity): async def async_added_to_hass(self) -> None: """Call the mixin to subscribe and setup an async_track_point_in_utc_time to turn off the sensor if needed.""" - self._schedule_update_to_recheck_turn_off_sensor() + self._update_from_data() await super().async_added_to_hass() + + async def async_will_remove_from_hass(self) -> None: + """When removing cancel any scheduled updates.""" + self._cancel_any_pending_updates() + await super().async_will_remove_from_hass() diff --git a/homeassistant/components/august/entity.py b/homeassistant/components/august/entity.py index 8dab470376b..2f163469bfa 100644 --- a/homeassistant/components/august/entity.py +++ b/homeassistant/components/august/entity.py @@ -1,8 +1,13 @@ """Base class for August entity.""" +from abc import abstractmethod + +from yalexs.doorbell import Doorbell +from yalexs.lock import Lock + from homeassistant.core import callback from homeassistant.helpers.entity import DeviceInfo, Entity -from . import DOMAIN +from . import DOMAIN, AugustData from .const import MANUFACTURER DEVICE_TYPES = ["keypad", "lock", "camera", "doorbell", "door", "bell"] @@ -13,7 +18,7 @@ class AugustEntityMixin(Entity): _attr_should_poll = False - def __init__(self, data, device): + def __init__(self, data: AugustData, device: Doorbell | Lock) -> None: """Initialize an August device.""" super().__init__() self._data = data @@ -46,6 +51,10 @@ class AugustEntityMixin(Entity): self._update_from_data() self.async_write_ha_state() + @abstractmethod + def _update_from_data(self): + """Update the entity state from the data object.""" + async def async_added_to_hass(self): """Subscribe to updates.""" self.async_on_remove( diff --git a/homeassistant/components/august/manifest.json b/homeassistant/components/august/manifest.json index faa908f1e17..b01e1bf3664 100644 --- a/homeassistant/components/august/manifest.json +++ b/homeassistant/components/august/manifest.json @@ -28,5 +28,5 @@ "documentation": "https://www.home-assistant.io/integrations/august", "iot_class": "cloud_push", "loggers": ["pubnub", "yalexs"], - "requirements": ["yalexs==1.2.6", "yalexs_ble==2.0.0"] + "requirements": ["yalexs==1.2.7", "yalexs_ble==2.0.0"] } diff --git a/requirements_all.txt b/requirements_all.txt index f16aeb78e41..4f074464f6b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2670,7 +2670,7 @@ yalesmartalarmclient==0.3.9 yalexs-ble==2.0.0 # homeassistant.components.august -yalexs==1.2.6 +yalexs==1.2.7 # homeassistant.components.august yalexs_ble==2.0.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index ff1e38ca513..00d17289f20 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1892,7 +1892,7 @@ yalesmartalarmclient==0.3.9 yalexs-ble==2.0.0 # homeassistant.components.august -yalexs==1.2.6 +yalexs==1.2.7 # homeassistant.components.august yalexs_ble==2.0.0