Migrate NumberEntity u-z to native_value (#73488)

This commit is contained in:
Erik Montnemery 2022-06-14 20:15:56 +02:00 committed by GitHub
parent 23fa19b75a
commit 576de9ac40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 84 additions and 84 deletions

View file

@ -198,15 +198,15 @@ class ProtectNumbers(ProtectDeviceEntity, NumberEntity):
) -> None:
"""Initialize the Number Entities."""
super().__init__(data, device, description)
self._attr_max_value = self.entity_description.ufp_max
self._attr_min_value = self.entity_description.ufp_min
self._attr_step = self.entity_description.ufp_step
self._attr_native_max_value = self.entity_description.ufp_max
self._attr_native_min_value = self.entity_description.ufp_min
self._attr_native_step = self.entity_description.ufp_step
@callback
def _async_update_device_from_protect(self) -> None:
super()._async_update_device_from_protect()
self._attr_value = self.entity_description.get_ufp_value(self.device)
self._attr_native_value = self.entity_description.get_ufp_value(self.device)
async def async_set_value(self, value: float) -> None:
async def async_set_native_value(self, value: float) -> None:
"""Set new value."""
await self.entity_description.ufp_set(self.device, value)

View file

@ -28,7 +28,7 @@ NUMBER_TYPES: dict[str, WallboxNumberEntityDescription] = {
CHARGER_MAX_CHARGING_CURRENT_KEY: WallboxNumberEntityDescription(
key=CHARGER_MAX_CHARGING_CURRENT_KEY,
name="Max. Charging Current",
min_value=6,
native_min_value=6,
),
}
@ -74,17 +74,17 @@ class WallboxNumber(WallboxEntity, NumberEntity):
self._attr_unique_id = f"{description.key}-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
@property
def max_value(self) -> float:
def native_max_value(self) -> float:
"""Return the maximum available current."""
return cast(float, self._coordinator.data[CHARGER_MAX_AVAILABLE_POWER_KEY])
@property
def value(self) -> float | None:
def native_value(self) -> float | None:
"""Return the state of the sensor."""
return cast(
Optional[float], self._coordinator.data[CHARGER_MAX_CHARGING_CURRENT_KEY]
)
async def async_set_value(self, value: float) -> None:
async def async_set_native_value(self, value: float) -> None:
"""Set the value of the entity."""
await self._coordinator.async_set_charging_current(value)

View file

