361 files
This commit is contained in:
@@ -10,10 +10,33 @@ if TYPE_CHECKING:
|
||||
from homeassistant.components.number import NumberEntity
|
||||
|
||||
|
||||
def native_value_as_float(entity: NumberEntity) -> float:
|
||||
"""Return the native value of a number entity as a float."""
|
||||
def shown_value_as_float(entity: NumberEntity) -> float:
|
||||
"""Return the value of a number entity, as it shows it, as a float.
|
||||
|
||||
In the units it shows, which are not always the units it works in: a
|
||||
thermostat that keeps Celsius can be shown in Fahrenheit. Stepping from
|
||||
this means stepping in what somebody sees, and the result has to go back
|
||||
through `async_set_shown_value` to land in the units the entity keeps.
|
||||
"""
|
||||
try:
|
||||
return float(entity.value)
|
||||
except (TypeError, ValueError) as err:
|
||||
msg = f"Native value {entity.value!r} for {entity.entity_id} is not a number"
|
||||
msg = f"Value {entity.value!r} for {entity.entity_id} is not a number"
|
||||
raise HomeAssistantError(msg) from err
|
||||
|
||||
|
||||
async def async_set_shown_value(entity: NumberEntity, value: float) -> None:
|
||||
"""Set a value given in the units the number shows, the way core does.
|
||||
|
||||
Converted to the units the entity works in, and kept within the limits
|
||||
it has there. Handing the shown value over as it is set a thermostat
|
||||
showing 68 °F to 69 °C on a step of one degree.
|
||||
"""
|
||||
try:
|
||||
native_value = entity.convert_to_native_value(value)
|
||||
native_value = min(
|
||||
max(native_value, entity.native_min_value), entity.native_max_value
|
||||
)
|
||||
await entity.async_set_native_value(native_value)
|
||||
except NotImplementedError:
|
||||
await entity.async_set_value(value)
|
||||
|
||||
@@ -10,7 +10,7 @@ import voluptuous as vol
|
||||
from homeassistant.components.number import DOMAIN, NumberEntity
|
||||
|
||||
from ....services import AbstractSpookEntityComponentService
|
||||
from . import native_value_as_float
|
||||
from . import async_set_shown_value, shown_value_as_float
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from homeassistant.core import ServiceCall
|
||||
@@ -44,9 +44,9 @@ class SpookService(AbstractSpookEntityComponentService[NumberEntity]):
|
||||
)
|
||||
raise ValueError(msg)
|
||||
|
||||
value = native_value_as_float(entity) - amount
|
||||
value = shown_value_as_float(entity) - amount
|
||||
|
||||
if entity.min_value is not None:
|
||||
value = max(value, entity.min_value)
|
||||
|
||||
await entity.async_set_native_value(value)
|
||||
await async_set_shown_value(entity, value)
|
||||
|
||||
@@ -10,7 +10,7 @@ import voluptuous as vol
|
||||
from homeassistant.components.number import DOMAIN, NumberEntity
|
||||
|
||||
from ....services import AbstractSpookEntityComponentService
|
||||
from . import native_value_as_float
|
||||
from . import async_set_shown_value, shown_value_as_float
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from homeassistant.core import ServiceCall
|
||||
@@ -44,9 +44,9 @@ class SpookService(AbstractSpookEntityComponentService[NumberEntity]):
|
||||
)
|
||||
raise ValueError(msg)
|
||||
|
||||
value = native_value_as_float(entity) + amount
|
||||
value = shown_value_as_float(entity) + amount
|
||||
|
||||
if entity.max_value is not None:
|
||||
value = min(value, entity.max_value)
|
||||
|
||||
await entity.async_set_native_value(value)
|
||||
await async_set_shown_value(entity, value)
|
||||
|
||||
Reference in New Issue
Block a user