mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Reduce duplicate code in august to create entities (#119934)
This commit is contained in:
parent
54f8b5afdf
commit
98140e0171
@ -13,8 +13,8 @@ from yalexs.activity import (
|
|||||||
Activity,
|
Activity,
|
||||||
ActivityType,
|
ActivityType,
|
||||||
)
|
)
|
||||||
from yalexs.doorbell import Doorbell, DoorbellDetail
|
from yalexs.doorbell import DoorbellDetail
|
||||||
from yalexs.lock import Lock, LockDetail, LockDoorStatus
|
from yalexs.lock import LockDetail, LockDoorStatus
|
||||||
from yalexs.manager.const import ACTIVITY_UPDATE_INTERVAL
|
from yalexs.manager.const import ACTIVITY_UPDATE_INTERVAL
|
||||||
from yalexs.util import update_lock_detail_from_activity
|
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 homeassistant.helpers.event import async_call_later
|
||||||
|
|
||||||
from . import AugustConfigEntry, AugustData
|
from . import AugustConfigEntry, AugustData
|
||||||
from .entity import AugustEntityMixin
|
from .entity import AugustDescriptionEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -180,21 +180,11 @@ async def async_setup_entry(
|
|||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class AugustDoorBinarySensor(AugustEntityMixin, BinarySensorEntity):
|
class AugustDoorBinarySensor(AugustDescriptionEntity, BinarySensorEntity):
|
||||||
"""Representation of an August Door binary sensor."""
|
"""Representation of an August Door binary sensor."""
|
||||||
|
|
||||||
_attr_device_class = BinarySensorDeviceClass.DOOR
|
_attr_device_class = BinarySensorDeviceClass.DOOR
|
||||||
|
description: BinarySensorEntityDescription
|
||||||
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}"
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _update_from_data(self) -> None:
|
def _update_from_data(self) -> None:
|
||||||
@ -219,29 +209,12 @@ 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(AugustDescriptionEntity, BinarySensorEntity):
|
||||||
class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity):
|
|
||||||
"""Representation of an August binary sensor."""
|
"""Representation of an August binary sensor."""
|
||||||
|
|
||||||
entity_description: AugustDoorbellBinarySensorEntityDescription
|
entity_description: AugustDoorbellBinarySensorEntityDescription
|
||||||
|
_check_for_off_update_listener: Callable[[], None] | None = None
|
||||||
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}"
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _update_from_data(self) -> None:
|
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()
|
||||||
self._check_for_off_update_listener = None
|
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:
|
async def async_will_remove_from_hass(self) -> None:
|
||||||
"""When removing cancel any scheduled updates."""
|
"""When removing cancel any scheduled updates."""
|
||||||
self._cancel_any_pending_updates()
|
self._cancel_any_pending_updates()
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
|
|
||||||
from yalexs.doorbell import Doorbell, DoorbellDetail
|
from yalexs.doorbell import Doorbell, DoorbellDetail
|
||||||
|
from yalexs.keypad import KeypadDetail
|
||||||
from yalexs.lock import Lock, LockDetail
|
from yalexs.lock import Lock, LockDetail
|
||||||
from yalexs.util import get_configuration_url
|
from yalexs.util import get_configuration_url
|
||||||
|
|
||||||
@ -10,7 +11,7 @@ from homeassistant.const import ATTR_CONNECTIONS
|
|||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
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 . import DOMAIN, AugustData
|
||||||
from .const import MANUFACTURER
|
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:
|
def _remove_device_types(name: str, device_types: list[str]) -> str:
|
||||||
"""Strip device types from a string.
|
"""Strip device types from a string.
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ from typing import Any, Generic, TypeVar, cast
|
|||||||
from yalexs.activity import ActivityType, LockOperationActivity
|
from yalexs.activity import ActivityType, LockOperationActivity
|
||||||
from yalexs.doorbell import Doorbell
|
from yalexs.doorbell import Doorbell
|
||||||
from yalexs.keypad import KeypadDetail
|
from yalexs.keypad import KeypadDetail
|
||||||
from yalexs.lock import Lock, LockDetail
|
from yalexs.lock import LockDetail
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
RestoreSensor,
|
RestoreSensor,
|
||||||
@ -42,7 +42,7 @@ from .const import (
|
|||||||
OPERATION_METHOD_REMOTE,
|
OPERATION_METHOD_REMOTE,
|
||||||
OPERATION_METHOD_TAG,
|
OPERATION_METHOD_TAG,
|
||||||
)
|
)
|
||||||
from .entity import AugustEntityMixin
|
from .entity import AugustDescriptionEntity, AugustEntityMixin
|
||||||
|
|
||||||
|
|
||||||
def _retrieve_device_battery_state(detail: LockDetail) -> int:
|
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]
|
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."""
|
"""Representation of an August sensor."""
|
||||||
|
|
||||||
entity_description: AugustSensorEntityDescription[_T]
|
entity_description: AugustSensorEntityDescription[_T]
|
||||||
_attr_device_class = SensorDeviceClass.BATTERY
|
_attr_device_class = SensorDeviceClass.BATTERY
|
||||||
_attr_native_unit_of_measurement = PERCENTAGE
|
_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
|
@callback
|
||||||
def _update_from_data(self) -> None:
|
def _update_from_data(self) -> None:
|
||||||
"""Get the latest state of the sensor."""
|
"""Get the latest state of the sensor."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user