@ -48,9 +48,9 @@ async def _async_set_ratio(device: wizlight, ratio: int) -> None:
NUMBERS: tuple[WizNumberEntityDescription, ...] = (
WizNumberEntityDescription(
key="effect_speed",
min_value=10,
max_value=200,
step=1,
native_min_value=10,
native_max_value=200,
native_step=1,
icon="mdi:speedometer",
name="Effect Speed",
value_fn=lambda device: cast(Optional[int], device.state.get_speed()),
@ -59,9 +59,9 @@ NUMBERS: tuple[WizNumberEntityDescription, ...] = (
),
WizNumberEntityDescription(
key="dual_head_ratio",
min_value=0,
max_value=100,
step=1,
native_min_value=0,
native_max_value=100,
native_step=1,
icon="mdi:floor-lamp-dual",
name="Dual Head Ratio",
value_fn=lambda device: cast(Optional[int], device.state.get_ratio()),
@ -113,9 +113,9 @@ class WizSpeedNumber(WizEntity, NumberEntity):
def _async_update_attrs(self) -> None:
"""Handle updating _attr values."""
if (value := self.entity_description.value_fn(self._device)) is not None:
self._attr_value = float(value)
self._attr_native_value = float(value)
async def async_set_value(self, value: float) -> None:
async def async_set_native_value(self, value: float) -> None:
"""Set the speed value."""
await self.entity_description.set_value_fn(self._device, int(value))
await self.coordinator.async_request_refresh()

View file

@ -41,17 +41,17 @@ NUMBERS = [
name="Speed",
icon="mdi:speedometer",
entity_category=EntityCategory.CONFIG,
step=1,
min_value=0,
max_value=255,
native_step=1,
native_min_value=0,
native_max_value=255,
),
NumberEntityDescription(
key=ATTR_INTENSITY,
name="Intensity",
entity_category=EntityCategory.CONFIG,
step=1,
min_value=0,
max_value=255,
native_step=1,
native_min_value=0,
native_max_value=255,
),
]
@ -93,7 +93,7 @@ class WLEDNumber(WLEDEntity, NumberEntity):
return super().available
@property
def value(self) -> float | None:
def native_value(self) -> float | None:
"""Return the current WLED segment number value."""
return getattr(
self.coordinator.data.state.segments[self._segment],
@ -101,7 +101,7 @@ class WLEDNumber(WLEDEntity, NumberEntity):
)
@wled_exception_handler
async def async_set_value(self, value: float) -> None:
async def async_set_native_value(self, value: float) -> None:
"""Set the WLED segment value."""
key = self.entity_description.key
if key == ATTR_SPEED:

View file

@ -108,10 +108,10 @@ NUMBER_TYPES = {
key=ATTR_MOTOR_SPEED,
name="Motor Speed",
icon="mdi:fast-forward-outline",
unit_of_measurement="rpm",
min_value=200,
max_value=2000,
step=10,
native_unit_of_measurement="rpm",
native_min_value=200,
native_max_value=2000,
native_step=10,
available_with_device_off=False,
method="async_set_motor_speed",
entity_category=EntityCategory.CONFIG,
@ -120,9 +120,9 @@ NUMBER_TYPES = {
key=ATTR_FAVORITE_LEVEL,
name="Favorite Level",
icon="mdi:star-cog",
min_value=0,
max_value=17,
step=1,
native_min_value=0,
native_max_value=17,
native_step=1,
method="async_set_favorite_level",
entity_category=EntityCategory.CONFIG,
),
@ -130,9 +130,9 @@ NUMBER_TYPES = {
key=ATTR_FAN_LEVEL,
name="Fan Level",
icon="mdi:fan",
min_value=1,
max_value=3,
step=1,
native_min_value=1,
native_max_value=3,
native_step=1,
method="async_set_fan_level",
entity_category=EntityCategory.CONFIG,
),
@ -140,9 +140,9 @@ NUMBER_TYPES = {
key=ATTR_VOLUME,
name="Volume",
icon="mdi:volume-high",
min_value=0,
max_value=100,
step=1,
native_min_value=0,
native_max_value=100,
native_step=1,
method="async_set_volume",
entity_category=EntityCategory.CONFIG,
),
@ -150,10 +150,10 @@ NUMBER_TYPES = {
key=ATTR_OSCILLATION_ANGLE,
name="Oscillation Angle",
icon="mdi:angle-acute",
unit_of_measurement=DEGREE,
min_value=1,
max_value=120,
step=1,
native_unit_of_measurement=DEGREE,
native_min_value=1,
native_max_value=120,
native_step=1,
method="async_set_oscillation_angle",
entity_category=EntityCategory.CONFIG,
),
@ -161,10 +161,10 @@ NUMBER_TYPES = {
key=ATTR_DELAY_OFF_COUNTDOWN,
name="Delay Off Countdown",
icon="mdi:fan-off",
unit_of_measurement=TIME_MINUTES,
min_value=0,
max_value=480,
step=1,
native_unit_of_measurement=TIME_MINUTES,
native_min_value=0,
native_max_value=480,
native_step=1,
method="async_set_delay_off_countdown",
entity_category=EntityCategory.CONFIG,
),
@ -172,9 +172,9 @@ NUMBER_TYPES = {
key=ATTR_LED_BRIGHTNESS,
name="Led Brightness",
icon="mdi:brightness-6",
min_value=0,
max_value=100,
step=1,
native_min_value=0,
native_max_value=100,
native_step=1,
method="async_set_led_brightness",
entity_category=EntityCategory.CONFIG,
),
@ -182,9 +182,9 @@ NUMBER_TYPES = {
key=ATTR_LED_BRIGHTNESS_LEVEL,
name="Led Brightness",
icon="mdi:brightness-6",
min_value=0,
max_value=8,
step=1,
native_min_value=0,
native_max_value=8,
native_step=1,
method="async_set_led_brightness_level",
entity_category=EntityCategory.CONFIG,
),
@ -192,10 +192,10 @@ NUMBER_TYPES = {
key=ATTR_FAVORITE_RPM,
name="Favorite Motor Speed",
icon="mdi:star-cog",
unit_of_measurement="rpm",
min_value=300,
max_value=2200,
step=10,
native_unit_of_measurement="rpm",
native_min_value=300,
native_max_value=2200,
native_step=10,
method="async_set_favorite_rpm",
entity_category=EntityCategory.CONFIG,
),
@ -298,7 +298,7 @@ class XiaomiNumberEntity(XiaomiCoordinatedMiioEntity, NumberEntity):
"""Initialize the generic Xiaomi attribute selector."""
super().__init__(name, device, entry, unique_id, coordinator)
self._attr_value = self._extract_value_from_attribute(
self._attr_native_value = self._extract_value_from_attribute(
coordinator.data, description.key
)
self.entity_description = description
@ -314,18 +314,18 @@ class XiaomiNumberEntity(XiaomiCoordinatedMiioEntity, NumberEntity):
return False
return super().available
async def async_set_value(self, value):
async def async_set_native_value(self, value):
"""Set an option of the miio device."""
method = getattr(self, self.entity_description.method)
if await method(int(value)):
self._attr_value = value
self._attr_native_value = value
self.async_write_ha_state()
@callback
def _handle_coordinator_update(self):
"""Fetch state from the device."""
# On state change the device doesn't provide the new state immediately.
self._attr_value = self._extract_value_from_attribute(
self._attr_native_value = self._extract_value_from_attribute(
self.coordinator.data, self.entity_description.key
)
self.async_write_ha_state()

View file

@ -45,15 +45,15 @@ class NumberCapability(MusicCastCapabilityEntity, NumberEntity):
) -> None:
"""Initialize the number entity."""
super().__init__(coordinator, capability, zone_id)
self._attr_min_value = capability.value_range.minimum
self._attr_max_value = capability.value_range.maximum
self._attr_step = capability.value_range.step
self._attr_native_min_value = capability.value_range.minimum
self._attr_native_max_value = capability.value_range.maximum
self._attr_native_step = capability.value_range.step
@property
def value(self):
def native_value(self):
"""Return the current value."""
return self.capability.current
async def async_set_value(self, value: float):
async def async_set_native_value(self, value: float):
"""Set a new value."""
await self.capability.set(value)

View file

@ -287,12 +287,12 @@ class ZhaNumber(ZhaEntity, NumberEntity):
)
@property
def value(self):
def native_value(self):
"""Return the current value."""
return self._analog_output_channel.present_value
@property
def min_value(self):
def native_min_value(self):
"""Return the minimum value."""
min_present_value = self._analog_output_channel.min_present_value
if min_present_value is not None:
@ -300,7 +300,7 @@ class ZhaNumber(ZhaEntity, NumberEntity):
return 0
@property
def max_value(self):
def native_max_value(self):
"""Return the maximum value."""
max_present_value = self._analog_output_channel.max_present_value
if max_present_value is not None:
@ -308,12 +308,12 @@ class ZhaNumber(ZhaEntity, NumberEntity):
return 1023
@property
def step(self):
def native_step(self):
"""Return the value step."""
resolution = self._analog_output_channel.resolution
if resolution is not None:
return resolution
return super().step
return super().native_step
@property
def name(self):
@ -332,7 +332,7 @@ class ZhaNumber(ZhaEntity, NumberEntity):
return super().icon
@property
def unit_of_measurement(self):
def native_unit_of_measurement(self):
"""Return the unit the value is expressed in."""
engineering_units = self._analog_output_channel.engineering_units
return UNITS.get(engineering_units)
@ -342,7 +342,7 @@ class ZhaNumber(ZhaEntity, NumberEntity):
"""Handle value update from channel."""
self.async_write_ha_state()
async def async_set_value(self, value):
async def async_set_native_value(self, value):
"""Update the current value from HA."""
num_value = float(value)
if await self._analog_output_channel.async_set_present_value(num_value):

View file

@ -71,34 +71,34 @@ class ZwaveNumberEntity(ZWaveBaseEntity, NumberEntity):
)
@property
def min_value(self) -> float:
def native_min_value(self) -> float:
"""Return the minimum value."""
if self.info.primary_value.metadata.min is None:
return 0
return float(self.info.primary_value.metadata.min)
@property
def max_value(self) -> float:
def native_max_value(self) -> float:
"""Return the maximum value."""
if self.info.primary_value.metadata.max is None:
return 255
return float(self.info.primary_value.metadata.max)
@property
def value(self) -> float | None:
def native_value(self) -> float | None:
"""Return the entity value."""
if self.info.primary_value.value is None:
return None
return float(self.info.primary_value.value)
@property
def unit_of_measurement(self) -> str | None:
def native_unit_of_measurement(self) -> str | None:
"""Return the unit of measurement of this entity, if any."""
if self.info.primary_value.metadata.unit is None:
return None
return str(self.info.primary_value.metadata.unit)
async def async_set_value(self, value: float) -> None:
async def async_set_native_value(self, value: float) -> None:
"""Set new value."""
if (target_value := self._target_value) is None:
raise HomeAssistantError("Missing target value on device.")
@ -121,19 +121,19 @@ class ZwaveVolumeNumberEntity(ZWaveBaseEntity, NumberEntity):
self.correction_factor = 1
# Entity class attributes
self._attr_min_value = 0
self._attr_max_value = 1
self._attr_step = 0.01
self._attr_native_min_value = 0
self._attr_native_max_value = 1
self._attr_native_step = 0.01
self._attr_name = self.generate_name(include_value_name=True)
@property
def value(self) -> float | None:
def native_value(self) -> float | None:
"""Return the entity value."""
if self.info.primary_value.value is None:
return None
return float(self.info.primary_value.value) / self.correction_factor
async def async_set_value(self, value: float) -> None:
async def async_set_native_value(self, value: float) -> None:
"""Set new value."""
await self.info.node.async_set_value(
self.info.primary_value, round(value * self.correction_factor)

View file

@ -40,13 +40,13 @@ class ZWaveMeNumber(ZWaveMeEntity, NumberEntity):
"""Representation of a ZWaveMe Multilevel Switch."""
@property
def value(self):
def native_value(self):
"""Return the unit of measurement."""
if self.device.level == 99: # Scale max value
return 100
return self.device.level
def set_value(self, value: float) -> None:
def set_native_value(self, value: float) -> None:
"""Update the current value."""
self.controller.zwave_api.send_command(
self.device.id, f"exact?level={str(round(value))}"