Add work area sensor for Husqvarna Automower (#119704)

* Add work area sensor to Husqvarna Automower

* fix exist_fn
This commit is contained in:
Thomas55555 2024-06-14 21:34:47 +02:00 committed by GitHub
parent c0ff2d866f
commit c2e31e9846
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 102 additions and 4 deletions

View file

@ -4,6 +4,7 @@ from collections.abc import Callable
from dataclasses import dataclass
from datetime import datetime
import logging
from typing import TYPE_CHECKING
from aioautomower.model import MowerAttributes, MowerModes, RestrictedReasons
@ -14,7 +15,7 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfLength, UnitOfTime
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
@ -184,11 +185,31 @@ RESTRICTED_REASONS: list = [
]
@callback
def _get_work_area_names(data: MowerAttributes) -> list[str]:
"""Return a list with all work area names."""
if TYPE_CHECKING:
# Sensor does not get created if it is None
assert data.work_areas is not None
return [data.work_areas[work_area_id].name for work_area_id in data.work_areas]
@callback
def _get_current_work_area_name(data: MowerAttributes) -> str:
"""Return the name of the current work area."""
if TYPE_CHECKING:
# Sensor does not get created if values are None
assert data.work_areas is not None
assert data.mower.work_area_id is not None
return data.work_areas[data.mower.work_area_id].name
@dataclass(frozen=True, kw_only=True)
class AutomowerSensorEntityDescription(SensorEntityDescription):
"""Describes Automower sensor entity."""
exists_fn: Callable[[MowerAttributes], bool] = lambda _: True
option_fn: Callable[[MowerAttributes], list[str] | None] = lambda _: None
value_fn: Callable[[MowerAttributes], StateType | datetime]
@ -204,7 +225,7 @@ SENSOR_TYPES: tuple[AutomowerSensorEntityDescription, ...] = (
key="mode",
translation_key="mode",
device_class=SensorDeviceClass.ENUM,
options=[option.lower() for option in list(MowerModes)],
option_fn=lambda data: [option.lower() for option in list(MowerModes)],
value_fn=(
lambda data: data.mower.mode.lower()
if data.mower.mode != MowerModes.UNKNOWN
@ -302,18 +323,26 @@ SENSOR_TYPES: tuple[AutomowerSensorEntityDescription, ...] = (
key="error",
translation_key="error",
device_class=SensorDeviceClass.ENUM,
option_fn=lambda data: ERROR_KEY_LIST,
value_fn=lambda data: (
"no_error" if data.mower.error_key is None else data.mower.error_key
),
options=ERROR_KEY_LIST,
),
AutomowerSensorEntityDescription(
key="restricted_reason",
translation_key="restricted_reason",
device_class=SensorDeviceClass.ENUM,
options=RESTRICTED_REASONS,
option_fn=lambda data: RESTRICTED_REASONS,
value_fn=lambda data: data.planner.restricted_reason.lower(),
),
AutomowerSensorEntityDescription(
key="work_area",
translation_key="work_area",
device_class=SensorDeviceClass.ENUM,
exists_fn=lambda data: data.capabilities.work_areas,
option_fn=_get_work_area_names,
value_fn=_get_current_work_area_name,
),
)
@ -352,3 +381,8 @@ class AutomowerSensorEntity(AutomowerBaseEntity, SensorEntity):
def native_value(self) -> StateType | datetime:
"""Return the state of the sensor."""
return self.entity_description.value_fn(self.mower_attributes)
@property
def options(self) -> list[str] | None:
"""Return the option of the sensor."""
return self.entity_description.option_fn(self.mower_attributes)