Skip to content

Commit

Permalink
fix: handle UnitOfConductivity import #54
Browse files Browse the repository at this point in the history
Move unit imports inside function 和 add fallback for older versions. Resolves
  • Loading branch information
knoop7 authored Dec 17, 2024
1 parent 1c37002 commit 679f277
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions custom_components/xiaomi_home/miot/miot_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,24 @@
from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_MILLION,
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
LIGHT_LUX,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS,
UnitOfEnergy,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfInformation,
UnitOfLength,
UnitOfMass,
UnitOfSpeed,
UnitOfTime,
UnitOfTemperature,
UnitOfPressure,
UnitOfPower,
UnitOfVolume,
UnitOfVolumeFlowRate,
)
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.components.switch import SwitchDeviceClass
Expand Down Expand Up @@ -572,22 +585,6 @@ def spec_transform(self) -> None:

def unit_convert(self, spec_unit: str) -> Optional[str]:
"""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,
Expand Down Expand Up @@ -625,6 +622,7 @@ def unit_convert(self, spec_unit: str) -> Optional[str]:
'kB': UnitOfInformation.KILOBYTES,
}

# Handle UnitOfConductivity separately since it might not be available in all HA versions
try:
from homeassistant.const import UnitOfConductivity
unit_map['μS/cm'] = UnitOfConductivity.MICROSIEMENS_PER_CM
Expand Down

0 comments on commit 679f277

Please sign in to comment.