Reduce code needed to set august unique ids (#119942)

* Reduce code needed to set august unique ids

Set the unique ids in the base class since they always
use the same format

* Reduce code needed to set august unique ids

Set the unique ids in the base class since they always
use the same format
This commit is contained in:
J. Nick Koston 2024-06-18 20:43:06 -05:00 committed by GitHub
parent f88b24f8a4
commit b11801df50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 12 additions and 30 deletions

View File

@ -1,12 +1,10 @@
"""Support for August buttons.""" """Support for August buttons."""
from yalexs.lock import Lock
from homeassistant.components.button import ButtonEntity from homeassistant.components.button import ButtonEntity
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import AugustConfigEntry, AugustData from . import AugustConfigEntry
from .entity import AugustEntityMixin from .entity import AugustEntityMixin
@ -17,7 +15,7 @@ async def async_setup_entry(
) -> None: ) -> None:
"""Set up August lock wake buttons.""" """Set up August lock wake buttons."""
data = config_entry.runtime_data data = config_entry.runtime_data
async_add_entities(AugustWakeLockButton(data, lock) for lock in data.locks) async_add_entities(AugustWakeLockButton(data, lock, "wake") for lock in data.locks)
class AugustWakeLockButton(AugustEntityMixin, ButtonEntity): class AugustWakeLockButton(AugustEntityMixin, ButtonEntity):
@ -25,11 +23,6 @@ class AugustWakeLockButton(AugustEntityMixin, ButtonEntity):
_attr_translation_key = "wake" _attr_translation_key = "wake"
def __init__(self, data: AugustData, device: Lock) -> None:
"""Initialize the lock wake button."""
super().__init__(data, device)
self._attr_unique_id = f"{self._device_id}_wake"
async def async_press(self) -> None: async def async_press(self) -> None:
"""Wake the device.""" """Wake the device."""
await self._data.async_status_async(self._device_id, self._hyper_bridge) await self._data.async_status_async(self._device_id, self._hyper_bridge)

View File

@ -51,10 +51,9 @@ class AugustCamera(AugustEntityMixin, Camera):
self, data: AugustData, device: Doorbell, session: ClientSession, timeout: int self, data: AugustData, device: Doorbell, session: ClientSession, timeout: int
) -> None: ) -> None:
"""Initialize an August security camera.""" """Initialize an August security camera."""
super().__init__(data, device) super().__init__(data, device, "camera")
self._timeout = timeout self._timeout = timeout
self._session = session self._session = session
self._attr_unique_id = f"{self._device_id:s}_camera"
@property @property
def is_recording(self) -> bool: def is_recording(self) -> bool:

View File

@ -25,12 +25,15 @@ class AugustEntityMixin(Entity):
_attr_should_poll = False _attr_should_poll = False
_attr_has_entity_name = True _attr_has_entity_name = True
def __init__(self, data: AugustData, device: Doorbell | Lock) -> None: def __init__(
self, data: AugustData, device: Doorbell | Lock | KeypadDetail, unique_id: str
) -> None:
"""Initialize an August device.""" """Initialize an August device."""
super().__init__() super().__init__()
self._data = data self._data = data
self._device = device self._device = device
detail = self._detail detail = self._detail
self._attr_unique_id = f"{device.device_id}_{unique_id}"
self._attr_device_info = DeviceInfo( self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._device_id)}, identifiers={(DOMAIN, self._device_id)},
manufacturer=MANUFACTURER, manufacturer=MANUFACTURER,
@ -77,6 +80,7 @@ class AugustEntityMixin(Entity):
self._device_id, self._update_from_data_and_write_state self._device_id, self._update_from_data_and_write_state
) )
) )
self._update_from_data()
class AugustDescriptionEntity(AugustEntityMixin): class AugustDescriptionEntity(AugustEntityMixin):
@ -89,14 +93,8 @@ class AugustDescriptionEntity(AugustEntityMixin):
description: EntityDescription, description: EntityDescription,
) -> None: ) -> None:
"""Initialize an August entity with a description.""" """Initialize an August entity with a description."""
super().__init__(data, device) super().__init__(data, device, description.key)
self.entity_description = description 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:

View File

@ -44,11 +44,9 @@ class AugustLock(AugustEntityMixin, RestoreEntity, LockEntity):
def __init__(self, data: AugustData, device: Lock) -> None: def __init__(self, data: AugustData, device: Lock) -> None:
"""Initialize the lock.""" """Initialize the lock."""
super().__init__(data, device) super().__init__(data, device, "lock")
self._attr_unique_id = f"{self._device_id:s}_lock"
if self._detail.unlatch_supported: if self._detail.unlatch_supported:
self._attr_supported_features = LockEntityFeature.OPEN self._attr_supported_features = LockEntityFeature.OPEN
self._update_from_data()
async def async_lock(self, **kwargs: Any) -> None: async def async_lock(self, **kwargs: Any) -> None:
"""Lock the device.""" """Lock the device."""

View File

@ -27,7 +27,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import AugustConfigEntry, AugustData from . import AugustConfigEntry
from .const import ( from .const import (
ATTR_OPERATION_AUTORELOCK, ATTR_OPERATION_AUTORELOCK,
ATTR_OPERATION_KEYPAD, ATTR_OPERATION_KEYPAD,
@ -91,7 +91,7 @@ async def async_setup_entry(
for device in data.locks: for device in data.locks:
detail = data.get_device_detail(device.device_id) detail = data.get_device_detail(device.device_id)
entities.append(AugustOperatorSensor(data, device)) entities.append(AugustOperatorSensor(data, device, "lock_operator"))
if SENSOR_TYPE_DEVICE_BATTERY.value_fn(detail): if SENSOR_TYPE_DEVICE_BATTERY.value_fn(detail):
entities.append( entities.append(
AugustBatterySensor[LockDetail]( AugustBatterySensor[LockDetail](
@ -124,12 +124,6 @@ class AugustOperatorSensor(AugustEntityMixin, RestoreSensor):
_operated_tag: bool | None = None _operated_tag: bool | None = None
_operated_autorelock: bool | None = None _operated_autorelock: bool | None = None
def __init__(self, data: AugustData, device) -> None:
"""Initialize the sensor."""
super().__init__(data, device)
self._attr_unique_id = f"{self._device_id}_lock_operator"
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 and update activity.""" """Get the latest state of the sensor and update activity."""