Cleanup code to add august sensors (#119929)

This commit is contained in:
J. Nick Koston 2024-06-18 16:03:46 -05:00 committed by GitHub
parent b8cafe7e5e
commit 3d45ced02e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 73 deletions

View File

@ -160,38 +160,22 @@ async def async_setup_entry(
data = config_entry.runtime_data data = config_entry.runtime_data
entities: list[BinarySensorEntity] = [] entities: list[BinarySensorEntity] = []
for door in data.locks: for lock in data.locks:
detail = data.get_device_detail(door.device_id) detail = data.get_device_detail(lock.device_id)
if not detail.doorsense: if detail.doorsense:
_LOGGER.debug( entities.append(AugustDoorBinarySensor(data, lock, SENSOR_TYPE_DOOR))
(
"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))
if detail.doorbell: if detail.doorbell:
for description in SENSOR_TYPES_DOORBELL: entities.extend(
_LOGGER.debug( AugustDoorbellBinarySensor(data, lock, description)
"Adding doorbell sensor class %s for %s", for description in SENSOR_TYPES_DOORBELL
description.device_class, )
door.device_name,
)
entities.append(AugustDoorbellBinarySensor(data, door, description))
for doorbell in data.doorbells: for doorbell in data.doorbells:
for description in SENSOR_TYPES_DOORBELL + SENSOR_TYPES_VIDEO_DOORBELL: entities.extend(
_LOGGER.debug( AugustDoorbellBinarySensor(data, doorbell, description)
"Adding doorbell sensor class %s for %s", for description in SENSOR_TYPES_DOORBELL + SENSOR_TYPES_VIDEO_DOORBELL
description.device_class, )
doorbell.device_name,
)
entities.append(AugustDoorbellBinarySensor(data, doorbell, description))
async_add_entities(entities) async_add_entities(entities)

View File

@ -4,7 +4,6 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
import logging
from typing import Any, Generic, TypeVar, cast from typing import Any, Generic, TypeVar, cast
from yalexs.activity import ActivityType, LockOperationActivity from yalexs.activity import ActivityType, LockOperationActivity
@ -45,8 +44,6 @@ from .const import (
) )
from .entity import AugustEntityMixin from .entity import AugustEntityMixin
_LOGGER = logging.getLogger(__name__)
def _retrieve_device_battery_state(detail: LockDetail) -> int: def _retrieve_device_battery_state(detail: LockDetail) -> int:
"""Get the latest state of the sensor.""" """Get the latest state of the sensor."""
@ -98,53 +95,28 @@ async def async_setup_entry(
"""Set up the August sensors.""" """Set up the August sensors."""
data = config_entry.runtime_data data = config_entry.runtime_data
entities: list[SensorEntity] = [] 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: 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) detail = data.get_device_detail(device.device_id)
if detail is None or SENSOR_TYPE_DEVICE_BATTERY.value_fn(detail) is None: entities.append(AugustOperatorSensor(data, device))
_LOGGER.debug( if SENSOR_TYPE_DEVICE_BATTERY.value_fn(detail):
"Not adding battery sensor for %s because it is not present", entities.append(
device.device_name, AugustBatterySensor[LockDetail](
data, device, SENSOR_TYPE_DEVICE_BATTERY
)
) )
continue if keypad := detail.keypad:
_LOGGER.debug( entities.append(
"Adding battery sensor for %s", AugustBatterySensor[KeypadDetail](
device.device_name, data, keypad, SENSOR_TYPE_KEYPAD_BATTERY
) )
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,
) )
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) async_add_entities(entities)