mirror of
https://github.com/home-assistant/core
synced 2024-09-03 08:14:07 +02:00
Migrate template NumberEntity to native_value (#73537)
This commit is contained in:
parent
b014d558ff
commit
7a82794ad7
@ -252,8 +252,6 @@ class NumberEntity(Entity):
|
||||
@property
|
||||
def native_step(self) -> float | None:
|
||||
"""Return the increment/decrement step."""
|
||||
if hasattr(self, "_attr_native_step"):
|
||||
return self._attr_native_step
|
||||
if (
|
||||
hasattr(self, "entity_description")
|
||||
and self.entity_description.native_step is not None
|
||||
@ -273,6 +271,8 @@ class NumberEntity(Entity):
|
||||
):
|
||||
self._report_deprecated_number_entity()
|
||||
return self.entity_description.step
|
||||
if hasattr(self, "_attr_native_step"):
|
||||
return self._attr_native_step
|
||||
if (native_step := self.native_step) is not None:
|
||||
return native_step
|
||||
step = DEFAULT_STEP
|
||||
|
@ -119,45 +119,45 @@ class TemplateNumber(TemplateEntity, NumberEntity):
|
||||
self._min_value_template = config[ATTR_MIN]
|
||||
self._max_value_template = config[ATTR_MAX]
|
||||
self._attr_assumed_state = self._optimistic = config[CONF_OPTIMISTIC]
|
||||
self._attr_value = None
|
||||
self._attr_step = None
|
||||
self._attr_min_value = None
|
||||
self._attr_max_value = None
|
||||
self._attr_native_value = None
|
||||
self._attr_native_step = None
|
||||
self._attr_native_min_value = None
|
||||
self._attr_native_max_value = None
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Register callbacks."""
|
||||
self.add_template_attribute(
|
||||
"_attr_value",
|
||||
"_attr_native_value",
|
||||
self._value_template,
|
||||
validator=vol.Coerce(float),
|
||||
none_on_template_error=True,
|
||||
)
|
||||
self.add_template_attribute(
|
||||
"_attr_step",
|
||||
"_attr_native_step",
|
||||
self._step_template,
|
||||
validator=vol.Coerce(float),
|
||||
none_on_template_error=True,
|
||||
)
|
||||
if self._min_value_template is not None:
|
||||
self.add_template_attribute(
|
||||
"_attr_min_value",
|
||||
"_attr_native_min_value",
|
||||
self._min_value_template,
|
||||
validator=vol.Coerce(float),
|
||||
none_on_template_error=True,
|
||||
)
|
||||
if self._max_value_template is not None:
|
||||
self.add_template_attribute(
|
||||
"_attr_max_value",
|
||||
"_attr_native_max_value",
|
||||
self._max_value_template,
|
||||
validator=vol.Coerce(float),
|
||||
none_on_template_error=True,
|
||||
)
|
||||
await super().async_added_to_hass()
|
||||
|
||||
async def async_set_value(self, value: float) -> None:
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set value of the number."""
|
||||
if self._optimistic:
|
||||
self._attr_value = value
|
||||
self._attr_native_value = value
|
||||
self.async_write_ha_state()
|
||||
await self.async_run_script(
|
||||
self._command_set_value,
|
||||
@ -193,35 +193,35 @@ class TriggerNumberEntity(TriggerEntity, NumberEntity):
|
||||
)
|
||||
|
||||
@property
|
||||
def value(self) -> float | None:
|
||||
def native_value(self) -> float | None:
|
||||
"""Return the currently selected option."""
|
||||
return vol.Any(vol.Coerce(float), None)(self._rendered.get(CONF_STATE))
|
||||
|
||||
@property
|
||||
def min_value(self) -> int:
|
||||
def native_min_value(self) -> int:
|
||||
"""Return the minimum value."""
|
||||
return vol.Any(vol.Coerce(float), None)(
|
||||
self._rendered.get(ATTR_MIN, super().min_value)
|
||||
self._rendered.get(ATTR_MIN, super().native_min_value)
|
||||
)
|
||||
|
||||
@property
|
||||
def max_value(self) -> int:
|
||||
def native_max_value(self) -> int:
|
||||
"""Return the maximum value."""
|
||||
return vol.Any(vol.Coerce(float), None)(
|
||||
self._rendered.get(ATTR_MAX, super().max_value)
|
||||
self._rendered.get(ATTR_MAX, super().native_max_value)
|
||||
)
|
||||
|
||||
@property
|
||||
def step(self) -> int:
|
||||
def native_step(self) -> int:
|
||||
"""Return the increment/decrement step."""
|
||||
return vol.Any(vol.Coerce(float), None)(
|
||||
self._rendered.get(ATTR_STEP, super().step)
|
||||
self._rendered.get(ATTR_STEP, super().native_step)
|
||||
)
|
||||
|
||||
async def async_set_value(self, value: float) -> None:
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set value of the number."""
|
||||
if self._config[CONF_OPTIMISTIC]:
|
||||
self._attr_value = value
|
||||
self._attr_native_value = value
|
||||
self.async_write_ha_state()
|
||||
await self._command_set_value.async_run(
|
||||
{ATTR_VALUE: value}, context=self._context
|
||||
|
Loading…
Reference in New Issue
Block a user