diff --git a/custom_components/sonoff/sensor.py b/custom_components/sonoff/sensor.py index 2048e092..a343547c 100644 --- a/custom_components/sonoff/sensor.py +++ b/custom_components/sonoff/sensor.py @@ -8,8 +8,6 @@ SensorStateClass, ) from homeassistant.const import ( - CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, - CONCENTRATION_PARTS_PER_MILLION, PERCENTAGE, SIGNAL_STRENGTH_DECIBELS_MILLIWATT, UnitOfElectricCurrent, @@ -21,6 +19,17 @@ ) from homeassistant.util import dt +try: + from homeassistant.const import UnitOfDensity, UnitOfRatio +except ImportError: + from homeassistant.const import ( + CONCENTRATION_MICROGRAMS_PER_CUBIC_METER as MICROGRAMS_PER_CUBIC_METER, + CONCENTRATION_PARTS_PER_MILLION as PARTS_PER_MILLION, + ) +else: + MICROGRAMS_PER_CUBIC_METER = UnitOfDensity.MICROGRAMS_PER_CUBIC_METER + PARTS_PER_MILLION = UnitOfRatio.PARTS_PER_MILLION + from .core.const import DOMAIN from .core.entity import XEntity from .core.ewelink import SIGNAL_ADD_ENTITIES, XRegistry @@ -58,7 +67,7 @@ async def async_setup_entry(hass, config_entry, add_entities): UNITS = { "battery": PERCENTAGE, "battery_voltage": UnitOfElectricPotential.VOLT, - "co2": CONCENTRATION_PARTS_PER_MILLION, + "co2": PARTS_PER_MILLION, "cpu_temperature": UnitOfTemperature.CELSIUS, "current": UnitOfElectricCurrent.AMPERE, "current_supply": UnitOfElectricCurrent.AMPERE, @@ -66,8 +75,8 @@ async def async_setup_entry(hass, config_entry, add_entities): "outdoor_temp": UnitOfTemperature.CELSIUS, "power": UnitOfPower.WATT, "power_supply": UnitOfPower.WATT, - "pm25": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, - "pm10": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, + "pm25": MICROGRAMS_PER_CUBIC_METER, + "pm10": MICROGRAMS_PER_CUBIC_METER, "remote_temperature": UnitOfTemperature.CELSIUS, "rssi": SIGNAL_STRENGTH_DECIBELS_MILLIWATT, "temperature": UnitOfTemperature.CELSIUS, diff --git a/tests/test_entity.py b/tests/test_entity.py index 8dfcd8a4..9a4cadc1 100644 --- a/tests/test_entity.py +++ b/tests/test_entity.py @@ -12,7 +12,6 @@ from homeassistant.components.switch import SwitchEntity from homeassistant.components.script import ATTR_LAST_TRIGGERED from homeassistant.const import ( - CONCENTRATION_PARTS_PER_MILLION, MAJOR_VERSION, MINOR_VERSION, STATE_ON, @@ -54,6 +53,7 @@ from custom_components.sonoff.number import XNumber, XPulseWidth from custom_components.sonoff.select import XSelectStartup from custom_components.sonoff.sensor import ( + PARTS_PER_MILLION, XButtonKey, XButtonLocalKey, XCloudEnergyDualR3, @@ -2664,7 +2664,7 @@ def test_sawf_08p(): assert co2.name == "Device1 CO2" assert co2.state == 510 assert co2.device_class == SensorDeviceClass.CO2 - assert co2.native_unit_of_measurement == CONCENTRATION_PARTS_PER_MILLION + assert co2.native_unit_of_measurement == PARTS_PER_MILLION assert co2.state_class == SensorStateClass.MEASUREMENT