Use shorthand attributes in xiaomi_miio (#145614)

This commit is contained in:
epenet 2025-05-26 15:07:05 +02:00 committed by GitHub
parent 2d5867cab6
commit d3275c3833
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 78 additions and 171 deletions

View File

@ -45,6 +45,8 @@ PROP_TO_ATTR = {
class AirMonitorB1(XiaomiMiioEntity, AirQualityEntity):
"""Air Quality class for Xiaomi cgllc.airmonitor.b1 device."""
_attr_icon = "mdi:cloud"
def __init__(
self,
name: str,
@ -55,7 +57,6 @@ class AirMonitorB1(XiaomiMiioEntity, AirQualityEntity):
"""Initialize the entity."""
super().__init__(name, device, entry, unique_id)
self._icon = "mdi:cloud"
self._air_quality_index = None
self._carbon_dioxide = None
self._carbon_dioxide_equivalent = None
@ -74,21 +75,11 @@ class AirMonitorB1(XiaomiMiioEntity, AirQualityEntity):
self._total_volatile_organic_compounds = round(state.tvoc, 3)
self._temperature = round(state.temperature, 2)
self._humidity = round(state.humidity, 2)
self._available = True
self._attr_available = True
except DeviceException as ex:
self._available = False
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
@property
def icon(self):
"""Return the icon to use for device if any."""
return self._icon
@property
def available(self):
"""Return true when state is known."""
return self._available
@property
def air_quality_index(self):
"""Return the Air Quality Index (AQI)."""
@ -149,10 +140,10 @@ class AirMonitorS1(AirMonitorB1):
self._total_volatile_organic_compounds = state.tvoc
self._temperature = state.temperature
self._humidity = state.humidity
self._available = True
self._attr_available = True
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
@ -165,10 +156,10 @@ class AirMonitorV1(AirMonitorB1):
state = await self.hass.async_add_executor_job(self._device.status)
_LOGGER.debug("Got new state: %s", state)
self._air_quality_index = state.aqi
self._available = True
self._attr_available = True
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
@property
@ -180,6 +171,8 @@ class AirMonitorV1(AirMonitorB1):
class AirMonitorCGDN1(XiaomiMiioEntity, AirQualityEntity):
"""Air Quality class for cgllc.airm.cgdn1 device."""
_attr_icon = "mdi:cloud"
def __init__(
self,
name: str,
@ -190,7 +183,6 @@ class AirMonitorCGDN1(XiaomiMiioEntity, AirQualityEntity):
"""Initialize the entity."""
super().__init__(name, device, entry, unique_id)
self._icon = "mdi:cloud"
self._carbon_dioxide = None
self._particulate_matter_2_5 = None
self._particulate_matter_10 = None
@ -203,21 +195,11 @@ class AirMonitorCGDN1(XiaomiMiioEntity, AirQualityEntity):
self._carbon_dioxide = state.co2
self._particulate_matter_2_5 = round(state.pm25, 1)
self._particulate_matter_10 = round(state.pm10, 1)
self._available = True
self._attr_available = True
except DeviceException as ex:
self._available = False
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
@property
def icon(self):
"""Return the icon to use for device if any."""
return self._icon
@property
def available(self):
"""Return true when state is known."""
return self._available
@property
def carbon_dioxide(self):
"""Return the CO2 (carbon dioxide) level."""

View File

@ -39,19 +39,9 @@ class XiaomiMiioEntity(Entity):
self._model = entry.data[CONF_MODEL]
self._mac = entry.data[CONF_MAC]
self._device_id = entry.unique_id
self._unique_id = unique_id
self._name = name
self._available = False
@property
def unique_id(self):
"""Return an unique ID."""
return self._unique_id
@property
def name(self):
"""Return the name of this entity, if any."""
return self._name
self._attr_unique_id = unique_id
self._attr_name = name
self._attr_available = False
@property
def device_info(self) -> DeviceInfo:
@ -62,7 +52,7 @@ class XiaomiMiioEntity(Entity):
identifiers={(DOMAIN, self._device_id)},
manufacturer="Xiaomi",
model=self._model,
name=self._name,
name=self._attr_name,
)
if self._mac is not None:
@ -92,12 +82,7 @@ class XiaomiCoordinatedMiioEntity[_T: DataUpdateCoordinator[Any]](
self._mac = entry.data[CONF_MAC]
self._device_id = entry.unique_id
self._device_name = entry.title
self._unique_id = unique_id
@property
def unique_id(self):
"""Return an unique ID."""
return self._unique_id
self._attr_unique_id = unique_id
@property
def device_info(self) -> DeviceInfo:
@ -183,18 +168,8 @@ class XiaomiGatewayDevice(
super().__init__(coordinator)
self._sub_device = sub_device
self._entry = entry
self._unique_id = sub_device.sid
self._name = f"{sub_device.name} ({sub_device.sid})"
@property
def unique_id(self):
"""Return an unique ID."""
return self._unique_id
@property
def name(self):
"""Return the name of this entity, if any."""
return self._name
self._attr_unique_id = sub_device.sid
self._attr_name = f"{sub_device.name} ({sub_device.sid})"
@property
def device_info(self) -> DeviceInfo:

View File

@ -275,11 +275,6 @@ class XiaomiPhilipsAbstractLight(XiaomiMiioEntity, LightEntity):
self._state = None
self._state_attrs: dict[str, Any] = {}
@property
def available(self) -> bool:
"""Return true when state is known."""
return self._available
@property
def extra_state_attributes(self):
"""Return the state attributes of the device."""
@ -302,9 +297,9 @@ class XiaomiPhilipsAbstractLight(XiaomiMiioEntity, LightEntity):
partial(func, *args, **kwargs)
)
except DeviceException as exc:
if self._available:
if self._attr_available:
_LOGGER.error(mask_error, exc)
self._available = False
self._attr_available = False
return False
@ -339,14 +334,14 @@ class XiaomiPhilipsAbstractLight(XiaomiMiioEntity, LightEntity):
try:
state = await self.hass.async_add_executor_job(self._device.status)
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
return
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._attr_available = True
self._state = state.is_on
self._brightness = ceil((255 / 100.0) * state.brightness)
@ -373,14 +368,14 @@ class XiaomiPhilipsGenericLight(XiaomiPhilipsAbstractLight):
try:
state = await self.hass.async_add_executor_job(self._device.status)
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
return
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._attr_available = True
self._state = state.is_on
self._brightness = ceil((255 / 100.0) * state.brightness)
@ -556,14 +551,14 @@ class XiaomiPhilipsBulb(XiaomiPhilipsGenericLight):
try:
state = await self.hass.async_add_executor_job(self._device.status)
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
return
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._attr_available = True
self._state = state.is_on
self._brightness = ceil((255 / 100.0) * state.brightness)
self._color_temp = self.translate(
@ -627,14 +622,14 @@ class XiaomiPhilipsCeilingLamp(XiaomiPhilipsBulb):
try:
state = await self.hass.async_add_executor_job(self._device.status)
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
return
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._attr_available = True
self._state = state.is_on
self._brightness = ceil((255 / 100.0) * state.brightness)
self._color_temp = self.translate(
@ -685,14 +680,14 @@ class XiaomiPhilipsEyecareLamp(XiaomiPhilipsGenericLight):
try:
state = await self.hass.async_add_executor_job(self._device.status)
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
return
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._attr_available = True
self._state = state.is_on
self._brightness = ceil((255 / 100.0) * state.brightness)
@ -836,14 +831,14 @@ class XiaomiPhilipsEyecareLampAmbientLight(XiaomiPhilipsAbstractLight):
try:
state = await self.hass.async_add_executor_job(self._device.status)
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
return
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._attr_available = True
self._state = state.ambient
self._brightness = ceil((255 / 100.0) * state.ambient_brightness)
@ -1007,14 +1002,14 @@ class XiaomiPhilipsMoonlightLamp(XiaomiPhilipsBulb):
try:
state = await self.hass.async_add_executor_job(self._device.status)
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
return
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._attr_available = True
self._state = state.is_on
self._brightness = ceil((255 / 100.0) * state.brightness)
self._color_temp = self.translate(
@ -1051,20 +1046,15 @@ class XiaomiGatewayLight(LightEntity):
def __init__(self, gateway_device, gateway_name, gateway_device_id):
"""Initialize the XiaomiGatewayLight."""
self._gateway = gateway_device
self._name = f"{gateway_name} Light"
self._attr_name = f"{gateway_name} Light"
self._gateway_device_id = gateway_device_id
self._unique_id = gateway_device_id
self._available = False
self._attr_unique_id = gateway_device_id
self._attr_available = False
self._is_on = None
self._brightness_pct = 100
self._rgb = (255, 255, 255)
self._hs = (0, 0)
@property
def unique_id(self):
"""Return an unique ID."""
return self._unique_id
@property
def device_info(self) -> DeviceInfo:
"""Return the device info of the gateway."""
@ -1072,16 +1062,6 @@ class XiaomiGatewayLight(LightEntity):
identifiers={(DOMAIN, self._gateway_device_id)},
)
@property
def name(self):
"""Return the name of this entity, if any."""
return self._name
@property
def available(self) -> bool:
"""Return true when state is known."""
return self._available
@property
def is_on(self):
"""Return true if it is on."""
@ -1125,14 +1105,14 @@ class XiaomiGatewayLight(LightEntity):
self._gateway.light.rgb_status
)
except GatewayException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error(
"Got exception while fetching the gateway light state: %s", ex
)
return
self._available = True
self._attr_available = True
self._is_on = state_dict["is_on"]
if self._is_on:

View File

@ -187,24 +187,14 @@ class XiaomiMiioRemote(RemoteEntity):
def __init__(self, friendly_name, device, unique_id, slot, timeout, commands):
"""Initialize the remote."""
self._name = friendly_name
self._attr_name = friendly_name
self._device = device
self._unique_id = unique_id
self._attr_unique_id = unique_id
self._slot = slot
self._timeout = timeout
self._state = False
self._commands = commands
@property
def unique_id(self):
"""Return an unique ID."""
return self._unique_id
@property
def name(self):
"""Return the name of the remote."""
return self._name
@property
def device(self):
"""Return the remote object."""

View File

@ -951,11 +951,6 @@ class XiaomiAirQualityMonitor(XiaomiMiioEntity, SensorEntity):
}
self.entity_description = description
@property
def available(self) -> bool:
"""Return true when state is known."""
return self._available
@property
def native_value(self):
"""Return the state of the device."""
@ -972,7 +967,7 @@ class XiaomiAirQualityMonitor(XiaomiMiioEntity, SensorEntity):
state = await self.hass.async_add_executor_job(self._device.status)
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._attr_available = True
self._state = state.aqi
self._state_attrs.update(
{
@ -988,8 +983,8 @@ class XiaomiAirQualityMonitor(XiaomiMiioEntity, SensorEntity):
)
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
@ -1005,8 +1000,8 @@ class XiaomiGatewaySensor(XiaomiGatewayDevice, SensorEntity):
) -> None:
"""Initialize the XiaomiSensor."""
super().__init__(coordinator, sub_device, entry)
self._unique_id = f"{sub_device.sid}-{description.key}"
self._name = f"{description.key} ({sub_device.sid})".capitalize()
self._attr_unique_id = f"{sub_device.sid}-{description.key}"
self._attr_name = f"{description.key} ({sub_device.sid})".capitalize()
self.entity_description = description
@property
@ -1027,14 +1022,9 @@ class XiaomiGatewayIlluminanceSensor(SensorEntity):
)
self._gateway = gateway_device
self.entity_description = description
self._available = False
self._attr_available = False
self._state = None
@property
def available(self) -> bool:
"""Return true when state is known."""
return self._available
@property
def native_value(self):
"""Return the state of the device."""
@ -1046,10 +1036,10 @@ class XiaomiGatewayIlluminanceSensor(SensorEntity):
self._state = await self.hass.async_add_executor_job(
self._gateway.get_illumination
)
self._available = True
self._attr_available = True
except GatewayException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error(
"Got exception while fetching the gateway illuminance state: %s", ex
)

View File

@ -779,8 +779,8 @@ class XiaomiGatewaySwitch(XiaomiGatewayDevice, SwitchEntity):
super().__init__(coordinator, sub_device, entry)
self._channel = GATEWAY_SWITCH_VARS[variable][KEY_CHANNEL]
self._data_key = f"status_ch{self._channel}"
self._unique_id = f"{sub_device.sid}-ch{self._channel}"
self._name = f"{sub_device.name} ch{self._channel} ({sub_device.sid})"
self._attr_unique_id = f"{sub_device.sid}-ch{self._channel}"
self._attr_name = f"{sub_device.name} ch{self._channel} ({sub_device.sid})"
@property
def is_on(self):
@ -803,6 +803,7 @@ class XiaomiGatewaySwitch(XiaomiGatewayDevice, SwitchEntity):
class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
"""Representation of a Xiaomi Plug Generic."""
_attr_icon = "mdi:power-socket"
_device: AirConditioningCompanionV3 | ChuangmiPlug | PowerStrip
def __init__(
@ -815,22 +816,11 @@ class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
"""Initialize the plug switch."""
super().__init__(name, device, entry, unique_id)
self._icon = "mdi:power-socket"
self._state: bool | None = None
self._state_attrs = {ATTR_TEMPERATURE: None, ATTR_MODEL: self._model}
self._device_features = FEATURE_FLAGS_GENERIC
self._skip_update = False
@property
def icon(self):
"""Return the icon to use for device if any."""
return self._icon
@property
def available(self) -> bool:
"""Return true when state is known."""
return self._available
@property
def extra_state_attributes(self):
"""Return the state attributes of the device."""
@ -848,9 +838,9 @@ class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
partial(func, *args, **kwargs)
)
except DeviceException as exc:
if self._available:
if self._attr_available:
_LOGGER.error(mask_error, exc)
self._available = False
self._attr_available = False
return False
@ -891,13 +881,13 @@ class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
state = await self.hass.async_add_executor_job(self._device.status)
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._attr_available = True
self._state = state.is_on
self._state_attrs[ATTR_TEMPERATURE] = state.temperature
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
async def async_set_wifi_led_on(self):
@ -972,7 +962,7 @@ class XiaomiPowerStripSwitch(XiaomiPlugGenericSwitch):
state = await self.hass.async_add_executor_job(self._device.status)
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._attr_available = True
self._state = state.is_on
self._state_attrs.update(
{ATTR_TEMPERATURE: state.temperature, ATTR_LOAD_POWER: state.load_power}
@ -991,8 +981,8 @@ class XiaomiPowerStripSwitch(XiaomiPlugGenericSwitch):
self._state_attrs[ATTR_POWER_PRICE] = state.power_price
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
async def async_set_power_mode(self, mode: str):
@ -1079,7 +1069,7 @@ class ChuangMiPlugSwitch(XiaomiPlugGenericSwitch):
state = await self.hass.async_add_executor_job(self._device.status)
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._attr_available = True
if self._channel_usb:
self._state = state.usb_power
else:
@ -1094,8 +1084,8 @@ class ChuangMiPlugSwitch(XiaomiPlugGenericSwitch):
self._state_attrs[ATTR_LOAD_POWER] = state.load_power
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
@ -1149,11 +1139,11 @@ class XiaomiAirConditioningCompanionSwitch(XiaomiPlugGenericSwitch):
state = await self.hass.async_add_executor_job(self._device.status)
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._attr_available = True
self._state = state.power_socket == "on"
self._state_attrs[ATTR_LOAD_POWER] = state.load_power
except DeviceException as ex:
if self._available:
self._available = False
if self._attr_available:
self._attr_available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)