Skip to content

Commit

Permalink
Reduce internal property lookups needed to write number entity state (h…
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 19, 2023
1 parent 615b02b commit c408b60
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions homeassistant/components/number/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,12 @@ async def async_internal_added_to_hass(self) -> None:
@property
def capability_attributes(self) -> dict[str, Any]:
"""Return capability attributes."""
min_value = self.min_value
max_value = self.max_value
return {
ATTR_MIN: self.min_value,
ATTR_MAX: self.max_value,
ATTR_STEP: self.step,
ATTR_MIN: min_value,
ATTR_MAX: max_value,
ATTR_STEP: self._calculate_step(min_value, max_value),
ATTR_MODE: self.mode,
}

Expand Down Expand Up @@ -290,13 +292,17 @@ def native_step(self) -> float | None:
@property
@final
def step(self) -> float:
"""Return the increment/decrement step."""
return self._calculate_step(self.min_value, self.max_value)

def _calculate_step(self, min_value: float, max_value: float) -> float:
"""Return the increment/decrement 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
value_range = abs(self.max_value - self.min_value)
value_range = abs(max_value - min_value)
if value_range != 0:
while value_range <= step:
step /= 10.0
Expand Down Expand Up @@ -337,11 +343,12 @@ def unit_of_measurement(self) -> str | None:
return self._number_option_unit_of_measurement

native_unit_of_measurement = self.native_unit_of_measurement

# device_class is checked after native_unit_of_measurement since most
# of the time we can avoid the device_class check
if (
self.device_class == NumberDeviceClass.TEMPERATURE
and native_unit_of_measurement
native_unit_of_measurement
in (UnitOfTemperature.CELSIUS, UnitOfTemperature.FAHRENHEIT)
and self.device_class == NumberDeviceClass.TEMPERATURE
):
return self.hass.config.units.temperature_unit

Expand Down Expand Up @@ -382,14 +389,14 @@ def _convert_to_state_value(
self, value: float, method: Callable[[float, int], float]
) -> float:
"""Convert a value in the number's native unit to the configured unit."""
# device_class is checked first since most of the time we can avoid
# the unit conversion
if (device_class := self.device_class) not in UNIT_CONVERTERS:
return value

native_unit_of_measurement = self.native_unit_of_measurement
unit_of_measurement = self.unit_of_measurement
device_class = self.device_class

if (
native_unit_of_measurement != unit_of_measurement
and device_class in UNIT_CONVERTERS
):
if native_unit_of_measurement != unit_of_measurement:
assert native_unit_of_measurement
assert unit_of_measurement

Expand All @@ -411,15 +418,14 @@ def _convert_to_state_value(

def convert_to_native_value(self, value: float) -> float:
"""Convert a value to the number's native unit."""
# device_class is checked first since most of the time we can avoid
# the unit conversion
if value is None or (device_class := self.device_class) not in UNIT_CONVERTERS:
return value

native_unit_of_measurement = self.native_unit_of_measurement
unit_of_measurement = self.unit_of_measurement
device_class = self.device_class

if (
value is not None
and native_unit_of_measurement != unit_of_measurement
and device_class in UNIT_CONVERTERS
):
if native_unit_of_measurement != unit_of_measurement:
assert native_unit_of_measurement
assert unit_of_measurement

Expand Down

0 comments on commit c408b60

Please sign in to comment.