Use shorthand attributes in xiaomi_miio (part 2) (#145616)

* Use shorthand attributes in xiaomi_miio (part 2)

* Brightness

* Is_on
This commit is contained in:
epenet 2025-05-26 15:57:01 +02:00 committed by GitHub
parent a14f3ab6b1
commit c346b932f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 65 additions and 118 deletions

View File

@ -313,7 +313,6 @@ class XiaomiGenericDevice(
super().__init__(device, entry, unique_id, coordinator) super().__init__(device, entry, unique_id, coordinator)
self._available_attributes: dict[str, Any] = {} self._available_attributes: dict[str, Any] = {}
self._state: bool | None = None
self._mode: str | None = None self._mode: str | None = None
self._fan_level: int | None = None self._fan_level: int | None = None
self._state_attrs: dict[str, Any] = {} self._state_attrs: dict[str, Any] = {}
@ -340,11 +339,6 @@ class XiaomiGenericDevice(
"""Return the state attributes of the device.""" """Return the state attributes of the device."""
return self._state_attrs return self._state_attrs
@property
def is_on(self) -> bool | None:
"""Return true if device is on."""
return self._state
async def async_turn_on( async def async_turn_on(
self, self,
percentage: int | None = None, percentage: int | None = None,
@ -364,7 +358,7 @@ class XiaomiGenericDevice(
await self.async_set_preset_mode(preset_mode) await self.async_set_preset_mode(preset_mode)
if result: if result:
self._state = True self._attr_is_on = True
self.async_write_ha_state() self.async_write_ha_state()
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
@ -375,7 +369,7 @@ class XiaomiGenericDevice(
) )
if result: if result:
self._state = False self._attr_is_on = False
self.async_write_ha_state() self.async_write_ha_state()
@ -402,7 +396,7 @@ class XiaomiGenericAirPurifier(XiaomiGenericDevice):
@property @property
def preset_mode(self) -> str | None: def preset_mode(self) -> str | None:
"""Get the active preset mode.""" """Get the active preset mode."""
if self._state: if self._attr_is_on:
preset_mode = self.operation_mode_class(self._mode).name preset_mode = self.operation_mode_class(self._mode).name
return preset_mode if preset_mode in self._preset_modes else None return preset_mode if preset_mode in self._preset_modes else None
@ -411,7 +405,7 @@ class XiaomiGenericAirPurifier(XiaomiGenericDevice):
@callback @callback
def _handle_coordinator_update(self): def _handle_coordinator_update(self):
"""Fetch state from the device.""" """Fetch state from the device."""
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._state_attrs.update( self._state_attrs.update(
{ {
key: self._extract_value_from_attribute(self.coordinator.data, value) key: self._extract_value_from_attribute(self.coordinator.data, value)
@ -510,7 +504,7 @@ class XiaomiAirPurifier(XiaomiGenericAirPurifier):
FanEntityFeature.TURN_OFF | FanEntityFeature.TURN_ON FanEntityFeature.TURN_OFF | FanEntityFeature.TURN_ON
) )
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._state_attrs.update( self._state_attrs.update(
{ {
key: self._extract_value_from_attribute(self.coordinator.data, value) key: self._extract_value_from_attribute(self.coordinator.data, value)
@ -528,7 +522,7 @@ class XiaomiAirPurifier(XiaomiGenericAirPurifier):
@property @property
def percentage(self) -> int | None: def percentage(self) -> int | None:
"""Return the current percentage based speed.""" """Return the current percentage based speed."""
if self._state: if self._attr_is_on:
mode = self.operation_mode_class(self._mode) mode = self.operation_mode_class(self._mode)
if mode in self.REVERSE_SPEED_MODE_MAPPING: if mode in self.REVERSE_SPEED_MODE_MAPPING:
return ranged_value_to_percentage( return ranged_value_to_percentage(
@ -604,7 +598,7 @@ class XiaomiAirPurifierMiot(XiaomiAirPurifier):
"""Return the current percentage based speed.""" """Return the current percentage based speed."""
if self._fan_level is None: if self._fan_level is None:
return None return None
if self._state: if self._attr_is_on:
return ranged_value_to_percentage((1, 3), self._fan_level) return ranged_value_to_percentage((1, 3), self._fan_level)
return None return None
@ -652,7 +646,7 @@ class XiaomiAirPurifierMB4(XiaomiGenericAirPurifier):
| FanEntityFeature.TURN_ON | FanEntityFeature.TURN_ON
) )
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._mode = self.coordinator.data.mode.value self._mode = self.coordinator.data.mode.value
self._favorite_rpm: int | None = None self._favorite_rpm: int | None = None
self._speed_range = (300, 2200) self._speed_range = (300, 2200)
@ -671,7 +665,7 @@ class XiaomiAirPurifierMB4(XiaomiGenericAirPurifier):
return ranged_value_to_percentage(self._speed_range, self._motor_speed) return ranged_value_to_percentage(self._speed_range, self._motor_speed)
if self._favorite_rpm is None: if self._favorite_rpm is None:
return None return None
if self._state: if self._attr_is_on:
return ranged_value_to_percentage(self._speed_range, self._favorite_rpm) return ranged_value_to_percentage(self._speed_range, self._favorite_rpm)
return None return None
@ -698,7 +692,7 @@ class XiaomiAirPurifierMB4(XiaomiGenericAirPurifier):
async def async_set_preset_mode(self, preset_mode: str) -> None: async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode of the fan.""" """Set the preset mode of the fan."""
if not self._state: if not self._attr_is_on:
await self.async_turn_on() await self.async_turn_on()
if await self._try_command( if await self._try_command(
@ -712,7 +706,7 @@ class XiaomiAirPurifierMB4(XiaomiGenericAirPurifier):
@callback @callback
def _handle_coordinator_update(self): def _handle_coordinator_update(self):
"""Fetch state from the device.""" """Fetch state from the device."""
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._mode = self.coordinator.data.mode.value self._mode = self.coordinator.data.mode.value
self._favorite_rpm = getattr(self.coordinator.data, ATTR_FAVORITE_RPM, None) self._favorite_rpm = getattr(self.coordinator.data, ATTR_FAVORITE_RPM, None)
self._motor_speed = min( self._motor_speed = min(
@ -763,7 +757,7 @@ class XiaomiAirFresh(XiaomiGenericAirPurifier):
| FanEntityFeature.TURN_ON | FanEntityFeature.TURN_ON
) )
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._state_attrs.update( self._state_attrs.update(
{ {
key: getattr(self.coordinator.data, value) key: getattr(self.coordinator.data, value)
@ -780,7 +774,7 @@ class XiaomiAirFresh(XiaomiGenericAirPurifier):
@property @property
def percentage(self) -> int | None: def percentage(self) -> int | None:
"""Return the current percentage based speed.""" """Return the current percentage based speed."""
if self._state: if self._attr_is_on:
mode = AirfreshOperationMode(self._mode) mode = AirfreshOperationMode(self._mode)
if mode in self.REVERSE_SPEED_MODE_MAPPING: if mode in self.REVERSE_SPEED_MODE_MAPPING:
return ranged_value_to_percentage( return ranged_value_to_percentage(
@ -865,7 +859,7 @@ class XiaomiAirFreshA1(XiaomiGenericAirPurifier):
| FanEntityFeature.TURN_ON | FanEntityFeature.TURN_ON
) )
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._mode = self.coordinator.data.mode.value self._mode = self.coordinator.data.mode.value
self._speed_range = (60, 150) self._speed_range = (60, 150)
@ -879,7 +873,7 @@ class XiaomiAirFreshA1(XiaomiGenericAirPurifier):
"""Return the current percentage based speed.""" """Return the current percentage based speed."""
if self._favorite_speed is None: if self._favorite_speed is None:
return None return None
if self._state: if self._attr_is_on:
return ranged_value_to_percentage(self._speed_range, self._favorite_speed) return ranged_value_to_percentage(self._speed_range, self._favorite_speed)
return None return None
@ -918,7 +912,7 @@ class XiaomiAirFreshA1(XiaomiGenericAirPurifier):
@callback @callback
def _handle_coordinator_update(self): def _handle_coordinator_update(self):
"""Fetch state from the device.""" """Fetch state from the device."""
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._mode = self.coordinator.data.mode.value self._mode = self.coordinator.data.mode.value
self._favorite_speed = getattr(self.coordinator.data, ATTR_FAVORITE_SPEED, None) self._favorite_speed = getattr(self.coordinator.data, ATTR_FAVORITE_SPEED, None)
self.async_write_ha_state() self.async_write_ha_state()
@ -993,7 +987,7 @@ class XiaomiGenericFan(XiaomiGenericDevice):
@property @property
def percentage(self) -> int | None: def percentage(self) -> int | None:
"""Return the current speed as a percentage.""" """Return the current speed as a percentage."""
if self._state: if self._attr_is_on:
return self._percentage return self._percentage
return None return None
@ -1038,7 +1032,7 @@ class XiaomiFan(XiaomiGenericFan):
"""Initialize the fan.""" """Initialize the fan."""
super().__init__(device, entry, unique_id, coordinator) super().__init__(device, entry, unique_id, coordinator)
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._oscillating = self.coordinator.data.oscillate self._oscillating = self.coordinator.data.oscillate
self._nature_mode = self.coordinator.data.natural_speed != 0 self._nature_mode = self.coordinator.data.natural_speed != 0
if self._nature_mode: if self._nature_mode:
@ -1063,7 +1057,7 @@ class XiaomiFan(XiaomiGenericFan):
@callback @callback
def _handle_coordinator_update(self): def _handle_coordinator_update(self):
"""Fetch state from the device.""" """Fetch state from the device."""
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._oscillating = self.coordinator.data.oscillate self._oscillating = self.coordinator.data.oscillate
self._nature_mode = self.coordinator.data.natural_speed != 0 self._nature_mode = self.coordinator.data.natural_speed != 0
if self._nature_mode: if self._nature_mode:
@ -1131,7 +1125,7 @@ class XiaomiFanP5(XiaomiGenericFan):
"""Initialize the fan.""" """Initialize the fan."""
super().__init__(device, entry, unique_id, coordinator) super().__init__(device, entry, unique_id, coordinator)
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._preset_mode = self.coordinator.data.mode.name self._preset_mode = self.coordinator.data.mode.name
self._oscillating = self.coordinator.data.oscillate self._oscillating = self.coordinator.data.oscillate
self._percentage = self.coordinator.data.speed self._percentage = self.coordinator.data.speed
@ -1144,7 +1138,7 @@ class XiaomiFanP5(XiaomiGenericFan):
@callback @callback
def _handle_coordinator_update(self): def _handle_coordinator_update(self):
"""Fetch state from the device.""" """Fetch state from the device."""
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._preset_mode = self.coordinator.data.mode.name self._preset_mode = self.coordinator.data.mode.name
self._oscillating = self.coordinator.data.oscillate self._oscillating = self.coordinator.data.oscillate
self._percentage = self.coordinator.data.speed self._percentage = self.coordinator.data.speed
@ -1197,7 +1191,7 @@ class XiaomiFanMiot(XiaomiGenericFan):
@callback @callback
def _handle_coordinator_update(self): def _handle_coordinator_update(self):
"""Fetch state from the device.""" """Fetch state from the device."""
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._preset_mode = self.coordinator.data.mode.name self._preset_mode = self.coordinator.data.mode.name
self._oscillating = self.coordinator.data.oscillate self._oscillating = self.coordinator.data.oscillate
if self.coordinator.data.is_on: if self.coordinator.data.is_on:
@ -1264,7 +1258,7 @@ class XiaomiFan1C(XiaomiFanMiot):
@callback @callback
def _handle_coordinator_update(self): def _handle_coordinator_update(self):
"""Fetch state from the device.""" """Fetch state from the device."""
self._state = self.coordinator.data.is_on self._attr_is_on = self.coordinator.data.is_on
self._preset_mode = self.coordinator.data.mode.name self._preset_mode = self.coordinator.data.mode.name
self._oscillating = self.coordinator.data.oscillate self._oscillating = self.coordinator.data.oscillate
if self.coordinator.data.is_on: if self.coordinator.data.is_on:

View File

@ -271,8 +271,6 @@ class XiaomiPhilipsAbstractLight(XiaomiMiioEntity, LightEntity):
"""Initialize the light device.""" """Initialize the light device."""
super().__init__(name, device, entry, unique_id) super().__init__(name, device, entry, unique_id)
self._brightness = None
self._state = None
self._state_attrs: dict[str, Any] = {} self._state_attrs: dict[str, Any] = {}
@property @property
@ -280,16 +278,6 @@ class XiaomiPhilipsAbstractLight(XiaomiMiioEntity, LightEntity):
"""Return the state attributes of the device.""" """Return the state attributes of the device."""
return self._state_attrs return self._state_attrs
@property
def is_on(self):
"""Return true if light is on."""
return self._state
@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
return self._brightness
async def _try_command(self, mask_error, func, *args, **kwargs): async def _try_command(self, mask_error, func, *args, **kwargs):
"""Call a light command handling error messages.""" """Call a light command handling error messages."""
try: try:
@ -321,7 +309,7 @@ class XiaomiPhilipsAbstractLight(XiaomiMiioEntity, LightEntity):
) )
if result: if result:
self._brightness = brightness self._attr_brightness = brightness
else: else:
await self._try_command("Turning the light on failed.", self._device.on) await self._try_command("Turning the light on failed.", self._device.on)
@ -342,8 +330,8 @@ class XiaomiPhilipsAbstractLight(XiaomiMiioEntity, LightEntity):
_LOGGER.debug("Got new state: %s", state) _LOGGER.debug("Got new state: %s", state)
self._attr_available = True self._attr_available = True
self._state = state.is_on self._attr_is_on = state.is_on
self._brightness = ceil((255 / 100.0) * state.brightness) self._attr_brightness = ceil((255 / 100.0) * state.brightness)
class XiaomiPhilipsGenericLight(XiaomiPhilipsAbstractLight): class XiaomiPhilipsGenericLight(XiaomiPhilipsAbstractLight):
@ -376,8 +364,8 @@ class XiaomiPhilipsGenericLight(XiaomiPhilipsAbstractLight):
_LOGGER.debug("Got new state: %s", state) _LOGGER.debug("Got new state: %s", state)
self._attr_available = True self._attr_available = True
self._state = state.is_on self._attr_is_on = state.is_on
self._brightness = ceil((255 / 100.0) * state.brightness) self._attr_brightness = ceil((255 / 100.0) * state.brightness)
delayed_turn_off = self.delayed_turn_off_timestamp( delayed_turn_off = self.delayed_turn_off_timestamp(
state.delay_off_countdown, state.delay_off_countdown,
@ -510,7 +498,7 @@ class XiaomiPhilipsBulb(XiaomiPhilipsGenericLight):
if result: if result:
self._color_temp = color_temp self._color_temp = color_temp
self._brightness = brightness self._attr_brightness = brightness
elif ATTR_COLOR_TEMP_KELVIN in kwargs: elif ATTR_COLOR_TEMP_KELVIN in kwargs:
_LOGGER.debug( _LOGGER.debug(
@ -541,7 +529,7 @@ class XiaomiPhilipsBulb(XiaomiPhilipsGenericLight):
) )
if result: if result:
self._brightness = brightness self._attr_brightness = brightness
else: else:
await self._try_command("Turning the light on failed.", self._device.on) await self._try_command("Turning the light on failed.", self._device.on)
@ -559,8 +547,8 @@ class XiaomiPhilipsBulb(XiaomiPhilipsGenericLight):
_LOGGER.debug("Got new state: %s", state) _LOGGER.debug("Got new state: %s", state)
self._attr_available = True self._attr_available = True
self._state = state.is_on self._attr_is_on = state.is_on
self._brightness = ceil((255 / 100.0) * state.brightness) self._attr_brightness = ceil((255 / 100.0) * state.brightness)
self._color_temp = self.translate( self._color_temp = self.translate(
state.color_temperature, state.color_temperature,
CCT_MIN, CCT_MIN,
@ -630,8 +618,8 @@ class XiaomiPhilipsCeilingLamp(XiaomiPhilipsBulb):
_LOGGER.debug("Got new state: %s", state) _LOGGER.debug("Got new state: %s", state)
self._attr_available = True self._attr_available = True
self._state = state.is_on self._attr_is_on = state.is_on
self._brightness = ceil((255 / 100.0) * state.brightness) self._attr_brightness = ceil((255 / 100.0) * state.brightness)
self._color_temp = self.translate( self._color_temp = self.translate(
state.color_temperature, state.color_temperature,
CCT_MIN, CCT_MIN,
@ -688,8 +676,8 @@ class XiaomiPhilipsEyecareLamp(XiaomiPhilipsGenericLight):
_LOGGER.debug("Got new state: %s", state) _LOGGER.debug("Got new state: %s", state)
self._attr_available = True self._attr_available = True
self._state = state.is_on self._attr_is_on = state.is_on
self._brightness = ceil((255 / 100.0) * state.brightness) self._attr_brightness = ceil((255 / 100.0) * state.brightness)
delayed_turn_off = self.delayed_turn_off_timestamp( delayed_turn_off = self.delayed_turn_off_timestamp(
state.delay_off_countdown, state.delay_off_countdown,
@ -814,7 +802,7 @@ class XiaomiPhilipsEyecareLampAmbientLight(XiaomiPhilipsAbstractLight):
) )
if result: if result:
self._brightness = brightness self._attr_brightness = brightness
else: else:
await self._try_command( await self._try_command(
"Turning the ambient light on failed.", self._device.ambient_on "Turning the ambient light on failed.", self._device.ambient_on
@ -839,8 +827,8 @@ class XiaomiPhilipsEyecareLampAmbientLight(XiaomiPhilipsAbstractLight):
_LOGGER.debug("Got new state: %s", state) _LOGGER.debug("Got new state: %s", state)
self._attr_available = True self._attr_available = True
self._state = state.ambient self._attr_is_on = state.ambient
self._brightness = ceil((255 / 100.0) * state.ambient_brightness) self._attr_brightness = ceil((255 / 100.0) * state.ambient_brightness)
class XiaomiPhilipsMoonlightLamp(XiaomiPhilipsBulb): class XiaomiPhilipsMoonlightLamp(XiaomiPhilipsBulb):
@ -928,7 +916,7 @@ class XiaomiPhilipsMoonlightLamp(XiaomiPhilipsBulb):
if result: if result:
self._hs_color = hs_color self._hs_color = hs_color
self._brightness = brightness self._attr_brightness = brightness
elif ATTR_BRIGHTNESS in kwargs and ATTR_COLOR_TEMP_KELVIN in kwargs: elif ATTR_BRIGHTNESS in kwargs and ATTR_COLOR_TEMP_KELVIN in kwargs:
_LOGGER.debug( _LOGGER.debug(
@ -951,7 +939,7 @@ class XiaomiPhilipsMoonlightLamp(XiaomiPhilipsBulb):
if result: if result:
self._color_temp = color_temp self._color_temp = color_temp
self._brightness = brightness self._attr_brightness = brightness
elif ATTR_HS_COLOR in kwargs: elif ATTR_HS_COLOR in kwargs:
_LOGGER.debug("Setting color: %s", rgb) _LOGGER.debug("Setting color: %s", rgb)
@ -992,7 +980,7 @@ class XiaomiPhilipsMoonlightLamp(XiaomiPhilipsBulb):
) )
if result: if result:
self._brightness = brightness self._attr_brightness = brightness
else: else:
await self._try_command("Turning the light on failed.", self._device.on) await self._try_command("Turning the light on failed.", self._device.on)
@ -1010,8 +998,8 @@ class XiaomiPhilipsMoonlightLamp(XiaomiPhilipsBulb):
_LOGGER.debug("Got new state: %s", state) _LOGGER.debug("Got new state: %s", state)
self._attr_available = True self._attr_available = True
self._state = state.is_on self._attr_is_on = state.is_on
self._brightness = ceil((255 / 100.0) * state.brightness) self._attr_brightness = ceil((255 / 100.0) * state.brightness)
self._color_temp = self.translate( self._color_temp = self.translate(
state.color_temperature, state.color_temperature,
CCT_MIN, CCT_MIN,
@ -1050,7 +1038,6 @@ class XiaomiGatewayLight(LightEntity):
self._gateway_device_id = gateway_device_id self._gateway_device_id = gateway_device_id
self._attr_unique_id = gateway_device_id self._attr_unique_id = gateway_device_id
self._attr_available = False self._attr_available = False
self._is_on = None
self._brightness_pct = 100 self._brightness_pct = 100
self._rgb = (255, 255, 255) self._rgb = (255, 255, 255)
self._hs = (0, 0) self._hs = (0, 0)
@ -1062,11 +1049,6 @@ class XiaomiGatewayLight(LightEntity):
identifiers={(DOMAIN, self._gateway_device_id)}, identifiers={(DOMAIN, self._gateway_device_id)},
) )
@property
def is_on(self):
"""Return true if it is on."""
return self._is_on
@property @property
def brightness(self): def brightness(self):
"""Return the brightness of this light between 0..255.""" """Return the brightness of this light between 0..255."""
@ -1113,9 +1095,9 @@ class XiaomiGatewayLight(LightEntity):
return return
self._attr_available = True self._attr_available = True
self._is_on = state_dict["is_on"] self._attr_is_on = state_dict["is_on"]
if self._is_on: if self._attr_is_on:
self._brightness_pct = state_dict["brightness"] self._brightness_pct = state_dict["brightness"]
self._rgb = state_dict["rgb"] self._rgb = state_dict["rgb"]
self._hs = color_util.color_RGB_to_hs(*self._rgb) self._hs = color_util.color_RGB_to_hs(*self._rgb)
@ -1139,7 +1121,7 @@ class XiaomiGatewayBulb(XiaomiGatewayDevice, LightEntity):
return self._sub_device.status["color_temp"] return self._sub_device.status["color_temp"]
@property @property
def is_on(self): def is_on(self) -> bool:
"""Return true if light is on.""" """Return true if light is on."""
return self._sub_device.status["status"] == "on" return self._sub_device.status["status"] == "on"

View File

@ -938,7 +938,6 @@ class XiaomiAirQualityMonitor(XiaomiMiioEntity, SensorEntity):
"""Initialize the entity.""" """Initialize the entity."""
super().__init__(name, device, entry, unique_id) super().__init__(name, device, entry, unique_id)
self._state = None
self._state_attrs = { self._state_attrs = {
ATTR_POWER: None, ATTR_POWER: None,
ATTR_BATTERY_LEVEL: None, ATTR_BATTERY_LEVEL: None,
@ -951,11 +950,6 @@ class XiaomiAirQualityMonitor(XiaomiMiioEntity, SensorEntity):
} }
self.entity_description = description self.entity_description = description
@property
def native_value(self):
"""Return the state of the device."""
return self._state
@property @property
def extra_state_attributes(self): def extra_state_attributes(self):
"""Return the state attributes of the device.""" """Return the state attributes of the device."""
@ -968,7 +962,7 @@ class XiaomiAirQualityMonitor(XiaomiMiioEntity, SensorEntity):
_LOGGER.debug("Got new state: %s", state) _LOGGER.debug("Got new state: %s", state)
self._attr_available = True self._attr_available = True
self._state = state.aqi self._attr_native_value = state.aqi
self._state_attrs.update( self._state_attrs.update(
{ {
ATTR_POWER: state.power, ATTR_POWER: state.power,
@ -1023,17 +1017,11 @@ class XiaomiGatewayIlluminanceSensor(SensorEntity):
self._gateway = gateway_device self._gateway = gateway_device
self.entity_description = description self.entity_description = description
self._attr_available = False self._attr_available = False
self._state = None
@property
def native_value(self):
"""Return the state of the device."""
return self._state
async def async_update(self) -> None: async def async_update(self) -> None:
"""Fetch state from the device.""" """Fetch state from the device."""
try: try:
self._state = await self.hass.async_add_executor_job( self._attr_native_value = await self.hass.async_add_executor_job(
self._gateway.get_illumination self._gateway.get_illumination
) )
self._attr_available = True self._attr_available = True

View File

@ -783,7 +783,7 @@ class XiaomiGatewaySwitch(XiaomiGatewayDevice, SwitchEntity):
self._attr_name = f"{sub_device.name} ch{self._channel} ({sub_device.sid})" self._attr_name = f"{sub_device.name} ch{self._channel} ({sub_device.sid})"
@property @property
def is_on(self): def is_on(self) -> bool:
"""Return true if switch is on.""" """Return true if switch is on."""
return self._sub_device.status[self._data_key] == "on" return self._sub_device.status[self._data_key] == "on"
@ -816,7 +816,6 @@ class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
"""Initialize the plug switch.""" """Initialize the plug switch."""
super().__init__(name, device, entry, unique_id) super().__init__(name, device, entry, unique_id)
self._state: bool | None = None
self._state_attrs = {ATTR_TEMPERATURE: None, ATTR_MODEL: self._model} self._state_attrs = {ATTR_TEMPERATURE: None, ATTR_MODEL: self._model}
self._device_features = FEATURE_FLAGS_GENERIC self._device_features = FEATURE_FLAGS_GENERIC
self._skip_update = False self._skip_update = False
@ -826,11 +825,6 @@ class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
"""Return the state attributes of the device.""" """Return the state attributes of the device."""
return self._state_attrs return self._state_attrs
@property
def is_on(self):
"""Return true if switch is on."""
return self._state
async def _try_command(self, mask_error, func, *args, **kwargs): async def _try_command(self, mask_error, func, *args, **kwargs):
"""Call a plug command handling error messages.""" """Call a plug command handling error messages."""
try: try:
@ -857,7 +851,7 @@ class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
result = await self._try_command("Turning the plug on failed", self._device.on) result = await self._try_command("Turning the plug on failed", self._device.on)
if result: if result:
self._state = True self._attr_is_on = True
self._skip_update = True self._skip_update = True
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
@ -867,7 +861,7 @@ class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
) )
if result: if result:
self._state = False self._attr_is_on = False
self._skip_update = True self._skip_update = True
async def async_update(self) -> None: async def async_update(self) -> None:
@ -882,7 +876,7 @@ class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
_LOGGER.debug("Got new state: %s", state) _LOGGER.debug("Got new state: %s", state)
self._attr_available = True self._attr_available = True
self._state = state.is_on self._attr_is_on = state.is_on
self._state_attrs[ATTR_TEMPERATURE] = state.temperature self._state_attrs[ATTR_TEMPERATURE] = state.temperature
except DeviceException as ex: except DeviceException as ex:
@ -963,7 +957,7 @@ class XiaomiPowerStripSwitch(XiaomiPlugGenericSwitch):
_LOGGER.debug("Got new state: %s", state) _LOGGER.debug("Got new state: %s", state)
self._attr_available = True self._attr_available = True
self._state = state.is_on self._attr_is_on = state.is_on
self._state_attrs.update( self._state_attrs.update(
{ATTR_TEMPERATURE: state.temperature, ATTR_LOAD_POWER: state.load_power} {ATTR_TEMPERATURE: state.temperature, ATTR_LOAD_POWER: state.load_power}
) )
@ -1039,7 +1033,7 @@ class ChuangMiPlugSwitch(XiaomiPlugGenericSwitch):
) )
if result: if result:
self._state = True self._attr_is_on = True
self._skip_update = True self._skip_update = True
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
@ -1055,7 +1049,7 @@ class ChuangMiPlugSwitch(XiaomiPlugGenericSwitch):
) )
if result: if result:
self._state = False self._attr_is_on = False
self._skip_update = True self._skip_update = True
async def async_update(self) -> None: async def async_update(self) -> None:
@ -1071,9 +1065,9 @@ class ChuangMiPlugSwitch(XiaomiPlugGenericSwitch):
self._attr_available = True self._attr_available = True
if self._channel_usb: if self._channel_usb:
self._state = state.usb_power self._attr_is_on = state.usb_power
else: else:
self._state = state.is_on self._attr_is_on = state.is_on
self._state_attrs[ATTR_TEMPERATURE] = state.temperature self._state_attrs[ATTR_TEMPERATURE] = state.temperature
@ -1114,7 +1108,7 @@ class XiaomiAirConditioningCompanionSwitch(XiaomiPlugGenericSwitch):
) )
if result: if result:
self._state = True self._attr_is_on = True
self._skip_update = True self._skip_update = True
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
@ -1125,7 +1119,7 @@ class XiaomiAirConditioningCompanionSwitch(XiaomiPlugGenericSwitch):
) )
if result: if result:
self._state = False self._attr_is_on = False
self._skip_update = True self._skip_update = True
async def async_update(self) -> None: async def async_update(self) -> None:
@ -1140,7 +1134,7 @@ class XiaomiAirConditioningCompanionSwitch(XiaomiPlugGenericSwitch):
_LOGGER.debug("Got new state: %s", state) _LOGGER.debug("Got new state: %s", state)
self._attr_available = True self._attr_available = True
self._state = state.power_socket == "on" self._attr_is_on = state.power_socket == "on"
self._state_attrs[ATTR_LOAD_POWER] = state.load_power self._state_attrs[ATTR_LOAD_POWER] = state.load_power
except DeviceException as ex: except DeviceException as ex:

View File

@ -6,7 +6,7 @@ from functools import partial
import logging import logging
from typing import Any from typing import Any
from miio import Device as MiioDevice, DeviceException from miio import DeviceException
import voluptuous as vol import voluptuous as vol
from homeassistant.components.vacuum import ( from homeassistant.components.vacuum import (
@ -194,17 +194,6 @@ class MiroboVacuum(
| VacuumEntityFeature.START | VacuumEntityFeature.START
) )
def __init__(
self,
device: MiioDevice,
entry: XiaomiMiioConfigEntry,
unique_id: str | None,
coordinator: DataUpdateCoordinator[VacuumCoordinatorData],
) -> None:
"""Initialize the Xiaomi vacuum cleaner robot handler."""
super().__init__(device, entry, unique_id, coordinator)
self._state: VacuumActivity | None = None
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Run when entity is about to be added to hass.""" """Run when entity is about to be added to hass."""
await super().async_added_to_hass() await super().async_added_to_hass()
@ -218,7 +207,7 @@ class MiroboVacuum(
if self.coordinator.data.status.got_error: if self.coordinator.data.status.got_error:
return VacuumActivity.ERROR return VacuumActivity.ERROR
return self._state return super().activity
@property @property
def battery_level(self) -> int: def battery_level(self) -> int:
@ -435,8 +424,8 @@ class MiroboVacuum(
self.coordinator.data.status.state, self.coordinator.data.status.state,
self.coordinator.data.status.state_code, self.coordinator.data.status.state_code,
) )
self._state = None self._attr_activity = None
else: else:
self._state = STATE_CODE_TO_STATE[state_code] self._attr_activity = STATE_CODE_TO_STATE[state_code]
super()._handle_coordinator_update() super()._handle_coordinator_update()