2021-06-29 12:33:04 +00:00
|
|
|
"""Support for esphome numbers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import math
|
|
|
|
|
2021-11-30 19:19:14 +00:00
|
|
|
from aioesphomeapi import NumberInfo, NumberMode as EsphomeNumberMode, NumberState
|
2021-06-29 12:33:04 +00:00
|
|
|
|
2021-11-30 19:19:14 +00:00
|
|
|
from homeassistant.components.number import NumberEntity, NumberMode
|
2021-06-29 12:33:04 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2021-11-30 19:19:14 +00:00
|
|
|
from . import (
|
|
|
|
EsphomeEntity,
|
|
|
|
EsphomeEnumMapper,
|
|
|
|
esphome_state_property,
|
|
|
|
platform_async_setup_entry,
|
|
|
|
)
|
2021-06-29 12:33:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up esphome numbers based on a config entry."""
|
|
|
|
await platform_async_setup_entry(
|
|
|
|
hass,
|
|
|
|
entry,
|
|
|
|
async_add_entities,
|
|
|
|
component_key="number",
|
|
|
|
info_type=NumberInfo,
|
|
|
|
entity_type=EsphomeNumber,
|
|
|
|
state_type=NumberState,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-11-30 19:19:14 +00:00
|
|
|
NUMBER_MODES: EsphomeEnumMapper[EsphomeNumberMode, NumberMode] = EsphomeEnumMapper(
|
|
|
|
{
|
|
|
|
EsphomeNumberMode.AUTO: NumberMode.AUTO,
|
|
|
|
EsphomeNumberMode.BOX: NumberMode.BOX,
|
|
|
|
EsphomeNumberMode.SLIDER: NumberMode.SLIDER,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-06-29 12:33:04 +00:00
|
|
|
# https://github.com/PyCQA/pylint/issues/3150 for all @esphome_state_property
|
2021-07-15 04:44:57 +00:00
|
|
|
# pylint: disable=invalid-overridden-method
|
2021-06-29 12:33:04 +00:00
|
|
|
|
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
class EsphomeNumber(EsphomeEntity[NumberInfo, NumberState], NumberEntity):
|
2021-06-29 12:33:04 +00:00
|
|
|
"""A number implementation for esphome."""
|
|
|
|
|
|
|
|
@property
|
2022-06-14 18:16:36 +00:00
|
|
|
def native_min_value(self) -> float:
|
2021-06-29 12:33:04 +00:00
|
|
|
"""Return the minimum value."""
|
|
|
|
return super()._static_info.min_value
|
|
|
|
|
|
|
|
@property
|
2022-06-14 18:16:36 +00:00
|
|
|
def native_max_value(self) -> float:
|
2021-06-29 12:33:04 +00:00
|
|
|
"""Return the maximum value."""
|
|
|
|
return super()._static_info.max_value
|
|
|
|
|
|
|
|
@property
|
2022-06-14 18:16:36 +00:00
|
|
|
def native_step(self) -> float:
|
2021-06-29 12:33:04 +00:00
|
|
|
"""Return the increment/decrement step."""
|
|
|
|
return super()._static_info.step
|
|
|
|
|
2021-11-30 14:20:40 +00:00
|
|
|
@property
|
2022-06-14 18:16:36 +00:00
|
|
|
def native_unit_of_measurement(self) -> str | None:
|
2021-11-30 14:20:40 +00:00
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return super()._static_info.unit_of_measurement
|
|
|
|
|
2021-11-30 19:19:14 +00:00
|
|
|
@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
|
|
|
|
|
2021-06-29 12:33:04 +00:00
|
|
|
@esphome_state_property
|
2022-06-14 18:16:36 +00:00
|
|
|
def native_value(self) -> float | None:
|
2021-06-29 12:33:04 +00:00
|
|
|
"""Return the state of the entity."""
|
|
|
|
if math.isnan(self._state.state):
|
|
|
|
return None
|
|
|
|
if self._state.missing_state:
|
|
|
|
return None
|
|
|
|
return self._state.state
|
|
|
|
|
2022-06-14 18:16:36 +00:00
|
|
|
async def async_set_native_value(self, value: float) -> None:
|
2021-06-29 12:33:04 +00:00
|
|
|
"""Update the current value."""
|
|
|
|
await self._client.number_command(self._static_info.key, value)
|