Add number entities to ESPHome (#52241)
Co-authored-by: Franck Nijhof <git@frenck.dev>pull/52298/head^2
parent
a639cb7ba7
commit
e1797ea670
homeassistant/components/esphome
|
@ -274,6 +274,7 @@ omit =
|
|||
homeassistant/components/esphome/entry_data.py
|
||||
homeassistant/components/esphome/fan.py
|
||||
homeassistant/components/esphome/light.py
|
||||
homeassistant/components/esphome/number.py
|
||||
homeassistant/components/esphome/sensor.py
|
||||
homeassistant/components/esphome/switch.py
|
||||
homeassistant/components/essent/sensor.py
|
||||
|
|
|
@ -16,6 +16,7 @@ from aioesphomeapi import (
|
|||
EntityState,
|
||||
FanInfo,
|
||||
LightInfo,
|
||||
NumberInfo,
|
||||
SensorInfo,
|
||||
SwitchInfo,
|
||||
TextSensorInfo,
|
||||
|
@ -41,6 +42,7 @@ INFO_TYPE_TO_PLATFORM = {
|
|||
CoverInfo: "cover",
|
||||
FanInfo: "fan",
|
||||
LightInfo: "light",
|
||||
NumberInfo: "number",
|
||||
SensorInfo: "sensor",
|
||||
SwitchInfo: "switch",
|
||||
TextSensorInfo: "sensor",
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
"name": "ESPHome",
|
||||
"config_flow": true,
|
||||
"documentation": "https://www.home-assistant.io/integrations/esphome",
|
||||
"requirements": ["aioesphomeapi==3.0.1"],
|
||||
"requirements": ["aioesphomeapi==3.1.0"],
|
||||
"zeroconf": ["_esphomelib._tcp.local."],
|
||||
"codeowners": ["@OttoWinter", "@jesserockz"],
|
||||
"after_dependencies": ["zeroconf", "tag"],
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
"""Support for esphome numbers."""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
from aioesphomeapi import NumberInfo, NumberState
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.number import NumberEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
||||
|
||||
ICON_SCHEMA = vol.Schema(cv.icon)
|
||||
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
# https://github.com/PyCQA/pylint/issues/3150 for all @esphome_state_property
|
||||
# pylint: disable=invalid-overridden-method
|
||||
|
||||
|
||||
class EsphomeNumber(EsphomeEntity, NumberEntity):
|
||||
"""A number implementation for esphome."""
|
||||
|
||||
@property
|
||||
def _static_info(self) -> NumberInfo:
|
||||
return super()._static_info
|
||||
|
||||
@property
|
||||
def _state(self) -> NumberState | None:
|
||||
return super()._state
|
||||
|
||||
@property
|
||||
def icon(self) -> str | None:
|
||||
"""Return the icon."""
|
||||
if not self._static_info.icon:
|
||||
return None
|
||||
return ICON_SCHEMA(self._static_info.icon)
|
||||
|
||||
@property
|
||||
def min_value(self) -> float:
|
||||
"""Return the minimum value."""
|
||||
return super()._static_info.min_value
|
||||
|
||||
@property
|
||||
def max_value(self) -> float:
|
||||
"""Return the maximum value."""
|
||||
return super()._static_info.max_value
|
||||
|
||||
@property
|
||||
def step(self) -> float:
|
||||
"""Return the increment/decrement step."""
|
||||
return super()._static_info.step
|
||||
|
||||
@esphome_state_property
|
||||
def value(self) -> float:
|
||||
"""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
|
||||
|
||||
async def async_set_value(self, value: float) -> None:
|
||||
"""Update the current value."""
|
||||
await self._client.number_command(self._static_info.key, value)
|
|
@ -160,7 +160,7 @@ aioeafm==0.1.2
|
|||
aioemonitor==1.0.5
|
||||
|
||||
# homeassistant.components.esphome
|
||||
aioesphomeapi==3.0.1
|
||||
aioesphomeapi==3.1.0
|
||||
|
||||
# homeassistant.components.flo
|
||||
aioflo==0.4.1
|
||||
|
|
|
@ -100,7 +100,7 @@ aioeafm==0.1.2
|
|||
aioemonitor==1.0.5
|
||||
|
||||
# homeassistant.components.esphome
|
||||
aioesphomeapi==3.0.1
|
||||
aioesphomeapi==3.1.0
|
||||
|
||||
# homeassistant.components.flo
|
||||
aioflo==0.4.1
|
||||
|
|
Loading…
Reference in New Issue