From 1c370023017ec3d9da9db2e91102ad9f8a1f5bdd Mon Sep 17 00:00:00 2001 From: KNOOP Date: Tue, 17 Dec 2024 16:20:02 +0800 Subject: [PATCH] fix: handle UnitOfConductivity import Move unit imports inside function and add fallback for older versions. Resolves #123 --- .../xiaomi_home/miot/miot_device.py | 51 +++++++++++-------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/custom_components/xiaomi_home/miot/miot_device.py b/custom_components/xiaomi_home/miot/miot_device.py index 56637d6..3531ebf 100644 --- a/custom_components/xiaomi_home/miot/miot_device.py +++ b/custom_components/xiaomi_home/miot/miot_device.py @@ -59,20 +59,6 @@ LIGHT_LUX, PERCENTAGE, 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.components.switch import SwitchDeviceClass @@ -505,7 +491,8 @@ def parse_miot_property_entity( prop_access.add('read') if prop.writable: 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 if prop.format_ not in SPEC_PROP_TRANS_MAP[ 'entities'][platform]['format']: @@ -584,7 +571,24 @@ def spec_transform(self) -> None: self.append_action(action=action) 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, 'weeks': UnitOfTime.WEEKS, 'days': UnitOfTime.DAYS, @@ -616,11 +620,18 @@ def unit_convert(self, spec_unit: str) -> Optional[str]: 'm': UnitOfLength.METERS, 'km': UnitOfLength.KILOMETERS, 'm3/h': UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR, - 'μS/cm': UnitOfConductivity.MICROSIEMENS_PER_CM, 'gram': UnitOfMass.GRAMS, 'dB': SIGNAL_STRENGTH_DECIBELS, '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]: if spec_unit in ['percentage']: @@ -1170,8 +1181,8 @@ async def async_added_to_hass(self) -> None: handler=self.__on_device_state_changed) # Sub value changed self.miot_device.sub_event( - handler=self.__on_event_occurred, siid=self.service.iid, - eiid=self.spec.iid) + handler=self.__on_event_occurred, + siid=self.service.iid, eiid=self.spec.iid) async def async_will_remove_from_hass(self) -> None: self.miot_device.unsub_device_state(