Fix NumberEntity HA changes and add step control

This commit is contained in:
sibowler
2022-07-14 06:29:56 +10:00
parent 364569bad8
commit 0fe550576a
2 changed files with 28 additions and 10 deletions

View File

@@ -5,7 +5,6 @@ from functools import partial
import voluptuous as vol
from homeassistant.components.number import DOMAIN, NumberEntity
from homeassistant.const import CONF_DEVICE_CLASS, STATE_UNKNOWN
from homeassistant.helpers.restore_state import RestoreEntity
from .common import LocalTuyaEntity, async_setup_entry
@@ -17,10 +16,12 @@ from .const import (
CONF_MAX_VALUE,
CONF_DEFAULT_VALUE,
CONF_RESTORE_ON_RECONNECT,
CONF_STEPSIZE_VALUE,
)
DEFAULT_MIN = 0
DEFAULT_MAX = 100000
DEFAULT_STEP = 1.0
def flow_schema(dps):
@@ -34,12 +35,16 @@ def flow_schema(dps):
vol.Coerce(float),
vol.Range(min=-1000000.0, max=1000000.0),
),
vol.Required(CONF_STEPSIZE_VALUE, default=DEFAULT_STEP): vol.All(
vol.Coerce(float),
vol.Range(min=0.0, max=1000000.0),
),
vol.Optional(CONF_DEFAULT_VALUE): str,
vol.Required(CONF_RESTORE_ON_RECONNECT): bool,
}
class LocaltuyaNumber(LocalTuyaEntity, NumberEntity, RestoreEntity):
class LocaltuyaNumber(LocalTuyaEntity, NumberEntity):
"""Representation of a Tuya Number."""
def __init__(
@@ -57,34 +62,47 @@ class LocaltuyaNumber(LocalTuyaEntity, NumberEntity, RestoreEntity):
if CONF_MIN_VALUE in self._config:
self._min_value = self._config.get(CONF_MIN_VALUE)
self._max_value = self._config.get(CONF_MAX_VALUE)
self._max_value = DEFAULT_MAX
if CONF_MAX_VALUE in self._config:
self._max_value = self._config.get(CONF_MAX_VALUE)
self._step_size = DEFAULT_STEP
if CONF_STEPSIZE_VALUE in self._config:
self._step_size = self._config.get(CONF_STEPSIZE_VALUE)
#Override standard default value handling to cast to a float
default_value = self._config.get(CONF_DEFAULT_VALUE)
if default_value is not None:
self._default_value = float(default_value)
@property
def value(self) -> float:
def native_value(self) -> float:
"""Return sensor state."""
return self._state
@property
def min_value(self) -> float:
def native_min_value(self) -> float:
"""Return the minimum value."""
return self._min_value
@property
def max_value(self) -> float:
def native_max_value(self) -> float:
"""Return the maximum value."""
return self._max_value
@property
def native_step(self) -> float:
"""Return the maximum value."""
return self._step_size
@property
def device_class(self):
"""Return the class of this device."""
return self._config.get(CONF_DEVICE_CLASS)
async def async_set_value(self, value: float) -> None:
async def async_set_native_value(self, value: float) -> None:
"""Update the current value."""
await self._device.set_dp(value, self._dp_id)