diff --git a/homeassistant/components/husqvarna_automower/number.py b/homeassistant/components/husqvarna_automower/number.py index 8745b93479d..e2e617b427b 100644 --- a/homeassistant/components/husqvarna_automower/number.py +++ b/homeassistant/components/husqvarna_automower/number.py @@ -3,7 +3,7 @@ from collections.abc import Awaitable, Callable from dataclasses import dataclass import logging -from typing import Any +from typing import TYPE_CHECKING, Any from aioautomower.exceptions import ApiException from aioautomower.model import MowerAttributes @@ -12,7 +12,7 @@ from aioautomower.session import AutomowerSession from homeassistant.components.number import NumberEntity, NumberEntityDescription from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -32,6 +32,15 @@ class AutomowerNumberEntityDescription(NumberEntityDescription): set_value_fn: Callable[[AutomowerSession, str, float], Awaitable[Any]] +@callback +def _async_get_cutting_height(data: MowerAttributes) -> int: + """Return the cutting height.""" + if TYPE_CHECKING: + # Sensor does not get created if it is None + assert data.cutting_height is not None + return data.cutting_height + + NUMBER_TYPES: tuple[AutomowerNumberEntityDescription, ...] = ( AutomowerNumberEntityDescription( key="cutting_height", @@ -41,7 +50,7 @@ NUMBER_TYPES: tuple[AutomowerNumberEntityDescription, ...] = ( native_min_value=1, native_max_value=9, exists_fn=lambda data: data.cutting_height is not None, - value_fn=lambda data: data.cutting_height, + value_fn=_async_get_cutting_height, set_value_fn=lambda session, mower_id, cheight: session.set_cutting_height( mower_id, int(cheight) ),