Cleanup august binary sensors (#88332)

This commit is contained in:
J. Nick Koston 2023-02-17 12:15:56 -06:00 committed by GitHub
parent a16e298599
commit 30384501af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 18 deletions

View file

@ -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()

View file

@ -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(

View file

@ -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"]
}

View file

@ -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

View file

@ -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