Migrate NumberEntity r-t to native_value (#73485)

This commit is contained in:
Erik Montnemery 2022-06-15 10:56:41 +02:00 committed by GitHub
parent 4ace2c4d3a
commit e05e79e53d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 47 deletions

View File

@ -38,8 +38,8 @@ class DiffuserPerfumeAmount(DiffuserEntity, NumberEntity):
"""Representation of a diffuser perfume amount number.""" """Representation of a diffuser perfume amount number."""
_attr_icon = "mdi:gauge" _attr_icon = "mdi:gauge"
_attr_max_value = MAX_PERFUME_AMOUNT _attr_native_max_value = MAX_PERFUME_AMOUNT
_attr_min_value = MIN_PERFUME_AMOUNT _attr_native_min_value = MIN_PERFUME_AMOUNT
def __init__( def __init__(
self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator
@ -48,11 +48,11 @@ class DiffuserPerfumeAmount(DiffuserEntity, NumberEntity):
super().__init__(diffuser, coordinator, PERFUME_AMOUNT_SUFFIX) super().__init__(diffuser, coordinator, PERFUME_AMOUNT_SUFFIX)
@property @property
def value(self) -> int: def native_value(self) -> int:
"""Return the current perfume amount.""" """Return the current perfume amount."""
return self._diffuser.perfume_amount return self._diffuser.perfume_amount
async def async_set_value(self, value: float) -> None: async def async_set_native_value(self, value: float) -> None:
"""Set the perfume amount.""" """Set the perfume amount."""
if not value.is_integer(): if not value.is_integer():
raise ValueError( raise ValueError(

View File

@ -46,16 +46,16 @@ class ScreenLogicNumber(ScreenlogicEntity, NumberEntity):
"""Initialize of the entity.""" """Initialize of the entity."""
super().__init__(coordinator, data_key, enabled) super().__init__(coordinator, data_key, enabled)
self._body_type = SUPPORTED_SCG_NUMBERS.index(self._data_key) self._body_type = SUPPORTED_SCG_NUMBERS.index(self._data_key)
self._attr_max_value = SCG.LIMIT_FOR_BODY[self._body_type] self._attr_native_max_value = SCG.LIMIT_FOR_BODY[self._body_type]
self._attr_name = f"{self.gateway_name} {self.sensor['name']}" self._attr_name = f"{self.gateway_name} {self.sensor['name']}"
self._attr_unit_of_measurement = self.sensor["unit"] self._attr_native_unit_of_measurement = self.sensor["unit"]
@property @property
def value(self) -> float: def native_value(self) -> float:
"""Return the current value.""" """Return the current value."""
return self.sensor["value"] return self.sensor["value"]
async def async_set_value(self, value: float) -> None: async def async_set_native_value(self, value: float) -> None:
"""Update the current value.""" """Update the current value."""
# Need to set both levels at the same time, so we gather # Need to set both levels at the same time, so we gather
# both existing level values and override the one that changed. # both existing level values and override the one that changed.

View File

@ -39,9 +39,9 @@ DEVICE_NUMBER_TYPES = (
icon="mdi:thermometer", icon="mdi:thermometer",
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
min_value=-10, native_min_value=-10,
max_value=10, native_max_value=10,
step=0.1, native_step=0.1,
), ),
SensiboNumberEntityDescription( SensiboNumberEntityDescription(
key="calibration_hum", key="calibration_hum",
@ -50,9 +50,9 @@ DEVICE_NUMBER_TYPES = (
icon="mdi:water", icon="mdi:water",
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
min_value=-10, native_min_value=-10,
max_value=10, native_max_value=10,
step=0.1, native_step=0.1,
), ),
) )
@ -89,12 +89,12 @@ class SensiboNumber(SensiboDeviceBaseEntity, NumberEntity):
self._attr_name = f"{self.device_data.name} {entity_description.name}" self._attr_name = f"{self.device_data.name} {entity_description.name}"
@property @property
def value(self) -> float | None: def native_value(self) -> float | None:
"""Return the value from coordinator data.""" """Return the value from coordinator data."""
value: float | None = getattr(self.device_data, self.entity_description.key) value: float | None = getattr(self.device_data, self.entity_description.key)
return value return value
async def async_set_value(self, value: float) -> None: async def async_set_native_value(self, value: float) -> None:
"""Set value for calibration.""" """Set value for calibration."""
data = {self.entity_description.remote_key: value} data = {self.entity_description.remote_key: value}
result = await self.async_send_command("set_calibration", {"data": data}) result = await self.async_send_command("set_calibration", {"data": data})

View File

@ -42,12 +42,12 @@ NUMBERS: Final = {
key="device|valvepos", key="device|valvepos",
icon="mdi:pipe-valve", icon="mdi:pipe-valve",
name="Valve Position", name="Valve Position",
unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
available=lambda block: cast(int, block.valveError) != 1, available=lambda block: cast(int, block.valveError) != 1,
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
min_value=0, native_min_value=0,
max_value=100, native_max_value=100,
step=1, native_step=1,
mode=NumberMode("slider"), mode=NumberMode("slider"),
rest_path="thermostat/0", rest_path="thermostat/0",
rest_arg="pos", rest_arg="pos",
@ -62,11 +62,11 @@ def _build_block_description(entry: RegistryEntry) -> BlockNumberDescription:
key="", key="",
name="", name="",
icon=entry.original_icon, icon=entry.original_icon,
unit_of_measurement=entry.unit_of_measurement, native_unit_of_measurement=entry.unit_of_measurement,
device_class=entry.original_device_class, device_class=entry.original_device_class,
min_value=cast(float, entry.capabilities.get("min")), native_min_value=cast(float, entry.capabilities.get("min")),
max_value=cast(float, entry.capabilities.get("max")), native_max_value=cast(float, entry.capabilities.get("max")),
step=cast(float, entry.capabilities.get("step")), native_step=cast(float, entry.capabilities.get("step")),
mode=cast(NumberMode, entry.capabilities.get("mode")), mode=cast(NumberMode, entry.capabilities.get("mode")),
) )
@ -97,14 +97,14 @@ class BlockSleepingNumber(ShellySleepingBlockAttributeEntity, NumberEntity):
entity_description: BlockNumberDescription entity_description: BlockNumberDescription
@property @property
def value(self) -> float: def native_value(self) -> float:
"""Return value of number.""" """Return value of number."""
if self.block is not None: if self.block is not None:
return cast(float, self.attribute_value) return cast(float, self.attribute_value)
return cast(float, self.last_state) return cast(float, self.last_state)
async def async_set_value(self, value: float) -> None: async def async_set_native_value(self, value: float) -> None:
"""Set value.""" """Set value."""
# Example for Shelly Valve: http://192.168.188.187/thermostat/0?pos=13.0 # Example for Shelly Valve: http://192.168.188.187/thermostat/0?pos=13.0
await self._set_state_full_path( await self._set_state_full_path(

View File

@ -70,9 +70,9 @@ def _get_sleeper_unique_id(bed: SleepIQBed, sleeper: SleepIQSleeper) -> str:
NUMBER_DESCRIPTIONS: dict[str, SleepIQNumberEntityDescription] = { NUMBER_DESCRIPTIONS: dict[str, SleepIQNumberEntityDescription] = {
FIRMNESS: SleepIQNumberEntityDescription( FIRMNESS: SleepIQNumberEntityDescription(
key=FIRMNESS, key=FIRMNESS,
min_value=5, native_min_value=5,
max_value=100, native_max_value=100,
step=5, native_step=5,
name=ENTITY_TYPES[FIRMNESS], name=ENTITY_TYPES[FIRMNESS],
icon=ICON_OCCUPIED, icon=ICON_OCCUPIED,
value_fn=lambda sleeper: cast(float, sleeper.sleep_number), value_fn=lambda sleeper: cast(float, sleeper.sleep_number),
@ -82,9 +82,9 @@ NUMBER_DESCRIPTIONS: dict[str, SleepIQNumberEntityDescription] = {
), ),
ACTUATOR: SleepIQNumberEntityDescription( ACTUATOR: SleepIQNumberEntityDescription(
key=ACTUATOR, key=ACTUATOR,
min_value=0, native_min_value=0,
max_value=100, native_max_value=100,
step=1, native_step=1,
name=ENTITY_TYPES[ACTUATOR], name=ENTITY_TYPES[ACTUATOR],
icon=ICON_OCCUPIED, icon=ICON_OCCUPIED,
value_fn=lambda actuator: cast(float, actuator.position), value_fn=lambda actuator: cast(float, actuator.position),
@ -152,9 +152,9 @@ class SleepIQNumberEntity(SleepIQBedEntity, NumberEntity):
@callback @callback
def _async_update_attrs(self) -> None: def _async_update_attrs(self) -> None:
"""Update number attributes.""" """Update number attributes."""
self._attr_value = float(self.entity_description.value_fn(self.device)) self._attr_native_value = float(self.entity_description.value_fn(self.device))
async def async_set_value(self, value: float) -> None: async def async_set_native_value(self, value: float) -> None:
"""Set the number value.""" """Set the number value."""
await self.entity_description.set_value_fn(self.device, int(value)) await self.entity_description.set_value_fn(self.device, int(value))
self._attr_value = value self._attr_value = value

View File

@ -73,7 +73,7 @@ class SonosLevelEntity(SonosEntity, NumberEntity):
name_suffix = level_type.replace("_", " ").title() name_suffix = level_type.replace("_", " ").title()
self._attr_name = f"{self.speaker.zone_name} {name_suffix}" self._attr_name = f"{self.speaker.zone_name} {name_suffix}"
self.level_type = level_type self.level_type = level_type
self._attr_min_value, self._attr_max_value = valid_range self._attr_native_min_value, self._attr_native_max_value = valid_range
async def _async_fallback_poll(self) -> None: async def _async_fallback_poll(self) -> None:
"""Poll the value if subscriptions are not working.""" """Poll the value if subscriptions are not working."""
@ -86,11 +86,11 @@ class SonosLevelEntity(SonosEntity, NumberEntity):
setattr(self.speaker, self.level_type, state) setattr(self.speaker, self.level_type, state)
@soco_error() @soco_error()
def set_value(self, value: float) -> None: def set_native_value(self, value: float) -> None:
"""Set a new value.""" """Set a new value."""
setattr(self.soco, self.level_type, value) setattr(self.soco, self.level_type, value)
@property @property
def value(self) -> float: def native_value(self) -> float:
"""Return the current value.""" """Return the current value."""
return getattr(self.speaker, self.level_type) return getattr(self.speaker, self.level_type)

View File

@ -34,8 +34,8 @@ class ToloNumberEntityDescription(
"""Class describing TOLO Number entities.""" """Class describing TOLO Number entities."""
entity_category = EntityCategory.CONFIG entity_category = EntityCategory.CONFIG
min_value = 0 native_min_value = 0
step = 1 native_step = 1
NUMBERS = ( NUMBERS = (
@ -43,8 +43,8 @@ NUMBERS = (
key="power_timer", key="power_timer",
icon="mdi:power-settings", icon="mdi:power-settings",
name="Power Timer", name="Power Timer",
unit_of_measurement=TIME_MINUTES, native_unit_of_measurement=TIME_MINUTES,
max_value=POWER_TIMER_MAX, native_max_value=POWER_TIMER_MAX,
getter=lambda settings: settings.power_timer, getter=lambda settings: settings.power_timer,
setter=lambda client, value: client.set_power_timer(value), setter=lambda client, value: client.set_power_timer(value),
), ),
@ -52,8 +52,8 @@ NUMBERS = (
key="salt_bath_timer", key="salt_bath_timer",
icon="mdi:shaker-outline", icon="mdi:shaker-outline",
name="Salt Bath Timer", name="Salt Bath Timer",
unit_of_measurement=TIME_MINUTES, native_unit_of_measurement=TIME_MINUTES,
max_value=SALT_BATH_TIMER_MAX, native_max_value=SALT_BATH_TIMER_MAX,
getter=lambda settings: settings.salt_bath_timer, getter=lambda settings: settings.salt_bath_timer,
setter=lambda client, value: client.set_salt_bath_timer(value), setter=lambda client, value: client.set_salt_bath_timer(value),
), ),
@ -61,8 +61,8 @@ NUMBERS = (
key="fan_timer", key="fan_timer",
icon="mdi:fan-auto", icon="mdi:fan-auto",
name="Fan Timer", name="Fan Timer",
unit_of_measurement=TIME_MINUTES, native_unit_of_measurement=TIME_MINUTES,
max_value=FAN_TIMER_MAX, native_max_value=FAN_TIMER_MAX,
getter=lambda settings: settings.fan_timer, getter=lambda settings: settings.fan_timer,
setter=lambda client, value: client.set_fan_timer(value), setter=lambda client, value: client.set_fan_timer(value),
), ),
@ -98,11 +98,11 @@ class ToloNumberEntity(ToloSaunaCoordinatorEntity, NumberEntity):
self._attr_unique_id = f"{entry.entry_id}_{entity_description.key}" self._attr_unique_id = f"{entry.entry_id}_{entity_description.key}"
@property @property
def value(self) -> float: def native_value(self) -> float:
"""Return the value of this TOLO Number entity.""" """Return the value of this TOLO Number entity."""
return self.entity_description.getter(self.coordinator.data.settings) or 0 return self.entity_description.getter(self.coordinator.data.settings) or 0
def set_value(self, value: float) -> None: def set_native_value(self, value: float) -> None:
"""Set the value of this TOLO Number entity.""" """Set the value of this TOLO Number entity."""
int_value = int(value) int_value = int(value)
if int_value == 0: if int_value == 0: