From 3d45ced02e95fd514880ec42eba7626fffca002f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 18 Jun 2024 16:03:46 -0500 Subject: [PATCH] Cleanup code to add august sensors (#119929) --- .../components/august/binary_sensor.py | 40 ++++-------- homeassistant/components/august/sensor.py | 62 +++++-------------- 2 files changed, 29 insertions(+), 73 deletions(-) diff --git a/homeassistant/components/august/binary_sensor.py b/homeassistant/components/august/binary_sensor.py index 81d84965d58..27371f5e3f6 100644 --- a/homeassistant/components/august/binary_sensor.py +++ b/homeassistant/components/august/binary_sensor.py @@ -160,38 +160,22 @@ async def async_setup_entry( data = config_entry.runtime_data entities: list[BinarySensorEntity] = [] - for door in data.locks: - detail = data.get_device_detail(door.device_id) - if not detail.doorsense: - _LOGGER.debug( - ( - "Not adding sensor class door for lock %s because it does not have" - " doorsense" - ), - door.device_name, - ) - continue - - _LOGGER.debug("Adding sensor class door for %s", door.device_name) - entities.append(AugustDoorBinarySensor(data, door, SENSOR_TYPE_DOOR)) + for lock in data.locks: + detail = data.get_device_detail(lock.device_id) + if detail.doorsense: + entities.append(AugustDoorBinarySensor(data, lock, SENSOR_TYPE_DOOR)) if detail.doorbell: - for description in SENSOR_TYPES_DOORBELL: - _LOGGER.debug( - "Adding doorbell sensor class %s for %s", - description.device_class, - door.device_name, - ) - entities.append(AugustDoorbellBinarySensor(data, door, description)) + entities.extend( + AugustDoorbellBinarySensor(data, lock, description) + for description in SENSOR_TYPES_DOORBELL + ) for doorbell in data.doorbells: - for description in SENSOR_TYPES_DOORBELL + SENSOR_TYPES_VIDEO_DOORBELL: - _LOGGER.debug( - "Adding doorbell sensor class %s for %s", - description.device_class, - doorbell.device_name, - ) - entities.append(AugustDoorbellBinarySensor(data, doorbell, description)) + entities.extend( + AugustDoorbellBinarySensor(data, doorbell, description) + for description in SENSOR_TYPES_DOORBELL + SENSOR_TYPES_VIDEO_DOORBELL + ) async_add_entities(entities) diff --git a/homeassistant/components/august/sensor.py b/homeassistant/components/august/sensor.py index 8ad32df3c08..e5d29bb23d8 100644 --- a/homeassistant/components/august/sensor.py +++ b/homeassistant/components/august/sensor.py @@ -4,7 +4,6 @@ from __future__ import annotations from collections.abc import Callable from dataclasses import dataclass -import logging from typing import Any, Generic, TypeVar, cast from yalexs.activity import ActivityType, LockOperationActivity @@ -45,8 +44,6 @@ from .const import ( ) from .entity import AugustEntityMixin -_LOGGER = logging.getLogger(__name__) - def _retrieve_device_battery_state(detail: LockDetail) -> int: """Get the latest state of the sensor.""" @@ -98,53 +95,28 @@ async def async_setup_entry( """Set up the August sensors.""" data = config_entry.runtime_data entities: list[SensorEntity] = [] - operation_sensors = [] - batteries: dict[str, list[Doorbell | Lock]] = { - "device_battery": [], - "linked_keypad_battery": [], - } - for device in data.doorbells: - batteries["device_battery"].append(device) + for device in data.locks: - batteries["device_battery"].append(device) - batteries["linked_keypad_battery"].append(device) - operation_sensors.append(device) - - for device in batteries["device_battery"]: detail = data.get_device_detail(device.device_id) - if detail is None or SENSOR_TYPE_DEVICE_BATTERY.value_fn(detail) is None: - _LOGGER.debug( - "Not adding battery sensor for %s because it is not present", - device.device_name, + entities.append(AugustOperatorSensor(data, device)) + if SENSOR_TYPE_DEVICE_BATTERY.value_fn(detail): + entities.append( + AugustBatterySensor[LockDetail]( + data, device, SENSOR_TYPE_DEVICE_BATTERY + ) ) - continue - _LOGGER.debug( - "Adding battery sensor for %s", - device.device_name, - ) - entities.append( - AugustBatterySensor[LockDetail](data, device, SENSOR_TYPE_DEVICE_BATTERY) - ) - - for device in batteries["linked_keypad_battery"]: - detail = data.get_device_detail(device.device_id) - - if detail.keypad is None: - _LOGGER.debug( - "Not adding keypad battery sensor for %s because it is not present", - device.device_name, + if keypad := detail.keypad: + entities.append( + AugustBatterySensor[KeypadDetail]( + data, keypad, SENSOR_TYPE_KEYPAD_BATTERY + ) ) - continue - _LOGGER.debug( - "Adding keypad battery sensor for %s", - device.device_name, - ) - keypad_battery_sensor = AugustBatterySensor[KeypadDetail]( - data, detail.keypad, SENSOR_TYPE_KEYPAD_BATTERY - ) - entities.append(keypad_battery_sensor) - entities.extend(AugustOperatorSensor(data, device) for device in operation_sensors) + entities.extend( + AugustBatterySensor[Doorbell](data, device, SENSOR_TYPE_DEVICE_BATTERY) + for device in data.doorbells + if SENSOR_TYPE_DEVICE_BATTERY.value_fn(data.get_device_detail(device.device_id)) + ) async_add_entities(entities)