1
mirror of https://github.com/home-assistant/core synced 2024-08-28 03:36:46 +02:00

Fix native_step in number not looking at _attr_native_step (#106327)

This commit is contained in:
J. Nick Koston 2023-12-23 13:20:24 -10:00 committed by GitHub
parent abd3c54cbe
commit e43f4412fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 2 deletions

View File

@ -306,6 +306,8 @@ class NumberEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
@cached_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
@ -321,8 +323,6 @@ class NumberEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
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

View File

@ -131,6 +131,31 @@ class MockNumberEntityDescr(NumberEntity):
return None
class MockNumberEntityAttrWithDescription(NumberEntity):
"""Mock NumberEntity device to use in tests.
This class sets an entity description and overrides
all the values with _attr members to ensure the _attr
members take precedence over the entity description.
"""
def __init__(self):
"""Initialize the clas instance."""
self.entity_description = NumberEntityDescription(
"test",
native_max_value=10.0,
native_min_value=-10.0,
native_step=2.0,
native_unit_of_measurement="native_rabbits",
)
_attr_native_max_value = 1000.0
_attr_native_min_value = -1000.0
_attr_native_step = 100.0
_attr_native_unit_of_measurement = "native_dogs"
_attr_native_value = 500.0
class MockDefaultNumberEntityDeprecated(NumberEntity):
"""Mock NumberEntity device to use in tests.
@ -277,6 +302,21 @@ async def test_attributes(hass: HomeAssistant) -> None:
ATTR_STEP: 2.0,
}
number_5 = MockNumberEntityAttrWithDescription()
number_5.hass = hass
assert number_5.max_value == 1000.0
assert number_5.min_value == -1000.0
assert number_5.step == 100.0
assert number_5.native_step == 100.0
assert number_5.unit_of_measurement == "native_dogs"
assert number_5.value == 500.0
assert number_5.capability_attributes == {
ATTR_MAX: 1000.0,
ATTR_MIN: -1000.0,
ATTR_MODE: NumberMode.AUTO,
ATTR_STEP: 100.0,
}
async def test_sync_set_value(hass: HomeAssistant) -> None:
"""Test if async set_value calls sync set_value."""