mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2025-06-21 15:20:00 +08:00
refactor: refactor miot device and spec (#592)
* fix: fix miot_device type error * fix: fix type error * feat: remove spec cache storage * feat: update std_lib and multi_lang logic * feat: update entity value-range * feat: update value-list logic * feat: update prop.format_ logic * fix: fix miot cloud log error * fix: fix fan entity * style: ignore type error * style: rename spec_filter func name * feat: move bool_trans from storage to spec * feat: move sepc_filter from storage to spec, use the YAML format file * feat: same prop supports multiple sub * feat: same event supports multiple sub * fix: fix device remove error * feat: add func slugify_did * fix: fix multi lang error * feat: update action debug logic * feat: ignore normal disconnect log * feat: support binary mode * feat: change miot spec name type define * style: ignore i18n tranlate type error * fix: fix pylint warning * fix: miot storage type error * feat: support binary display mode configure * feat: set default sensor state_class * fix: fix sensor entity type error * fix: fix __init__ type error * feat: update test case logic * fix: github actions add dependencies lib * fix: fix some type error * feat: update device list changed notify logic
This commit is contained in:
@ -76,6 +76,12 @@ async def async_setup_entry(
|
||||
for prop in miot_device.prop_list.get('sensor', []):
|
||||
new_entities.append(Sensor(miot_device=miot_device, spec=prop))
|
||||
|
||||
if miot_device.miot_client.display_binary_text:
|
||||
for prop in miot_device.prop_list.get('binary_sensor', []):
|
||||
if not prop.value_list:
|
||||
continue
|
||||
new_entities.append(Sensor(miot_device=miot_device, spec=prop))
|
||||
|
||||
if new_entities:
|
||||
async_add_entities(new_entities)
|
||||
|
||||
@ -91,7 +97,7 @@ class Sensor(MIoTPropertyEntity, SensorEntity):
|
||||
self._attr_device_class = SensorDeviceClass.ENUM
|
||||
self._attr_icon = 'mdi:message-text'
|
||||
self._attr_native_unit_of_measurement = None
|
||||
self._attr_options = list(self._value_list.values())
|
||||
self._attr_options = self._value_list.descriptions
|
||||
else:
|
||||
self._attr_device_class = spec.device_class
|
||||
if spec.external_unit:
|
||||
@ -100,29 +106,29 @@ class Sensor(MIoTPropertyEntity, SensorEntity):
|
||||
# device_class is not empty but unit is empty.
|
||||
# Set the default unit according to device_class.
|
||||
unit_sets = DEVICE_CLASS_UNITS.get(
|
||||
self._attr_device_class, None)
|
||||
self._attr_device_class, None) # type: ignore
|
||||
self._attr_native_unit_of_measurement = list(
|
||||
unit_sets)[0] if unit_sets else None
|
||||
# Set state_class
|
||||
if spec.state_class:
|
||||
self._attr_state_class = spec.state_class
|
||||
# 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:
|
||||
"""Return the current value of the sensor."""
|
||||
if self._value_range and isinstance(self._value, (int, float)):
|
||||
if (
|
||||
self._value < self._value_range['min']
|
||||
or self._value > self._value_range['max']
|
||||
self._value < self._value_range.min_
|
||||
or self._value > self._value_range.max_
|
||||
):
|
||||
_LOGGER.info(
|
||||
'%s, data exception, out of range, %s, %s',
|
||||
self.entity_id, self._value, self._value_range)
|
||||
if self._value_list:
|
||||
return self._value_list.get(self._value, None)
|
||||
return self.get_vlist_description(self._value)
|
||||
if isinstance(self._value, str):
|
||||
return self._value[:255]
|
||||
return self._value
|
||||
|
Reference in New Issue
Block a user