Compare commits

...

8 Commits

Author SHA1 Message Date
tiger
c3ac9f2aeb
Merge 2b2a670e2d into 310029d8ed 2024-12-26 09:16:17 +08:00
Li Shuzhen
310029d8ed
chore: update issue template (#445)
Some checks failed
Tests / check-rule-format (push) Has been cancelled
Validate / validate-hassfest (push) Has been cancelled
Validate / validate-hacs (push) Has been cancelled
Validate / validate-lint (push) Has been cancelled
Validate / validate-setup (push) Has been cancelled
2024-12-26 09:11:18 +08:00
LiShuzhen
2b2a670e2d fix: merge conflicts 2024-12-24 19:38:47 +08:00
tiger
a6a73856c9 style: fix pylint format 2024-12-18 17:38:57 +08:00
tiger
fc0dedb399 style: fix pylint format 2024-12-17 21:00:30 +08:00
tiger
679af9dd62 feat:add support for state_class and unit process 2024-12-17 19:25:13 +08:00
tiger
9f9226ad29 feat: add optional state_class and unit_of_measurement for properties 2024-12-17 19:24:25 +08:00
tiger
6d2cb94284 feat: add state_class for sensor 2024-12-17 19:24:09 +08:00
5 changed files with 98 additions and 5 deletions

View File

@ -1,4 +1,4 @@
name: Bug report / 报告问题
name: Bug Report / 报告问题
description: Create a report to help us improve. / 报告问题以帮助我们改进
body:
- type: input
@ -33,12 +33,36 @@ body:
validations:
required: true
- type: input
attributes:
label: Reproduce Time / 问题复现的时间点
description: |
> Year-month-day, 24-hour time.
> 年-月-日24小时制。
placeholder: "2025-01-01 17:00:00"
validations:
required: true
- type: textarea
attributes:
label: Home Assistant Logs / 系统日志
description: |
> Please [set the log level](https://github.com/XiaoMi/ha_xiaomi_home/blob/main/CONTRIBUTING.md#reporting-bugs) to `debug` and try to reproduce the problem.
> [Settings > System > Logs > DOWNLOAD FULL LOG](https://my.home-assistant.io/redirect/logs) > Filter `xiaomi_home`
> If you are concerned about privacy, you can send the log to ha_xiaomi_home@xiaomi.com . The mail body should include the link to this issue.
> 请将[日志级别设置](https://github.com/XiaoMi/ha_xiaomi_home/blob/main/doc/CONTRIBUTING_zh.md#%E6%88%91%E5%8F%AF%E4%BB%A5%E5%A6%82%E4%BD%95%E8%B4%A1%E7%8C%AE)为 `debug` 并尝试复现问题。
> [设置 > 系统 > 日志 > 下载完整日志](https://my.home-assistant.io/redirect/logs) > 筛选 `xiaomi_home`
> 如果您担心隐私问题,可将日志发送至 ha_xiaomi_home@xiaomi.com ,邮件正文附上此问题的链接。
- type: input
attributes:
label: Log Timezone / 日志时区
description: |
> The [timezone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) of the timestamp in the log.
> 日志所用时间戳的[时区](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)。
placeholder: "Asia/Shanghai"
validations:
required: true
- type: input
attributes:

View File

@ -515,9 +515,18 @@ class MIoTDevice:
prop.icon = self.icon_convert(prop.unit)
device_class = SPEC_PROP_TRANS_MAP['properties'][prop_name][
'device_class']
prop.platform = 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')
if prop_optional_state_class:
result['state_class'] = prop_optional_state_class
return {'platform': platform, 'device_class': device_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."""
@ -544,6 +553,13 @@ class MIoTDevice:
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:

View File

@ -79,6 +79,7 @@ class MIoTSpecBase:
# External params
platform: str
device_class: Any
state_class: Any
icon: str
external_unit: Any
@ -96,6 +97,7 @@ class MIoTSpecBase:
self.platform = None
self.device_class = None
self.state_class = None
self.icon = None
self.external_unit = None

View File

@ -46,8 +46,16 @@ off Xiaomi or its affiliates' products.
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
{
@ -325,7 +333,11 @@ SPEC_SERVICE_TRANS_MAP: dict[str, dict | str] = {
'properties': {
'<property instance name>':{
'device_class': str,
'entity': str
'entity': str,
'optional':{
'state_class': str,
'unit_of_measurement': str
}
}
}
}
@ -381,7 +393,11 @@ SPEC_PROP_TRANS_MAP: dict[str, dict | str] = {
},
'voltage': {
'device_class': SensorDeviceClass.VOLTAGE,
'entity': 'sensor'
'entity': 'sensor',
'optional': {
'state_class': SensorStateClass.MEASUREMENT,
'unit_of_measurement': UnitOfElectricPotential.VOLT
}
},
'illumination': {
'device_class': SensorDeviceClass.ILLUMINANCE,
@ -391,6 +407,38 @@ SPEC_PROP_TRANS_MAP: dict[str, dict | str] = {
'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'
}

View File

@ -106,6 +106,9 @@ class Sensor(MIoTPropertyEntity, SensorEntity):
# 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: