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, Activity,
ActivityType, ActivityType,
) )
from yalexs.doorbell import DoorbellDetail from yalexs.doorbell import Doorbell, DoorbellDetail
from yalexs.lock import LockDoorStatus from yalexs.lock import Lock, LockDoorStatus
from yalexs.util import update_lock_detail_from_activity from yalexs.util import update_lock_detail_from_activity
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
@ -196,7 +196,7 @@ class AugustDoorBinarySensor(AugustEntityMixin, BinarySensorEntity):
_attr_device_class = BinarySensorDeviceClass.DOOR _attr_device_class = BinarySensorDeviceClass.DOOR
def __init__( def __init__(
self, data, device, description: BinarySensorEntityDescription self, data: AugustData, device: Lock, description: BinarySensorEntityDescription
) -> None: ) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
super().__init__(data, device) super().__init__(data, device)
@ -207,7 +207,6 @@ class AugustDoorBinarySensor(AugustEntityMixin, BinarySensorEntity):
self._attr_unique_id = ( self._attr_unique_id = (
f"{self._device_id}_{cast(str, description.name).lower()}" f"{self._device_id}_{cast(str, description.name).lower()}"
) )
self._update_from_data()
@callback @callback
def _update_from_data(self): def _update_from_data(self):
@ -231,6 +230,11 @@ class AugustDoorBinarySensor(AugustEntityMixin, BinarySensorEntity):
self._attr_available = self._detail.bridge_is_online self._attr_available = self._detail.bridge_is_online
self._attr_is_on = self._detail.door_state == LockDoorStatus.OPEN 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): class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity):
"""Representation of an August binary sensor.""" """Representation of an August binary sensor."""
@ -238,7 +242,10 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity):
entity_description: AugustBinarySensorEntityDescription entity_description: AugustBinarySensorEntityDescription
def __init__( def __init__(
self, data, device, description: AugustBinarySensorEntityDescription self,
data: AugustData,
device: Doorbell,
description: AugustBinarySensorEntityDescription,
) -> None: ) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
super().__init__(data, device) super().__init__(data, device)
@ -249,7 +256,6 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity):
self._attr_unique_id = ( self._attr_unique_id = (
f"{self._device_id}_{cast(str, description.name).lower()}" f"{self._device_id}_{cast(str, description.name).lower()}"
) )
self._update_from_data()
@callback @callback
def _update_from_data(self): def _update_from_data(self):
@ -265,16 +271,10 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity):
def _schedule_update_to_recheck_turn_off_sensor(self): 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.""" """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 the sensor is already off there is nothing to do
if not self.is_on: if not self.is_on:
return 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 @callback
def _scheduled_update(now): def _scheduled_update(now):
"""Timer callback for sensor update.""" """Timer callback for sensor update."""
@ -297,5 +297,10 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity):
async def async_added_to_hass(self) -> None: 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.""" """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() 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.""" """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.core import callback
from homeassistant.helpers.entity import DeviceInfo, Entity from homeassistant.helpers.entity import DeviceInfo, Entity
from . import DOMAIN from . import DOMAIN, AugustData
from .const import MANUFACTURER from .const import MANUFACTURER
DEVICE_TYPES = ["keypad", "lock", "camera", "doorbell", "door", "bell"] DEVICE_TYPES = ["keypad", "lock", "camera", "doorbell", "door", "bell"]
@ -13,7 +18,7 @@ class AugustEntityMixin(Entity):
_attr_should_poll = False _attr_should_poll = False
def __init__(self, data, device): def __init__(self, data: AugustData, device: Doorbell | Lock) -> None:
"""Initialize an August device.""" """Initialize an August device."""
super().__init__() super().__init__()
self._data = data self._data = data
@ -46,6 +51,10 @@ class AugustEntityMixin(Entity):
self._update_from_data() self._update_from_data()
self.async_write_ha_state() 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): async def async_added_to_hass(self):
"""Subscribe to updates.""" """Subscribe to updates."""
self.async_on_remove( self.async_on_remove(

View File

@ -28,5 +28,5 @@
"documentation": "https://www.home-assistant.io/integrations/august", "documentation": "https://www.home-assistant.io/integrations/august",
"iot_class": "cloud_push", "iot_class": "cloud_push",
"loggers": ["pubnub", "yalexs"], "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 yalexs-ble==2.0.0
# homeassistant.components.august # homeassistant.components.august
yalexs==1.2.6 yalexs==1.2.7
# homeassistant.components.august # homeassistant.components.august
yalexs_ble==2.0.0 yalexs_ble==2.0.0

View File

@ -1892,7 +1892,7 @@ yalesmartalarmclient==0.3.9
yalexs-ble==2.0.0 yalexs-ble==2.0.0
# homeassistant.components.august # homeassistant.components.august
yalexs==1.2.6 yalexs==1.2.7
# homeassistant.components.august # homeassistant.components.august
yalexs_ble==2.0.0 yalexs_ble==2.0.0