diff --git a/homeassistant/components/august/binary_sensor.py b/homeassistant/components/august/binary_sensor.py index 27371f5e3f6..fe5c1f06505 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 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() diff --git a/homeassistant/components/august/entity.py b/homeassistant/components/august/entity.py index 47cb966bdc1..a13a319b568 100644 --- a/homeassistant/components/august/entity.py +++ b/homeassistant/components/august/entity.py @@ -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. diff --git a/homeassistant/components/august/sensor.py b/homeassistant/components/august/sensor.py index e5d29bb23d8..657368e019f 100644 --- a/homeassistant/components/august/sensor.py +++ b/homeassistant/components/august/sensor.py @@ -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."""