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

修复枚举值类型报错的问题 & 补充一些unit #39

Merged
merged 2 commits into from
Jul 25, 2023
Merged
Changes from all 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
30 changes: 25 additions & 5 deletions custom_components/haier/core/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.components.switch import SwitchDeviceClass
from homeassistant.const import Platform, UnitOfTemperature, PERCENTAGE
from homeassistant.const import Platform, UnitOfTemperature, PERCENTAGE, UnitOfVolume, UnitOfEnergy, REVOLUTIONS_PER_MINUTE


class HaierAttribute:
Expand Down Expand Up @@ -93,19 +93,39 @@ def _parse_as_sensor(attribute):
if attribute['type'] == 'enum':
value_comparison_table = {}
for item in attribute['variants']:
value_comparison_table[item['stdValue']] = item['description']
value_comparison_table[str(item['stdValue'])] = item['description']

options['device_class'] = SensorDeviceClass.ENUM
options['options'] = list(value_comparison_table.values())
ext['value_comparison_table'] = value_comparison_table

if isinstance(attribute['variants'], dict) and 'unit' in attribute['variants']:
if attribute['variants']['unit'] in ['L']:
if attribute['variants']['unit'] in ['L']: # 用水量
options['device_class'] = SensorDeviceClass.WATER
options['native_unit_of_measurement'] = UnitOfVolume.LITERS

if attribute['variants']['unit'] in ['℃']:
elif attribute['variants']['unit'] in ['℃']: # 温度
options['device_class'] = SensorDeviceClass.TEMPERATURE
options['unit_of_measurement'] = UnitOfTemperature.CELSIUS
options['native_unit_of_measurement'] = UnitOfTemperature.CELSIUS

elif attribute['variants']['unit'] in ['%'] and '湿度' in attribute['description']:
options['device_class'] = SensorDeviceClass.HUMIDITY
options['native_unit_of_measurement'] = PERCENTAGE

elif attribute['variants']['unit'] in ['KWh']: # 用电量
options['device_class'] = SensorDeviceClass.ENERGY
options['native_unit_of_measurement'] = UnitOfEnergy.KILO_WATT_HOUR # kWh

elif attribute['variants']['unit'] in ['h', 'min', 's']: # 时间
options['device_class'] = SensorDeviceClass.DURATION
options['native_unit_of_measurement'] = attribute['variants']['unit']

elif attribute['variants']['unit'] in ['g', 'kg']:
options['device_class'] = SensorDeviceClass.WEIGHT
options['native_unit_of_measurement'] = attribute['variants']['unit']

elif attribute['variants']['unit'] in ['RPM']: # 转速
options['native_unit_of_measurement'] = REVOLUTIONS_PER_MINUTE

return HaierAttribute(attribute['name'], attribute['description'], Platform.SENSOR, options, ext)

Expand Down