mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2025-04-04 00:35:33 +08:00
fix: handle UnitOfConductivity import
Move unit imports inside function and add fallback for older versions. Resolves #123
This commit is contained in:
parent
83dbceac6d
commit
6bb4bf32d7
@ -59,20 +59,6 @@ from homeassistant.const import (
|
|||||||
LIGHT_LUX,
|
LIGHT_LUX,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
SIGNAL_STRENGTH_DECIBELS,
|
SIGNAL_STRENGTH_DECIBELS,
|
||||||
UnitOfEnergy,
|
|
||||||
UnitOfElectricCurrent,
|
|
||||||
UnitOfElectricPotential,
|
|
||||||
UnitOfInformation,
|
|
||||||
UnitOfLength,
|
|
||||||
UnitOfMass,
|
|
||||||
UnitOfSpeed,
|
|
||||||
UnitOfTime,
|
|
||||||
UnitOfTemperature,
|
|
||||||
UnitOfPressure,
|
|
||||||
UnitOfPower,
|
|
||||||
UnitOfVolume,
|
|
||||||
UnitOfVolumeFlowRate,
|
|
||||||
UnitOfConductivity
|
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.components.switch import SwitchDeviceClass
|
from homeassistant.components.switch import SwitchDeviceClass
|
||||||
@ -505,7 +491,8 @@ class MIoTDevice:
|
|||||||
prop_access.add('read')
|
prop_access.add('read')
|
||||||
if prop.writable:
|
if prop.writable:
|
||||||
prop_access.add('write')
|
prop_access.add('write')
|
||||||
if prop_access != (SPEC_PROP_TRANS_MAP['entities'][platform]['access']):
|
if prop_access != (SPEC_PROP_TRANS_MAP[
|
||||||
|
'entities'][platform]['access']):
|
||||||
return None
|
return None
|
||||||
if prop.format_ not in SPEC_PROP_TRANS_MAP[
|
if prop.format_ not in SPEC_PROP_TRANS_MAP[
|
||||||
'entities'][platform]['format']:
|
'entities'][platform]['format']:
|
||||||
@ -584,7 +571,24 @@ class MIoTDevice:
|
|||||||
self.append_action(action=action)
|
self.append_action(action=action)
|
||||||
|
|
||||||
def unit_convert(self, spec_unit: str) -> Optional[str]:
|
def unit_convert(self, spec_unit: str) -> Optional[str]:
|
||||||
return {
|
"""Convert MIoT unit to Home Assistant unit."""
|
||||||
|
from homeassistant.const import (
|
||||||
|
UnitOfEnergy,
|
||||||
|
UnitOfElectricCurrent,
|
||||||
|
UnitOfElectricPotential,
|
||||||
|
UnitOfInformation,
|
||||||
|
UnitOfLength,
|
||||||
|
UnitOfMass,
|
||||||
|
UnitOfSpeed,
|
||||||
|
UnitOfTime,
|
||||||
|
UnitOfTemperature,
|
||||||
|
UnitOfPressure,
|
||||||
|
UnitOfPower,
|
||||||
|
UnitOfVolume,
|
||||||
|
UnitOfVolumeFlowRate,
|
||||||
|
)
|
||||||
|
|
||||||
|
unit_map = {
|
||||||
'percentage': PERCENTAGE,
|
'percentage': PERCENTAGE,
|
||||||
'weeks': UnitOfTime.WEEKS,
|
'weeks': UnitOfTime.WEEKS,
|
||||||
'days': UnitOfTime.DAYS,
|
'days': UnitOfTime.DAYS,
|
||||||
@ -616,11 +620,18 @@ class MIoTDevice:
|
|||||||
'm': UnitOfLength.METERS,
|
'm': UnitOfLength.METERS,
|
||||||
'km': UnitOfLength.KILOMETERS,
|
'km': UnitOfLength.KILOMETERS,
|
||||||
'm3/h': UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR,
|
'm3/h': UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR,
|
||||||
'μS/cm': UnitOfConductivity.MICROSIEMENS_PER_CM,
|
|
||||||
'gram': UnitOfMass.GRAMS,
|
'gram': UnitOfMass.GRAMS,
|
||||||
'dB': SIGNAL_STRENGTH_DECIBELS,
|
'dB': SIGNAL_STRENGTH_DECIBELS,
|
||||||
'kB': UnitOfInformation.KILOBYTES,
|
'kB': UnitOfInformation.KILOBYTES,
|
||||||
}.get(spec_unit, None)
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
from homeassistant.const import UnitOfConductivity
|
||||||
|
unit_map['μS/cm'] = UnitOfConductivity.MICROSIEMENS_PER_CM
|
||||||
|
except ImportError:
|
||||||
|
unit_map['μS/cm'] = 'μS/cm'
|
||||||
|
|
||||||
|
return unit_map.get(spec_unit, None)
|
||||||
|
|
||||||
def icon_convert(self, spec_unit: str) -> Optional[str]:
|
def icon_convert(self, spec_unit: str) -> Optional[str]:
|
||||||
if spec_unit in ['percentage']:
|
if spec_unit in ['percentage']:
|
||||||
@ -1170,8 +1181,8 @@ class MIoTEventEntity(Entity):
|
|||||||
handler=self.__on_device_state_changed)
|
handler=self.__on_device_state_changed)
|
||||||
# Sub value changed
|
# Sub value changed
|
||||||
self.miot_device.sub_event(
|
self.miot_device.sub_event(
|
||||||
handler=self.__on_event_occurred, siid=self.service.iid,
|
handler=self.__on_event_occurred,
|
||||||
eiid=self.spec.iid)
|
siid=self.service.iid, eiid=self.spec.iid)
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self) -> None:
|
async def async_will_remove_from_hass(self) -> None:
|
||||||
self.miot_device.unsub_device_state(
|
self.miot_device.unsub_device_state(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user