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: ) -> None:
"""Initialize the Number Entities.""" """Initialize the Number Entities."""
super().__init__(data, device, description) super().__init__(data, device, description)
self._attr_max_value = self.entity_description.ufp_max self._attr_native_max_value = self.entity_description.ufp_max
self._attr_min_value = self.entity_description.ufp_min self._attr_native_min_value = self.entity_description.ufp_min
self._attr_step = self.entity_description.ufp_step self._attr_native_step = self.entity_description.ufp_step
@callback @callback
def _async_update_device_from_protect(self) -> None: def _async_update_device_from_protect(self) -> None:
super()._async_update_device_from_protect() 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.""" """Set new value."""
await self.entity_description.ufp_set(self.device, 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( CHARGER_MAX_CHARGING_CURRENT_KEY: WallboxNumberEntityDescription(
key=CHARGER_MAX_CHARGING_CURRENT_KEY, key=CHARGER_MAX_CHARGING_CURRENT_KEY,
name="Max. Charging Current", 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]}" self._attr_unique_id = f"{description.key}-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
@property @property
def max_value(self) -> float: def native_max_value(self) -> float:
"""Return the maximum available current.""" """Return the maximum available current."""
return cast(float, self._coordinator.data[CHARGER_MAX_AVAILABLE_POWER_KEY]) return cast(float, self._coordinator.data[CHARGER_MAX_AVAILABLE_POWER_KEY])
@property @property
def value(self) -> float | None: def native_value(self) -> float | None:
"""Return the state of the sensor.""" """Return the state of the sensor."""
return cast( return cast(
Optional[float], self._coordinator.data[CHARGER_MAX_CHARGING_CURRENT_KEY] 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.""" """Set the value of the entity."""
await self._coordinator.async_set_charging_current(value) 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, ...] = ( NUMBERS: tuple[WizNumberEntityDescription, ...] = (
WizNumberEntityDescription( WizNumberEntityDescription(
key="effect_speed", key="effect_speed",
min_value=10, native_min_value=10,
max_value=200, native_max_value=200,
step=1, native_step=1,
icon="mdi:speedometer", icon="mdi:speedometer",
name="Effect Speed", name="Effect Speed",
value_fn=lambda device: cast(Optional[int], device.state.get_speed()), value_fn=lambda device: cast(Optional[int], device.state.get_speed()),
@ -59,9 +59,9 @@ NUMBERS: tuple[WizNumberEntityDescription, ...] = (
), ),
WizNumberEntityDescription( WizNumberEntityDescription(
key="dual_head_ratio", key="dual_head_ratio",
min_value=0, native_min_value=0,
max_value=100, native_max_value=100,
step=1, native_step=1,
icon="mdi:floor-lamp-dual", icon="mdi:floor-lamp-dual",
name="Dual Head Ratio", name="Dual Head Ratio",
value_fn=lambda device: cast(Optional[int], device.state.get_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: def _async_update_attrs(self) -> None:
"""Handle updating _attr values.""" """Handle updating _attr values."""
if (value := self.entity_description.value_fn(self._device)) is not None: 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.""" """Set the speed value."""
await self.entity_description.set_value_fn(self._device, int(value)) await self.entity_description.set_value_fn(self._device, int(value))
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()

View File

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

View File

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

View File

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

View File

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

View File

@ -71,34 +71,34 @@ class ZwaveNumberEntity(ZWaveBaseEntity, NumberEntity):
) )
@property @property
def min_value(self) -> float: def native_min_value(self) -> float:
"""Return the minimum value.""" """Return the minimum value."""
if self.info.primary_value.metadata.min is None: if self.info.primary_value.metadata.min is None:
return 0 return 0
return float(self.info.primary_value.metadata.min) return float(self.info.primary_value.metadata.min)
@property @property
def max_value(self) -> float: def native_max_value(self) -> float:
"""Return the maximum value.""" """Return the maximum value."""
if self.info.primary_value.metadata.max is None: if self.info.primary_value.metadata.max is None:
return 255 return 255
return float(self.info.primary_value.metadata.max) return float(self.info.primary_value.metadata.max)
@property @property
def value(self) -> float | None: def native_value(self) -> float | None:
"""Return the entity value.""" """Return the entity value."""
if self.info.primary_value.value is None: if self.info.primary_value.value is None:
return None return None
return float(self.info.primary_value.value) return float(self.info.primary_value.value)
@property @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.""" """Return the unit of measurement of this entity, if any."""
if self.info.primary_value.metadata.unit is None: if self.info.primary_value.metadata.unit is None:
return None return None
return str(self.info.primary_value.metadata.unit) 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.""" """Set new value."""
if (target_value := self._target_value) is None: if (target_value := self._target_value) is None:
raise HomeAssistantError("Missing target value on device.") raise HomeAssistantError("Missing target value on device.")
@ -121,19 +121,19 @@ class ZwaveVolumeNumberEntity(ZWaveBaseEntity, NumberEntity):
self.correction_factor = 1 self.correction_factor = 1
# Entity class attributes # Entity class attributes
self._attr_min_value = 0 self._attr_native_min_value = 0
self._attr_max_value = 1 self._attr_native_max_value = 1
self._attr_step = 0.01 self._attr_native_step = 0.01
self._attr_name = self.generate_name(include_value_name=True) self._attr_name = self.generate_name(include_value_name=True)
@property @property
def value(self) -> float | None: def native_value(self) -> float | None:
"""Return the entity value.""" """Return the entity value."""
if self.info.primary_value.value is None: if self.info.primary_value.value is None:
return None return None
return float(self.info.primary_value.value) / self.correction_factor 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.""" """Set new value."""
await self.info.node.async_set_value( await self.info.node.async_set_value(
self.info.primary_value, round(value * self.correction_factor) 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.""" """Representation of a ZWaveMe Multilevel Switch."""
@property @property
def value(self): def native_value(self):
"""Return the unit of measurement.""" """Return the unit of measurement."""
if self.device.level == 99: # Scale max value if self.device.level == 99: # Scale max value
return 100 return 100
return self.device.level return self.device.level
def set_value(self, value: float) -> None: def set_native_value(self, value: float) -> None:
"""Update the current value.""" """Update the current value."""
self.controller.zwave_api.send_command( self.controller.zwave_api.send_command(
self.device.id, f"exact?level={str(round(value))}" self.device.id, f"exact?level={str(round(value))}"