Prevent using deprecated number features (#73578)

This commit is contained in:
Erik Montnemery 2022-06-20 10:26:24 +02:00 committed by GitHub
parent 4e6d753d2f
commit 9680a367c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -120,13 +120,14 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class NumberEntityDescription(EntityDescription): class NumberEntityDescription(EntityDescription):
"""A class that describes number entities.""" """A class that describes number entities."""
max_value: float | None = None max_value: None = None
min_value: float | None = None min_value: None = None
native_max_value: float | None = None native_max_value: float | None = None
native_min_value: float | None = None native_min_value: float | None = None
native_unit_of_measurement: str | None = None native_unit_of_measurement: str | None = None
native_step: float | None = None native_step: float | None = None
step: float | None = None step: None = None
unit_of_measurement: None = None # Type override, use native_unit_of_measurement
def __post_init__(self) -> None: def __post_init__(self) -> None:
"""Post initialisation processing.""" """Post initialisation processing."""
@ -136,7 +137,7 @@ class NumberEntityDescription(EntityDescription):
or self.step is not None or self.step is not None
or self.unit_of_measurement is not None or self.unit_of_measurement is not None
): ):
if self.__class__.__name__ == "NumberEntityDescription": if self.__class__.__name__ == "NumberEntityDescription": # type: ignore[unreachable]
caller = inspect.stack()[2] caller = inspect.stack()[2]
module = inspect.getmodule(caller[0]) module = inspect.getmodule(caller[0])
else: else:
@ -180,12 +181,12 @@ class NumberEntity(Entity):
"""Representation of a Number entity.""" """Representation of a Number entity."""
entity_description: NumberEntityDescription entity_description: NumberEntityDescription
_attr_max_value: float _attr_max_value: None
_attr_min_value: float _attr_min_value: None
_attr_state: None = None _attr_state: None = None
_attr_step: float _attr_step: None
_attr_mode: NumberMode = NumberMode.AUTO _attr_mode: NumberMode = NumberMode.AUTO
_attr_value: float _attr_value: None
_attr_native_max_value: float _attr_native_max_value: float
_attr_native_min_value: float _attr_native_min_value: float
_attr_native_step: float _attr_native_step: float
@ -248,16 +249,17 @@ class NumberEntity(Entity):
return DEFAULT_MIN_VALUE return DEFAULT_MIN_VALUE
@property @property
@final
def min_value(self) -> float: def min_value(self) -> float:
"""Return the minimum value.""" """Return the minimum value."""
if hasattr(self, "_attr_min_value"): if hasattr(self, "_attr_min_value"):
self._report_deprecated_number_entity() self._report_deprecated_number_entity()
return self._attr_min_value return self._attr_min_value # type: ignore[return-value]
if ( if (
hasattr(self, "entity_description") hasattr(self, "entity_description")
and self.entity_description.min_value is not None and self.entity_description.min_value is not None
): ):
self._report_deprecated_number_entity() self._report_deprecated_number_entity() # type: ignore[unreachable]
return self.entity_description.min_value return self.entity_description.min_value
return self._convert_to_state_value(self.native_min_value, floor_decimal) return self._convert_to_state_value(self.native_min_value, floor_decimal)
@ -274,16 +276,17 @@ class NumberEntity(Entity):
return DEFAULT_MAX_VALUE return DEFAULT_MAX_VALUE
@property @property
@final
def max_value(self) -> float: def max_value(self) -> float:
"""Return the maximum value.""" """Return the maximum value."""
if hasattr(self, "_attr_max_value"): if hasattr(self, "_attr_max_value"):
self._report_deprecated_number_entity() self._report_deprecated_number_entity()
return self._attr_max_value return self._attr_max_value # type: ignore[return-value]
if ( if (
hasattr(self, "entity_description") hasattr(self, "entity_description")
and self.entity_description.max_value is not None and self.entity_description.max_value is not None
): ):
self._report_deprecated_number_entity() self._report_deprecated_number_entity() # type: ignore[unreachable]
return self.entity_description.max_value return self.entity_description.max_value
return self._convert_to_state_value(self.native_max_value, ceil_decimal) return self._convert_to_state_value(self.native_max_value, ceil_decimal)
@ -298,16 +301,17 @@ class NumberEntity(Entity):
return None return None
@property @property
@final
def step(self) -> float: def step(self) -> float:
"""Return the increment/decrement step.""" """Return the increment/decrement step."""
if hasattr(self, "_attr_step"): if hasattr(self, "_attr_step"):
self._report_deprecated_number_entity() self._report_deprecated_number_entity()
return self._attr_step return self._attr_step # type: ignore[return-value]
if ( if (
hasattr(self, "entity_description") hasattr(self, "entity_description")
and self.entity_description.step is not None and self.entity_description.step is not None
): ):
self._report_deprecated_number_entity() self._report_deprecated_number_entity() # type: ignore[unreachable]
return self.entity_description.step return self.entity_description.step
if hasattr(self, "_attr_native_step"): if hasattr(self, "_attr_native_step"):
return self._attr_native_step return self._attr_native_step
@ -341,6 +345,7 @@ class NumberEntity(Entity):
return None return None
@property @property
@final
def unit_of_measurement(self) -> str | None: def unit_of_measurement(self) -> str | None:
"""Return the unit of measurement of the entity, after unit conversion.""" """Return the unit of measurement of the entity, after unit conversion."""
if hasattr(self, "_attr_unit_of_measurement"): if hasattr(self, "_attr_unit_of_measurement"):
@ -349,7 +354,7 @@ class NumberEntity(Entity):
hasattr(self, "entity_description") hasattr(self, "entity_description")
and self.entity_description.unit_of_measurement is not None and self.entity_description.unit_of_measurement is not None
): ):
return self.entity_description.unit_of_measurement return self.entity_description.unit_of_measurement # type: ignore[unreachable]
native_unit_of_measurement = self.native_unit_of_measurement native_unit_of_measurement = self.native_unit_of_measurement
@ -367,6 +372,7 @@ class NumberEntity(Entity):
return self._attr_native_value return self._attr_native_value
@property @property
@final
def value(self) -> float | None: def value(self) -> float | None:
"""Return the entity value to represent the entity state.""" """Return the entity value to represent the entity state."""
if hasattr(self, "_attr_value"): if hasattr(self, "_attr_value"):
@ -385,10 +391,12 @@ class NumberEntity(Entity):
"""Set new value.""" """Set new value."""
await self.hass.async_add_executor_job(self.set_native_value, value) await self.hass.async_add_executor_job(self.set_native_value, value)
@final
def set_value(self, value: float) -> None: def set_value(self, value: float) -> None:
"""Set new value.""" """Set new value."""
raise NotImplementedError() raise NotImplementedError()
@final
async def async_set_value(self, value: float) -> None: async def async_set_value(self, value: float) -> None:
"""Set new value.""" """Set new value."""
await self.hass.async_add_executor_job(self.set_value, value) await self.hass.async_add_executor_job(self.set_value, value)