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
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)

View file

@ -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)