From b11801df50fd164cb6a0825dc71a0ec4e5eb0afe Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 18 Jun 2024 20:43:06 -0500 Subject: [PATCH] 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 --- homeassistant/components/august/button.py | 11 ++--------- homeassistant/components/august/camera.py | 3 +-- homeassistant/components/august/entity.py | 14 ++++++-------- homeassistant/components/august/lock.py | 4 +--- homeassistant/components/august/sensor.py | 10 ++-------- 5 files changed, 12 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/august/button.py b/homeassistant/components/august/button.py index d7aefca5d3c..406475db601 100644 --- a/homeassistant/components/august/button.py +++ b/homeassistant/components/august/button.py @@ -1,12 +1,10 @@ """Support for August buttons.""" -from yalexs.lock import Lock - from homeassistant.components.button import ButtonEntity from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import AugustConfigEntry, AugustData +from . import AugustConfigEntry from .entity import AugustEntityMixin @@ -17,7 +15,7 @@ async def async_setup_entry( ) -> None: """Set up August lock wake buttons.""" 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): @@ -25,11 +23,6 @@ class AugustWakeLockButton(AugustEntityMixin, ButtonEntity): _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: """Wake the device.""" await self._data.async_status_async(self._device_id, self._hyper_bridge) diff --git a/homeassistant/components/august/camera.py b/homeassistant/components/august/camera.py index f8541388ec2..ba29b2905d3 100644 --- a/homeassistant/components/august/camera.py +++ b/homeassistant/components/august/camera.py @@ -51,10 +51,9 @@ class AugustCamera(AugustEntityMixin, Camera): self, data: AugustData, device: Doorbell, session: ClientSession, timeout: int ) -> None: """Initialize an August security camera.""" - super().__init__(data, device) + super().__init__(data, device, "camera") self._timeout = timeout self._session = session - self._attr_unique_id = f"{self._device_id:s}_camera" @property def is_recording(self) -> bool: diff --git a/homeassistant/components/august/entity.py b/homeassistant/components/august/entity.py index a13a319b568..960dddbc005 100644 --- a/homeassistant/components/august/entity.py +++ b/homeassistant/components/august/entity.py @@ -25,12 +25,15 @@ class AugustEntityMixin(Entity): _attr_should_poll = False _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.""" super().__init__() self._data = data self._device = device detail = self._detail + self._attr_unique_id = f"{device.device_id}_{unique_id}" self._attr_device_info = DeviceInfo( identifiers={(DOMAIN, self._device_id)}, manufacturer=MANUFACTURER, @@ -77,6 +80,7 @@ class AugustEntityMixin(Entity): self._device_id, self._update_from_data_and_write_state ) ) + self._update_from_data() class AugustDescriptionEntity(AugustEntityMixin): @@ -89,14 +93,8 @@ class AugustDescriptionEntity(AugustEntityMixin): description: EntityDescription, ) -> None: """Initialize an August entity with a description.""" - super().__init__(data, device) + super().__init__(data, device, description.key) 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: diff --git a/homeassistant/components/august/lock.py b/homeassistant/components/august/lock.py index 1e1664018b4..10d32ebd323 100644 --- a/homeassistant/components/august/lock.py +++ b/homeassistant/components/august/lock.py @@ -44,11 +44,9 @@ class AugustLock(AugustEntityMixin, RestoreEntity, LockEntity): def __init__(self, data: AugustData, device: Lock) -> None: """Initialize the lock.""" - super().__init__(data, device) - self._attr_unique_id = f"{self._device_id:s}_lock" + super().__init__(data, device, "lock") if self._detail.unlatch_supported: self._attr_supported_features = LockEntityFeature.OPEN - self._update_from_data() async def async_lock(self, **kwargs: Any) -> None: """Lock the device.""" diff --git a/homeassistant/components/august/sensor.py b/homeassistant/components/august/sensor.py index 59f4d0cf33f..847d7f32a5a 100644 --- a/homeassistant/components/august/sensor.py +++ b/homeassistant/components/august/sensor.py @@ -27,7 +27,7 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import AugustConfigEntry, AugustData +from . import AugustConfigEntry from .const import ( ATTR_OPERATION_AUTORELOCK, ATTR_OPERATION_KEYPAD, @@ -91,7 +91,7 @@ async def async_setup_entry( for device in data.locks: 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): entities.append( AugustBatterySensor[LockDetail]( @@ -124,12 +124,6 @@ class AugustOperatorSensor(AugustEntityMixin, RestoreSensor): _operated_tag: 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 def _update_from_data(self) -> None: """Get the latest state of the sensor and update activity."""