Skip to content

Commit

Permalink
fix: handle UnitOfConductivity import
Browse files Browse the repository at this point in the history
Move unit imports inside function and add fallback for older versions.

Resolves #123
  • Loading branch information
knoop7 authored Dec 17, 2024
1 parent 507018d commit 1c37002
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions custom_components/xiaomi_home/miot/miot_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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']:
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 1c37002

Please sign in to comment.