From 62de02874c067b0e7bab890c4926b3cc9d580ee1 Mon Sep 17 00:00:00 2001 From: James Pinckney Date: Tue, 19 Jul 2022 23:08:51 -0400 Subject: [PATCH] Migrate device classes to StrEnum --- custom_components/ecowitt/const.py | 224 ++++++++++++++-------------- custom_components/ecowitt/sensor.py | 17 ++- 2 files changed, 118 insertions(+), 123 deletions(-) diff --git a/custom_components/ecowitt/const.py b/custom_components/ecowitt/const.py index e57fc40..4a11542 100644 --- a/custom_components/ecowitt/const.py +++ b/custom_components/ecowitt/const.py @@ -23,23 +23,17 @@ TIME_MONTHS, TIME_YEARS, UV_INDEX, - DEVICE_CLASS_BATTERY, - DEVICE_CLASS_HUMIDITY, - DEVICE_CLASS_ILLUMINANCE, - DEVICE_CLASS_TEMPERATURE, - DEVICE_CLASS_PRESSURE, - DEVICE_CLASS_TIMESTAMP, - DEVICE_CLASS_VOLTAGE, ELECTRIC_POTENTIAL_VOLT, ) from homeassistant.components.binary_sensor import ( - DEVICE_CLASS_MOISTURE, + BinarySensorDeviceClass, ) from homeassistant.components.sensor import ( STATE_CLASS_MEASUREMENT, STATE_CLASS_TOTAL_INCREASING, + SensorDeviceClass, ) ECOWITT_PLATFORMS = ["sensor", "binary_sensor"] @@ -251,16 +245,16 @@ # name, uom, kind, device_class, icon, metric, state_class = SENSOR_TYPES[x] SENSOR_TYPES = { TYPE_BAROMABSHPA: ("Absolute Pressure", PRESSURE_HPA, - TYPE_SENSOR, DEVICE_CLASS_PRESSURE, + TYPE_SENSOR, SensorDeviceClass.PRESSURE, "mdi:gauge", S_METRIC, STATE_CLASS_MEASUREMENT), TYPE_BAROMRELHPA: ("Relative Pressure", PRESSURE_HPA, - TYPE_SENSOR, DEVICE_CLASS_PRESSURE, + TYPE_SENSOR, SensorDeviceClass.PRESSURE, "mdi:gauge", S_METRIC, STATE_CLASS_MEASUREMENT), TYPE_BAROMABSIN: ("Absolute Pressure", PRESSURE_INHG, - TYPE_SENSOR, DEVICE_CLASS_PRESSURE, + TYPE_SENSOR, SensorDeviceClass.PRESSURE, "mdi:gauge", S_IMPERIAL, STATE_CLASS_MEASUREMENT), TYPE_BAROMRELIN: ("Relative Pressure", PRESSURE_INHG, - TYPE_SENSOR, DEVICE_CLASS_PRESSURE, + TYPE_SENSOR, SensorDeviceClass.PRESSURE, "mdi:gauge", S_IMPERIAL, STATE_CLASS_MEASUREMENT), TYPE_RAINRATEIN: ("Rain Rate", f"{LENGTH_INCHES}/{TIME_HOURS}", TYPE_SENSOR, None, "mdi:water", S_IMPERIAL, @@ -311,34 +305,34 @@ TYPE_SENSOR, None, "mdi:water", S_METRIC, STATE_CLASS_TOTAL_INCREASING), TYPE_HUMIDITY: ("Humidity", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_HUMIDITYIN: ("Indoor Humidity", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_HUMIDITY1: ("Humidity 1", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_HUMIDITY2: ("Humidity 2", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_HUMIDITY3: ("Humidity 3", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_HUMIDITY4: ("Humidity 4", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_HUMIDITY5: ("Humidity 5", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_HUMIDITY6: ("Humidity 6", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_HUMIDITY7: ("Humidity 7", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_HUMIDITY8: ("Humidity 8", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_WINDDIR: ("Wind Direction", DEGREE, TYPE_SENSOR, None, "mdi:compass", 0, STATE_CLASS_MEASUREMENT), @@ -382,96 +376,96 @@ TYPE_SENSOR, None, "mdi:weather-windy", S_METRIC_MS, STATE_CLASS_MEASUREMENT), TYPE_TEMPC: ("Outdoor Temperature", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, "mdi:thermometer", 0, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_TEMP1C: ("Temperature 1", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, "mdi:thermometer", 0, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_TEMP2C: ("Temperature 2", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, "mdi:thermometer", 0, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_TEMP3C: ("Temperature 3", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, "mdi:thermometer", 0, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_TEMP4C: ("Temperature 4", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, "mdi:thermometer", 0, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_TEMP5C: ("Temperature 5", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, "mdi:thermometer", 0, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_TEMP6C: ("Temperature 6", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, "mdi:thermometer", 0, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_TEMP7C: ("Temperature 7", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, "mdi:thermometer", 0, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_TEMP8C: ("Temperature 8", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, "mdi:thermometer", 0, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_TEMPINC: ("Indoor Temperature", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_DEWPOINTC: ("Dewpoint", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_DEWPOINTINC: ("Indoor Dewpoint", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_DEWPOINT1C: ("Dewpoint 1", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_DEWPOINT2C: ("Dewpoint 2", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_DEWPOINT3C: ("Dewpoint 3", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_DEWPOINT4C: ("Dewpoint 4", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_DEWPOINT5C: ("Dewpoint 5", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_DEWPOINT6C: ("Dewpoint 6", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_DEWPOINT7C: ("Dewpoint 7", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_DEWPOINT8C: ("Dewpoint 8", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_WINDCHILLC: ("Windchill", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_SOLARRADIATION: ("Solar Radiation", f"{POWER_WATT}/m^2", - TYPE_SENSOR, DEVICE_CLASS_ILLUMINANCE, + TYPE_SENSOR, SensorDeviceClass.ILLUMINANCE, "mdi:weather-sunny", 0, STATE_CLASS_MEASUREMENT), TYPE_UV: ("UV Index", UV_INDEX, TYPE_SENSOR, None, "mdi:sunglasses", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILMOISTURE1: ("Soil Moisture 1", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILMOISTURE2: ("Soil Moisture 2", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILMOISTURE3: ("Soil Moisture 3", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILMOISTURE4: ("Soil Moisture 4", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILMOISTURE5: ("Soil Moisture 5", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILMOISTURE6: ("Soil Moisture 6", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILMOISTURE7: ("Soil Moisture 7", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILMOISTURE8: ("Soil Moisture 8", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_PM25_CH1: ("PM2.5 1", CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, TYPE_SENSOR, None, "mdi:eye", 0, STATE_CLASS_MEASUREMENT), @@ -498,7 +492,7 @@ TYPE_SENSOR, None, "mdi:eye", 0, STATE_CLASS_MEASUREMENT), TYPE_LIGHTNING_TIME: ("Last Lightning strike", "", - TYPE_SENSOR, DEVICE_CLASS_TIMESTAMP, "mdi:clock", 0, + TYPE_SENSOR, SensorDeviceClass.TIMESTAMP, "mdi:clock", 0, STATE_CLASS_MEASUREMENT), TYPE_LIGHTNING_NUM: ("Lightning strikes", f"strikes/{TIME_DAYS}", TYPE_SENSOR, None, "mdi:weather-lightning", 0, @@ -510,13 +504,13 @@ TYPE_SENSOR, None, "mdi:ruler", S_IMPERIAL, STATE_CLASS_MEASUREMENT), TYPE_LEAK_CH1: ("Leak Detection 1", LEAK_DETECTED, TYPE_BINARY_SENSOR, - DEVICE_CLASS_MOISTURE, "mdi:leak", 0, None), + BinarySensorDeviceClass.MOISTURE, "mdi:leak", 0, None), TYPE_LEAK_CH2: ("Leak Detection 2", LEAK_DETECTED, TYPE_BINARY_SENSOR, - DEVICE_CLASS_MOISTURE, "mdi:leak", 0, None), + BinarySensorDeviceClass.MOISTURE, "mdi:leak", 0, None), TYPE_LEAK_CH3: ("Leak Detection 3", LEAK_DETECTED, TYPE_BINARY_SENSOR, - DEVICE_CLASS_MOISTURE, "mdi:leak", 0, None), + BinarySensorDeviceClass.MOISTURE, "mdi:leak", 0, None), TYPE_LEAK_CH4: ("Leak Detection 4", LEAK_DETECTED, TYPE_BINARY_SENSOR, - DEVICE_CLASS_MOISTURE, "mdi:leak", 0, None), + BinarySensorDeviceClass.MOISTURE, "mdi:leak", 0, None), TYPE_CO2_PM25: ("WH45 PM2.5", CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, TYPE_SENSOR, None, "mdi:eye", 0, STATE_CLASS_MEASUREMENT), TYPE_CO2_PM25_AVG_24H: ("WH45 PM2.5 24h average", @@ -530,10 +524,10 @@ TYPE_SENSOR, None, "mdi:eye", 0, STATE_CLASS_MEASUREMENT), TYPE_CO2_HUMIDITY: ("WH45 Humidity", PERCENTAGE, - TYPE_SENSOR, DEVICE_CLASS_HUMIDITY, + TYPE_SENSOR, SensorDeviceClass.HUMIDITY, "mdi:water-percent", 0, STATE_CLASS_MEASUREMENT), TYPE_CO2_TEMPC: ("WH45 Temperature", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_CO2_CO2: ("WH45 CO2", CONCENTRATION_PARTS_PER_MILLION, TYPE_SENSOR, None, "mdi:molecule-co2", 0, @@ -542,161 +536,161 @@ TYPE_SENSOR, None, "mdi:molecule-co2", 0, STATE_CLASS_MEASUREMENT), TYPE_CO2_BATT: ("WH45 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_WH25BATT: ("WH25 Battery", "BATT", TYPE_BINARY_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, None), + SensorDeviceClass.BATTERY, "mdi:battery", 0, None), TYPE_WH26BATT: ("WH26 Battery", "BATT", TYPE_BINARY_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, None), + SensorDeviceClass.BATTERY, "mdi:battery", 0, None), TYPE_WH40BATT: ("WH40 Battery", ELECTRIC_POTENTIAL_VOLT, TYPE_SENSOR, - DEVICE_CLASS_VOLTAGE, "mdi:battery", 0, + SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_WH57BATT: ("WH57 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_WH65BATT: ("WH65 Battery", "BATT", TYPE_BINARY_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, None), + SensorDeviceClass.BATTERY, "mdi:battery", 0, None), TYPE_WH68BATT: ("WH68 Battery", ELECTRIC_POTENTIAL_VOLT, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_WH80BATT: ("WH80 Battery", ELECTRIC_POTENTIAL_VOLT, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILBATT1: ("Soil Moisture 1 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, "mdi:battery", 0, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILBATT2: ("Soil Moisture 2 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, "mdi:battery", 0, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILBATT3: ("Soil Moisture 3 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, "mdi:battery", 0, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILBATT4: ("Soil Moisture 4 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, "mdi:battery", 0, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILBATT5: ("Soil Moisture 5 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, "mdi:battery", 0, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILBATT6: ("Soil Moisture 6 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, "mdi:battery", 0, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILBATT7: ("Soil Moisture 7 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, "mdi:battery", 0, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_SOILBATT8: ("Soil Moisture 8 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, "mdi:battery", 0, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_BATTERY1: ("Battery 1", "BATT", TYPE_BINARY_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, None), + SensorDeviceClass.BATTERY, "mdi:battery", 0, None), TYPE_BATTERY2: ("Battery 2", "BATT", TYPE_BINARY_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, None), + SensorDeviceClass.BATTERY, "mdi:battery", 0, None), TYPE_BATTERY3: ("Battery 3", "BATT", TYPE_BINARY_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, None), + SensorDeviceClass.BATTERY, "mdi:battery", 0, None), TYPE_BATTERY4: ("Battery 4", "BATT", TYPE_BINARY_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, None), + SensorDeviceClass.BATTERY, "mdi:battery", 0, None), TYPE_BATTERY5: ("Battery 5", "BATT", TYPE_BINARY_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, None), + SensorDeviceClass.BATTERY, "mdi:battery", 0, None), TYPE_BATTERY6: ("Battery 6", "BATT", TYPE_BINARY_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, None), + SensorDeviceClass.BATTERY, "mdi:battery", 0, None), TYPE_BATTERY7: ("Battery 7", "BATT", TYPE_BINARY_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, None), + SensorDeviceClass.BATTERY, "mdi:battery", 0, None), TYPE_BATTERY8: ("Battery 8", "BATT", TYPE_BINARY_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, None), + SensorDeviceClass.BATTERY, "mdi:battery", 0, None), TYPE_PM25BATT1: ("PM2.5 1 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_PM25BATT2: ("PM2.5 2 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_PM25BATT3: ("PM2.5 3 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_PM25BATT4: ("PM2.5 4 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_PM25BATT5: ("PM2.5 5 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_PM25BATT6: ("PM2.5 6 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_PM25BATT7: ("PM2.5 7 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_PM25BATT8: ("PM2.5 8 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_LEAKBATT1: ("Leak 1 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_LEAKBATT2: ("Leak 2 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_LEAKBATT3: ("Leak 3 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_LEAKBATT4: ("Leak 4 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_LEAKBATT5: ("Leak 5 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_LEAKBATT6: ("Leak 6 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_LEAKBATT7: ("Leak 7 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_LEAKBATT8: ("Leak 8 Battery", PERCENTAGE, TYPE_SENSOR, - DEVICE_CLASS_BATTERY, "mdi:battery", 0, + SensorDeviceClass.BATTERY, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34TEMP1C: ("Soil Temperature 1", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34TEMP2C: ("Soil Temperature 2", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34TEMP3C: ("Soil Temperature 3", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34TEMP4C: ("Soil Temperature 4", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34TEMP5C: ("Soil Temperature 5", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34TEMP6C: ("Soil Temperature 6", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34TEMP7C: ("Soil Temperature 7", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34TEMP8C: ("Soil Temperature 8", TEMP_CELSIUS, - TYPE_SENSOR, DEVICE_CLASS_TEMPERATURE, + TYPE_SENSOR, SensorDeviceClass.TEMPERATURE, "mdi:thermometer", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34BATT1: ("Soil Temperature 1 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34BATT2: ("Soil Temperature 2 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34BATT3: ("Soil Temperature 3 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34BATT4: ("Soil Temperature 4 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34BATT5: ("Soil Temperature 5 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34BATT6: ("Soil Temperature 6 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34BATT7: ("Soil Temperature 7 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), TYPE_WN34BATT8: ("Soil Temperature 8 Battery", ELECTRIC_POTENTIAL_VOLT, - TYPE_SENSOR, DEVICE_CLASS_VOLTAGE, + TYPE_SENSOR, SensorDeviceClass.VOLTAGE, "mdi:battery", 0, STATE_CLASS_MEASUREMENT), } diff --git a/custom_components/ecowitt/sensor.py b/custom_components/ecowitt/sensor.py index 492c92e..44f1cca 100644 --- a/custom_components/ecowitt/sensor.py +++ b/custom_components/ecowitt/sensor.py @@ -2,9 +2,10 @@ import logging import homeassistant.util.dt as dt_util +from datetime import datetime from . import EcowittEntity, async_add_ecowitt_entities from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN -from homeassistant.components.sensor import SensorEntity +from homeassistant.components.sensor import SensorEntity, SensorDeviceClass from homeassistant.helpers.dispatcher import async_dispatcher_connect from .const import ( DOMAIN, @@ -15,8 +16,6 @@ from homeassistant.const import ( STATE_UNKNOWN, - DEVICE_CLASS_TIMESTAMP, - DEVICE_CLASS_BATTERY, PERCENTAGE, ) @@ -51,14 +50,16 @@ def native_value(self): """Return the state of the sensor.""" if self._key in self._ws.last_values: # The lightning time is reported in UTC, hooray. - if self._dc == DEVICE_CLASS_TIMESTAMP: + if self._dc == SensorDeviceClass.TIMESTAMP: if not isinstance(self._ws.last_values[self._key], int): return STATE_UNKNOWN - return dt_util.as_local( - dt_util.utc_from_timestamp(self._ws.last_values[self._key]) - ).isoformat() + return datetime.strptime( + dt_util.as_local( + dt_util.utc_from_timestamp(self._ws.last_values[self._key]) + ).isoformat(), "%Y-%m-%dT%H:%M:%S%z" + ) # Battery value is 0-5 - if self._dc == DEVICE_CLASS_BATTERY and self._uom == PERCENTAGE: + if self._dc == SensorDeviceClass.BATTERY and self._uom == PERCENTAGE: return self._ws.last_values[self._key] * 20.0 return self._ws.last_values[self._key] _LOGGER.warning("Sensor %s not in last update, check range or battery",