feat: add optional state_class and unit_of_measurement for properties

This commit is contained in:
tiger 2024-12-17 19:24:25 +08:00
parent 6d2cb94284
commit 9f9226ad29

View File

@ -46,8 +46,16 @@ off Xiaomi or its affiliates' products.
Conversion rules of MIoT-Spec-V2 instance to Home Assistant entity. Conversion rules of MIoT-Spec-V2 instance to Home Assistant entity.
""" """
from homeassistant.components.sensor import SensorDeviceClass from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.components.sensor import SensorStateClass
from homeassistant.components.event import EventDeviceClass from homeassistant.components.event import EventDeviceClass
from homeassistant.const import (
UnitOfEnergy,
UnitOfPower,
UnitOfElectricCurrent,
UnitOfElectricPotential,
)
# pylint: disable=pointless-string-statement # pylint: disable=pointless-string-statement
"""SPEC_DEVICE_TRANS_MAP """SPEC_DEVICE_TRANS_MAP
{ {
@ -302,7 +310,11 @@ SPEC_SERVICE_TRANS_MAP: dict[str, dict | str] = {
'properties': { 'properties': {
'<property instance name>':{ '<property instance name>':{
'device_class': str, 'device_class': str,
'entity': str 'entity': str,
'optional':{
'state_class': str,
'unit_of_measurement': str
}
} }
} }
} }
@ -358,7 +370,11 @@ SPEC_PROP_TRANS_MAP: dict[str, dict | str] = {
}, },
'voltage': { 'voltage': {
'device_class': SensorDeviceClass.VOLTAGE, 'device_class': SensorDeviceClass.VOLTAGE,
'entity': 'sensor' 'entity': 'sensor',
'optional': {
'state_class': SensorStateClass.MEASUREMENT,
'unit_of_measurement': UnitOfElectricPotential.VOLT
}
}, },
'illumination': { 'illumination': {
'device_class': SensorDeviceClass.ILLUMINANCE, 'device_class': SensorDeviceClass.ILLUMINANCE,
@ -368,6 +384,38 @@ SPEC_PROP_TRANS_MAP: dict[str, dict | str] = {
'device_class': SensorDeviceClass.DURATION, 'device_class': SensorDeviceClass.DURATION,
'entity': 'sensor' 'entity': 'sensor'
}, },
'electric-power': {
'device_class': SensorDeviceClass.POWER,
'entity': 'sensor',
'optional': {
'state_class': SensorStateClass.MEASUREMENT,
'unit_of_measurement': UnitOfPower.WATT
}
},
'electric-current': {
'device_class': SensorDeviceClass.CURRENT,
'entity': 'sensor',
'optional': {
'state_class': SensorStateClass.MEASUREMENT,
'unit_of_measurement': UnitOfElectricCurrent.AMPERE
}
},
'power-consumption': {
'device_class': SensorDeviceClass.ENERGY,
'entity': 'sensor',
'optional': {
'state_class': SensorStateClass.TOTAL_INCREASING,
'unit_of_measurement': UnitOfEnergy.KILO_WATT_HOUR
}
},
'total-battery': {
'device_class': SensorDeviceClass.ENERGY,
'entity': 'sensor',
'optional': {
'state_class': SensorStateClass.TOTAL_INCREASING,
'unit_of_measurement': UnitOfEnergy.KILO_WATT_HOUR
}
},
'has-someone-duration': 'no-one-determine-time', 'has-someone-duration': 'no-one-determine-time',
'no-one-duration': 'no-one-determine-time' 'no-one-duration': 'no-one-determine-time'
} }