Migrate esphome number platform to use _on_static_info_update (#94958)

This commit is contained in:
J. Nick Koston 2023-06-21 13:13:29 +02:00 committed by GitHub
parent 9876abcac9
commit 9d91cfa27f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,11 +3,16 @@ from __future__ import annotations
import math import math
from aioesphomeapi import NumberInfo, NumberMode as EsphomeNumberMode, NumberState from aioesphomeapi import (
EntityInfo,
NumberInfo,
NumberMode as EsphomeNumberMode,
NumberState,
)
from homeassistant.components.number import NumberDeviceClass, NumberEntity, NumberMode from homeassistant.components.number import NumberDeviceClass, NumberEntity, NumberMode
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.enum import try_parse_enum from homeassistant.util.enum import try_parse_enum
@ -44,48 +49,34 @@ NUMBER_MODES: EsphomeEnumMapper[EsphomeNumberMode, NumberMode] = EsphomeEnumMapp
class EsphomeNumber(EsphomeEntity[NumberInfo, NumberState], NumberEntity): class EsphomeNumber(EsphomeEntity[NumberInfo, NumberState], NumberEntity):
"""A number implementation for esphome.""" """A number implementation for esphome."""
@property @callback
def device_class(self) -> NumberDeviceClass | None: def _on_static_info_update(self, static_info: EntityInfo) -> None:
"""Return the class of this entity.""" """Set attrs from static info."""
return try_parse_enum(NumberDeviceClass, self._static_info.device_class) super()._on_static_info_update(static_info)
static_info = self._static_info
@property self._attr_device_class = try_parse_enum(
def native_min_value(self) -> float: NumberDeviceClass, self._static_info.device_class
"""Return the minimum value.""" )
return self._static_info.min_value self._attr_native_min_value = static_info.min_value
self._attr_native_max_value = static_info.max_value
@property self._attr_native_step = static_info.step
def native_max_value(self) -> float: self._attr_native_unit_of_measurement = static_info.unit_of_measurement
"""Return the maximum value.""" if mode := static_info.mode:
return self._static_info.max_value self._attr_mode = NUMBER_MODES.from_esphome(mode)
else:
@property self._attr_mode = NumberMode.AUTO
def native_step(self) -> float:
"""Return the increment/decrement step."""
return self._static_info.step
@property
def native_unit_of_measurement(self) -> str | None:
"""Return the unit of measurement."""
return self._static_info.unit_of_measurement
@property
def mode(self) -> NumberMode:
"""Return the mode of the entity."""
if self._static_info.mode:
return NUMBER_MODES.from_esphome(self._static_info.mode)
return NumberMode.AUTO
@property @property
@esphome_state_property @esphome_state_property
def native_value(self) -> float | None: def native_value(self) -> float | None:
"""Return the state of the entity.""" """Return the state of the entity."""
if math.isnan(self._state.state): state = self._state
if math.isnan(state.state):
return None return None
if self._state.missing_state: if state.missing_state:
return None return None
return self._state.state return state.state
async def async_set_native_value(self, value: float) -> None: async def async_set_native_value(self, value: float) -> None:
"""Update the current value.""" """Update the current value."""
await self._client.number_command(self._static_info.key, value) await self._client.number_command(self._key, value)