Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat:add missing parameter state_class #101

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions custom_components/xiaomi_home/miot/miot_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,18 @@ def parse_miot_property_entity(
prop.icon = self.icon_convert(prop.unit)
device_class = SPEC_PROP_TRANS_MAP['properties'][prop_name][
'device_class']
prop.platform = device_class

return {'platform': platform, 'device_class': device_class}
result = {'platform': platform, 'device_class': device_class}
# optional:
optional = SPEC_PROP_TRANS_MAP['properties'][prop_name].get('optional')
if optional:
prop_optional_state_class = optional.get('state_class')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if 'state_class' in optional:
result['state_class'] = optional['state_class']

The same logic below

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, and test pass in local ha.

if prop_optional_state_class:
result['state_class'] = prop_optional_state_class

prop_optional_unit = optional.get('unit_of_measurement')
if prop_optional_unit and not prop.unit:
result['unit_of_measurement'] = prop_optional_unit
return result

def spec_transform(self) -> None:
"""Parse service, property, event, action from device spec."""
Expand All @@ -544,6 +553,13 @@ def spec_transform(self) -> None:
if prop_entity:
prop.platform = prop_entity['platform']
prop.device_class = prop_entity['device_class']
if 'state_class' in prop_entity:
prop.state_class = prop_entity['state_class']
if 'unit_of_measurement' in prop_entity:
prop.external_unit = self.unit_convert(
prop_entity['unit_of_measurement'])
prop.icon = self.icon_convert(
prop_entity['unit_of_measurement'])
# general conversion
if not prop.platform:
if prop.writable:
Expand Down
2 changes: 2 additions & 0 deletions custom_components/xiaomi_home/miot/miot_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class MIoTSpecBase:
# External params
platform: str
device_class: any
state_class: any
icon: str
external_unit: any

Expand All @@ -96,6 +97,7 @@ def __init__(self, spec: dict) -> None:

self.platform = None
self.device_class = None
self.state_class = None
self.icon = None
self.external_unit = None

Expand Down
52 changes: 50 additions & 2 deletions custom_components/xiaomi_home/miot/specs/specv2entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@
Conversion rules of MIoT-Spec-V2 instance to Home Assistant entity.
"""
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.components.sensor import SensorStateClass
from homeassistant.components.event import EventDeviceClass

from homeassistant.const import (
UnitOfEnergy,
UnitOfPower,
UnitOfElectricCurrent,
UnitOfElectricPotential,
)

# pylint: disable=pointless-string-statement
"""SPEC_DEVICE_TRANS_MAP
{
Expand Down Expand Up @@ -302,7 +310,11 @@
'properties': {
'<property instance name>':{
'device_class': str,
'entity': str
'entity': str,
'optional':{
'state_class': str,
'unit_of_measurement': str
SusanPhevos marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Expand Down Expand Up @@ -358,7 +370,11 @@
},
'voltage': {
'device_class': SensorDeviceClass.VOLTAGE,
'entity': 'sensor'
'entity': 'sensor',
'optional': {
'state_class': SensorStateClass.MEASUREMENT,
'unit_of_measurement': UnitOfElectricPotential.VOLT
}
},
'illumination': {
'device_class': SensorDeviceClass.ILLUMINANCE,
Expand All @@ -368,6 +384,38 @@
'device_class': SensorDeviceClass.DURATION,
'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',
'no-one-duration': 'no-one-determine-time'
}
Expand Down
3 changes: 3 additions & 0 deletions custom_components/xiaomi_home/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ def __init__(self, miot_device: MIoTDevice, spec: MIoTSpecProperty) -> None:
# Set icon
if spec.icon:
self._attr_icon = spec.icon
# Set state_class
if spec.state_class:
self._attr_state_class = spec.state_class

@property
def native_value(self) -> any:
Expand Down
Loading