Small cleanups to number entity (#107624)
parent
82dc8260c6
commit
05d205ae7a
|
@ -424,22 +424,22 @@ class NumberEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
|
|||
native_unit_of_measurement = self.native_unit_of_measurement
|
||||
unit_of_measurement = self.unit_of_measurement
|
||||
if native_unit_of_measurement != unit_of_measurement:
|
||||
assert native_unit_of_measurement
|
||||
assert unit_of_measurement
|
||||
if TYPE_CHECKING:
|
||||
assert native_unit_of_measurement
|
||||
assert unit_of_measurement
|
||||
|
||||
value_s = str(value)
|
||||
prec = len(value_s) - value_s.index(".") - 1 if "." in value_s else 0
|
||||
|
||||
# Suppress ValueError (Could not convert value to float)
|
||||
with suppress(ValueError):
|
||||
value_new: float = UNIT_CONVERTERS[device_class].convert(
|
||||
value,
|
||||
value_new: float = UNIT_CONVERTERS[device_class].converter_factory(
|
||||
native_unit_of_measurement,
|
||||
unit_of_measurement,
|
||||
)
|
||||
)(value)
|
||||
|
||||
# Round to the wanted precision
|
||||
value = method(value_new, prec)
|
||||
return method(value_new, prec)
|
||||
|
||||
return value
|
||||
|
||||
|
@ -453,21 +453,22 @@ class NumberEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
|
|||
native_unit_of_measurement = self.native_unit_of_measurement
|
||||
unit_of_measurement = self.unit_of_measurement
|
||||
if native_unit_of_measurement != unit_of_measurement:
|
||||
assert native_unit_of_measurement
|
||||
assert unit_of_measurement
|
||||
if TYPE_CHECKING:
|
||||
assert native_unit_of_measurement
|
||||
assert unit_of_measurement
|
||||
|
||||
value = UNIT_CONVERTERS[device_class].convert(
|
||||
value,
|
||||
return UNIT_CONVERTERS[device_class].converter_factory(
|
||||
unit_of_measurement,
|
||||
native_unit_of_measurement,
|
||||
)
|
||||
)(value)
|
||||
|
||||
return value
|
||||
|
||||
@callback
|
||||
def async_registry_entry_updated(self) -> None:
|
||||
"""Run when the entity registry entry has been updated."""
|
||||
assert self.registry_entry
|
||||
if TYPE_CHECKING:
|
||||
assert self.registry_entry
|
||||
if (
|
||||
(number_options := self.registry_entry.options.get(DOMAIN))
|
||||
and (custom_unit := number_options.get(CONF_UNIT_OF_MEASUREMENT))
|
||||
|
|
|
@ -475,7 +475,7 @@ DEVICE_CLASS_UNITS: dict[NumberDeviceClass, set[type[StrEnum] | str | None]] = {
|
|||
NumberDeviceClass.WIND_SPEED: set(UnitOfSpeed),
|
||||
}
|
||||
|
||||
UNIT_CONVERTERS: dict[str, type[BaseUnitConverter]] = {
|
||||
UNIT_CONVERTERS: dict[NumberDeviceClass, type[BaseUnitConverter]] = {
|
||||
NumberDeviceClass.TEMPERATURE: TemperatureConverter,
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue