Reduce duplicate code in august to create entities (#119934)

This commit is contained in:
J. Nick Koston 2024-06-18 17:35:38 -05:00 committed by GitHub
parent 54f8b5afdf
commit 98140e0171
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 55 deletions

View file

@ -13,8 +13,8 @@ from yalexs.activity import (
Activity,
ActivityType,
)
from yalexs.doorbell import Doorbell, DoorbellDetail
from yalexs.lock import Lock, LockDetail, LockDoorStatus
from yalexs.doorbell import DoorbellDetail
from yalexs.lock import LockDetail, LockDoorStatus
from yalexs.manager.const import ACTIVITY_UPDATE_INTERVAL
from yalexs.util import update_lock_detail_from_activity
@ -29,7 +29,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_call_later
from . import AugustConfigEntry, AugustData
from .entity import AugustEntityMixin
from .entity import AugustDescriptionEntity
_LOGGER = logging.getLogger(__name__)
@ -180,21 +180,11 @@ async def async_setup_entry(
async_add_entities(entities)
class AugustDoorBinarySensor(AugustEntityMixin, BinarySensorEntity):
class AugustDoorBinarySensor(AugustDescriptionEntity, BinarySensorEntity):
"""Representation of an August Door binary sensor."""
_attr_device_class = BinarySensorDeviceClass.DOOR
def __init__(
self,
data: AugustData,
device: Lock,
description: BinarySensorEntityDescription,
) -> None:
"""Initialize the sensor."""
super().__init__(data, device)
self.entity_description = description
self._attr_unique_id = f"{self._device_id}_{description.key}"
description: BinarySensorEntityDescription
@callback
def _update_from_data(self) -> None:
@ -219,29 +209,12 @@ 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):
class AugustDoorbellBinarySensor(AugustDescriptionEntity, BinarySensorEntity):
"""Representation of an August binary sensor."""
entity_description: AugustDoorbellBinarySensorEntityDescription
def __init__(
self,
data: AugustData,
device: Doorbell | Lock,
description: AugustDoorbellBinarySensorEntityDescription,
) -> None:
"""Initialize the sensor."""
super().__init__(data, device)
self.entity_description = description
self._check_for_off_update_listener: Callable[[], None] | None = None
self._data = data
self._attr_unique_id = f"{self._device_id}_{description.key}"
_check_for_off_update_listener: Callable[[], None] | None = None
@callback
def _update_from_data(self) -> None:
@ -280,11 +253,6 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity):
self._check_for_off_update_listener()
self._check_for_off_update_listener = 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."""
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()

View file

@ -3,6 +3,7 @@
from abc import abstractmethod
from yalexs.doorbell import Doorbell, DoorbellDetail
from yalexs.keypad import KeypadDetail
from yalexs.lock import Lock, LockDetail
from yalexs.util import get_configuration_url
@ -10,7 +11,7 @@ from homeassistant.const import ATTR_CONNECTIONS
from homeassistant.core import callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity import Entity, EntityDescription
from . import DOMAIN, AugustData
from .const import MANUFACTURER
@ -78,6 +79,26 @@ class AugustEntityMixin(Entity):
)
class AugustDescriptionEntity(AugustEntityMixin):
"""An August entity with a description."""
def __init__(
self,
data: AugustData,
device: Doorbell | Lock | KeypadDetail,
description: EntityDescription,
) -> None:
"""Initialize an August entity with a description."""
super().__init__(data, device)
self.entity_description = description
self._attr_unique_id = f"{self._device_id}_{description.key}"
async def async_added_to_hass(self) -> None:
"""Update data before adding to hass."""
self._update_from_data()
await super().async_added_to_hass()
def _remove_device_types(name: str, device_types: list[str]) -> str:
"""Strip device types from a string.

View file

@ -9,7 +9,7 @@ from typing import Any, Generic, TypeVar, cast
from yalexs.activity import ActivityType, LockOperationActivity
from yalexs.doorbell import Doorbell
from yalexs.keypad import KeypadDetail
from yalexs.lock import Lock, LockDetail
from yalexs.lock import LockDetail
from homeassistant.components.sensor import (
RestoreSensor,
@ -42,7 +42,7 @@ from .const import (
OPERATION_METHOD_REMOTE,
OPERATION_METHOD_TAG,
)
from .entity import AugustEntityMixin
from .entity import AugustDescriptionEntity, AugustEntityMixin
def _retrieve_device_battery_state(detail: LockDetail) -> int:
@ -215,25 +215,13 @@ class AugustOperatorSensor(AugustEntityMixin, RestoreSensor):
self._operated_autorelock = last_state.attributes[ATTR_OPERATION_AUTORELOCK]
class AugustBatterySensor(AugustEntityMixin, SensorEntity, Generic[_T]):
class AugustBatterySensor(AugustDescriptionEntity, SensorEntity, Generic[_T]):
"""Representation of an August sensor."""
entity_description: AugustSensorEntityDescription[_T]
_attr_device_class = SensorDeviceClass.BATTERY
_attr_native_unit_of_measurement = PERCENTAGE
def __init__(
self,
data: AugustData,
device: Doorbell | Lock | KeypadDetail,
description: AugustSensorEntityDescription[_T],
) -> None:
"""Initialize the sensor."""
super().__init__(data, device)
self.entity_description = description
self._attr_unique_id = f"{self._device_id}_{description.key}"
self._update_from_data()
@callback
def _update_from_data(self) -> None:
"""Get the latest state of the sensor."""