Use enums in xiaomi_miio (#61979)
This commit is contained in:
parent
32e1a3d063
commit
83cb2d11d5
6 changed files with 101 additions and 115 deletions
|
@ -15,10 +15,10 @@ from miio.gateway.gateway import (
|
|||
)
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
STATE_CLASS_TOTAL_INCREASING,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
AREA_SQUARE_METERS,
|
||||
|
@ -28,17 +28,6 @@ from homeassistant.const import (
|
|||
CONCENTRATION_PARTS_PER_MILLION,
|
||||
CONF_HOST,
|
||||
CONF_TOKEN,
|
||||
DEVICE_CLASS_BATTERY,
|
||||
DEVICE_CLASS_CO2,
|
||||
DEVICE_CLASS_GAS,
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_ILLUMINANCE,
|
||||
DEVICE_CLASS_PM25,
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_PRESSURE,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
DEVICE_CLASS_TIMESTAMP,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
LIGHT_LUX,
|
||||
PERCENTAGE,
|
||||
POWER_WATT,
|
||||
|
@ -49,6 +38,7 @@ from homeassistant.const import (
|
|||
VOLUME_CUBIC_METERS,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.entity import EntityCategory
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import VacuumCoordinatorDataAttributes
|
||||
|
@ -148,137 +138,137 @@ SENSOR_TYPES = {
|
|||
key=ATTR_TEMPERATURE,
|
||||
name="Temperature",
|
||||
native_unit_of_measurement=TEMP_CELSIUS,
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
ATTR_HUMIDITY: XiaomiMiioSensorDescription(
|
||||
key=ATTR_HUMIDITY,
|
||||
name="Humidity",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
device_class=DEVICE_CLASS_HUMIDITY,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=SensorDeviceClass.HUMIDITY,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
ATTR_PRESSURE: XiaomiMiioSensorDescription(
|
||||
key=ATTR_PRESSURE,
|
||||
name="Pressure",
|
||||
native_unit_of_measurement=PRESSURE_HPA,
|
||||
device_class=DEVICE_CLASS_PRESSURE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=SensorDeviceClass.PRESSURE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
ATTR_LOAD_POWER: XiaomiMiioSensorDescription(
|
||||
key=ATTR_LOAD_POWER,
|
||||
name="Load Power",
|
||||
native_unit_of_measurement=POWER_WATT,
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
),
|
||||
ATTR_WATER_LEVEL: XiaomiMiioSensorDescription(
|
||||
key=ATTR_WATER_LEVEL,
|
||||
name="Water Level",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
icon="mdi:water-check",
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
ATTR_ACTUAL_SPEED: XiaomiMiioSensorDescription(
|
||||
key=ATTR_ACTUAL_SPEED,
|
||||
name="Actual Speed",
|
||||
native_unit_of_measurement="rpm",
|
||||
icon="mdi:fast-forward",
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
ATTR_MOTOR_SPEED: XiaomiMiioSensorDescription(
|
||||
key=ATTR_MOTOR_SPEED,
|
||||
name="Motor Speed",
|
||||
native_unit_of_measurement="rpm",
|
||||
icon="mdi:fast-forward",
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
ATTR_MOTOR2_SPEED: XiaomiMiioSensorDescription(
|
||||
key=ATTR_MOTOR2_SPEED,
|
||||
name="Second Motor Speed",
|
||||
native_unit_of_measurement="rpm",
|
||||
icon="mdi:fast-forward",
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
ATTR_USE_TIME: XiaomiMiioSensorDescription(
|
||||
key=ATTR_USE_TIME,
|
||||
name="Use Time",
|
||||
native_unit_of_measurement=TIME_SECONDS,
|
||||
icon="mdi:progress-clock",
|
||||
state_class=STATE_CLASS_TOTAL_INCREASING,
|
||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
ATTR_ILLUMINANCE: XiaomiMiioSensorDescription(
|
||||
key=ATTR_ILLUMINANCE,
|
||||
name="Illuminance",
|
||||
native_unit_of_measurement=UNIT_LUMEN,
|
||||
device_class=DEVICE_CLASS_ILLUMINANCE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=SensorDeviceClass.ILLUMINANCE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
ATTR_ILLUMINANCE_LUX: XiaomiMiioSensorDescription(
|
||||
key=ATTR_ILLUMINANCE,
|
||||
name="Illuminance",
|
||||
native_unit_of_measurement=LIGHT_LUX,
|
||||
device_class=DEVICE_CLASS_ILLUMINANCE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=SensorDeviceClass.ILLUMINANCE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
ATTR_AIR_QUALITY: XiaomiMiioSensorDescription(
|
||||
key=ATTR_AIR_QUALITY,
|
||||
native_unit_of_measurement="AQI",
|
||||
icon="mdi:cloud",
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
ATTR_PM25: XiaomiMiioSensorDescription(
|
||||
key=ATTR_AQI,
|
||||
name="PM2.5",
|
||||
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||
device_class=DEVICE_CLASS_PM25,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=SensorDeviceClass.PM25,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
ATTR_FILTER_LIFE_REMAINING: XiaomiMiioSensorDescription(
|
||||
key=ATTR_FILTER_LIFE_REMAINING,
|
||||
name="Filter Life Remaining",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
icon="mdi:air-filter",
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
attributes=("filter_type",),
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
ATTR_FILTER_USE: XiaomiMiioSensorDescription(
|
||||
key=ATTR_FILTER_HOURS_USED,
|
||||
name="Filter Use",
|
||||
native_unit_of_measurement=TIME_HOURS,
|
||||
icon="mdi:clock-outline",
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
ATTR_CARBON_DIOXIDE: XiaomiMiioSensorDescription(
|
||||
key=ATTR_CARBON_DIOXIDE,
|
||||
name="Carbon Dioxide",
|
||||
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
|
||||
device_class=DEVICE_CLASS_CO2,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=SensorDeviceClass.CO2,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
ATTR_PURIFY_VOLUME: XiaomiMiioSensorDescription(
|
||||
key=ATTR_PURIFY_VOLUME,
|
||||
name="Purify Volume",
|
||||
native_unit_of_measurement=VOLUME_CUBIC_METERS,
|
||||
device_class=DEVICE_CLASS_GAS,
|
||||
state_class=STATE_CLASS_TOTAL_INCREASING,
|
||||
device_class=SensorDeviceClass.GAS,
|
||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
ATTR_BATTERY: XiaomiMiioSensorDescription(
|
||||
key=ATTR_BATTERY,
|
||||
name="Battery",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
device_class=DEVICE_CLASS_BATTERY,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
device_class=SensorDeviceClass.BATTERY,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
}
|
||||
|
||||
|
@ -408,35 +398,35 @@ VACUUM_SENSORS = {
|
|||
key=ATTR_DND_START,
|
||||
icon="mdi:minus-circle-off",
|
||||
name="DnD Start",
|
||||
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
parent_key=VacuumCoordinatorDataAttributes.dnd_status,
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"dnd_{ATTR_DND_END}": XiaomiMiioSensorDescription(
|
||||
key=ATTR_DND_END,
|
||||
icon="mdi:minus-circle-off",
|
||||
name="DnD End",
|
||||
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
parent_key=VacuumCoordinatorDataAttributes.dnd_status,
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"last_clean_{ATTR_LAST_CLEAN_START}": XiaomiMiioSensorDescription(
|
||||
key=ATTR_LAST_CLEAN_START,
|
||||
icon="mdi:clock-time-twelve",
|
||||
name="Last Clean Start",
|
||||
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
parent_key=VacuumCoordinatorDataAttributes.last_clean_details,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"last_clean_{ATTR_LAST_CLEAN_END}": XiaomiMiioSensorDescription(
|
||||
key=ATTR_LAST_CLEAN_END,
|
||||
icon="mdi:clock-time-twelve",
|
||||
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
parent_key=VacuumCoordinatorDataAttributes.last_clean_details,
|
||||
name="Last Clean End",
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"last_clean_{ATTR_LAST_CLEAN_TIME}": XiaomiMiioSensorDescription(
|
||||
native_unit_of_measurement=TIME_SECONDS,
|
||||
|
@ -444,7 +434,7 @@ VACUUM_SENSORS = {
|
|||
key=ATTR_LAST_CLEAN_TIME,
|
||||
parent_key=VacuumCoordinatorDataAttributes.last_clean_details,
|
||||
name="Last Clean Duration",
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"last_clean_{ATTR_LAST_CLEAN_AREA}": XiaomiMiioSensorDescription(
|
||||
native_unit_of_measurement=AREA_SQUARE_METERS,
|
||||
|
@ -452,7 +442,7 @@ VACUUM_SENSORS = {
|
|||
key=ATTR_LAST_CLEAN_AREA,
|
||||
parent_key=VacuumCoordinatorDataAttributes.last_clean_details,
|
||||
name="Last Clean Area",
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"clean_history_{ATTR_CLEAN_HISTORY_TOTAL_DURATION}": XiaomiMiioSensorDescription(
|
||||
native_unit_of_measurement=TIME_SECONDS,
|
||||
|
@ -461,7 +451,7 @@ VACUUM_SENSORS = {
|
|||
parent_key=VacuumCoordinatorDataAttributes.clean_history_status,
|
||||
name="Total duration",
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"clean_history_{ATTR_CLEAN_HISTORY_TOTAL_AREA}": XiaomiMiioSensorDescription(
|
||||
native_unit_of_measurement=AREA_SQUARE_METERS,
|
||||
|
@ -470,17 +460,17 @@ VACUUM_SENSORS = {
|
|||
parent_key=VacuumCoordinatorDataAttributes.clean_history_status,
|
||||
name="Total Clean Area",
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"clean_history_{ATTR_CLEAN_HISTORY_COUNT}": XiaomiMiioSensorDescription(
|
||||
native_unit_of_measurement="",
|
||||
icon="mdi:counter",
|
||||
state_class=STATE_CLASS_TOTAL_INCREASING,
|
||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||
key=ATTR_CLEAN_HISTORY_COUNT,
|
||||
parent_key=VacuumCoordinatorDataAttributes.clean_history_status,
|
||||
name="Total Clean Count",
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"clean_history_{ATTR_CLEAN_HISTORY_DUST_COLLECTION_COUNT}": XiaomiMiioSensorDescription(
|
||||
native_unit_of_measurement="",
|
||||
|
@ -490,7 +480,7 @@ VACUUM_SENSORS = {
|
|||
parent_key=VacuumCoordinatorDataAttributes.clean_history_status,
|
||||
name="Total Dust Collection Count",
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"consumable_{ATTR_CONSUMABLE_STATUS_MAIN_BRUSH_LEFT}": XiaomiMiioSensorDescription(
|
||||
native_unit_of_measurement=TIME_SECONDS,
|
||||
|
@ -499,7 +489,7 @@ VACUUM_SENSORS = {
|
|||
parent_key=VacuumCoordinatorDataAttributes.consumable_status,
|
||||
name="Main Brush Left",
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"consumable_{ATTR_CONSUMABLE_STATUS_SIDE_BRUSH_LEFT}": XiaomiMiioSensorDescription(
|
||||
native_unit_of_measurement=TIME_SECONDS,
|
||||
|
@ -508,7 +498,7 @@ VACUUM_SENSORS = {
|
|||
parent_key=VacuumCoordinatorDataAttributes.consumable_status,
|
||||
name="Side Brush Left",
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"consumable_{ATTR_CONSUMABLE_STATUS_FILTER_LEFT}": XiaomiMiioSensorDescription(
|
||||
native_unit_of_measurement=TIME_SECONDS,
|
||||
|
@ -517,7 +507,7 @@ VACUUM_SENSORS = {
|
|||
parent_key=VacuumCoordinatorDataAttributes.consumable_status,
|
||||
name="Filter Left",
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
f"consumable_{ATTR_CONSUMABLE_STATUS_SENSOR_DIRTY_LEFT}": XiaomiMiioSensorDescription(
|
||||
native_unit_of_measurement=TIME_SECONDS,
|
||||
|
@ -526,7 +516,7 @@ VACUUM_SENSORS = {
|
|||
parent_key=VacuumCoordinatorDataAttributes.consumable_status,
|
||||
name="Sensor Dirty Left",
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
}
|
||||
|
||||
|
@ -700,7 +690,7 @@ class XiaomiGenericSensor(XiaomiCoordinatedMiioEntity, SensorEntity):
|
|||
)
|
||||
|
||||
if (
|
||||
self.device_class == DEVICE_CLASS_TIMESTAMP
|
||||
self.device_class == SensorDeviceClass.TIMESTAMP
|
||||
and native_value is not None
|
||||
and (native_datetime := dt_util.parse_datetime(str(native_value)))
|
||||
is not None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue