Use new unit enums in zha ()

* Use new unit enums in zha

* Add kPa

* Add kPa
This commit is contained in:
epenet 2022-12-09 13:33:08 +01:00 committed by GitHub
parent 01ee066163
commit e3aca53899
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,27 +18,22 @@ from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
ELECTRIC_CURRENT_AMPERE,
ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR,
FREQUENCY_HERTZ,
LIGHT_LUX,
PERCENTAGE,
POWER_WATT,
PRESSURE_HPA,
TEMP_CELSIUS,
TIME_HOURS,
TIME_MINUTES,
TIME_SECONDS,
VOLUME_CUBIC_FEET,
VOLUME_CUBIC_METERS,
VOLUME_FLOW_RATE_CUBIC_FEET_PER_MINUTE,
VOLUME_FLOW_RATE_CUBIC_METERS_PER_HOUR,
VOLUME_GALLONS,
VOLUME_LITERS,
Platform,
UnitOfApparentPower,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfFrequency,
UnitOfMass,
UnitOfPower,
UnitOfPressure,
UnitOfTemperature,
UnitOfTime,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -129,7 +124,6 @@ class Sensor(ZhaEntity, SensorEntity):
_decimals: int = 1
_divisor: int = 1
_multiplier: int | float = 1
_unit: str | None = None
def __init__(
self,
@ -167,11 +161,6 @@ class Sensor(ZhaEntity, SensorEntity):
self._channel, SIGNAL_ATTR_UPDATED, self.async_set_state
)
@property
def native_unit_of_measurement(self) -> str | None:
"""Return the unit of measurement of this entity."""
return self._unit
@property
def native_value(self) -> StateType:
"""Return the state of the entity."""
@ -221,7 +210,7 @@ class Battery(Sensor):
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_name: str = "Battery"
_unit = PERCENTAGE
_attr_native_unit_of_measurement = PERCENTAGE
@classmethod
def create_entity(
@ -273,7 +262,7 @@ class ElectricalMeasurement(Sensor):
_attr_should_poll = True # BaseZhaEntity defaults to False
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_attr_name: str = "Active power"
_unit = POWER_WATT
_attr_native_unit_of_measurement: str = UnitOfPower.WATT
_div_mul_prefix = "ac_power"
@property
@ -315,7 +304,7 @@ class ElectricalMeasurementApparentPower(
_attr_device_class: SensorDeviceClass = SensorDeviceClass.APPARENT_POWER
_attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor
_attr_name: str = "Apparent power"
_unit = UnitOfApparentPower.VOLT_AMPERE
_attr_native_unit_of_measurement = UnitOfApparentPower.VOLT_AMPERE
_div_mul_prefix = "ac_power"
@ -327,7 +316,7 @@ class ElectricalMeasurementRMSCurrent(ElectricalMeasurement, id_suffix="rms_curr
_attr_device_class: SensorDeviceClass = SensorDeviceClass.CURRENT
_attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor
_attr_name: str = "RMS current"
_unit = ELECTRIC_CURRENT_AMPERE
_attr_native_unit_of_measurement = UnitOfElectricCurrent.AMPERE
_div_mul_prefix = "ac_current"
@ -339,7 +328,7 @@ class ElectricalMeasurementRMSVoltage(ElectricalMeasurement, id_suffix="rms_volt
_attr_device_class: SensorDeviceClass = SensorDeviceClass.CURRENT
_attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor
_attr_name: str = "RMS voltage"
_unit = ELECTRIC_POTENTIAL_VOLT
_attr_native_unit_of_measurement = UnitOfElectricPotential.VOLT
_div_mul_prefix = "ac_voltage"
@ -351,7 +340,7 @@ class ElectricalMeasurementFrequency(ElectricalMeasurement, id_suffix="ac_freque
_attr_device_class: SensorDeviceClass = SensorDeviceClass.FREQUENCY
_attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor
_attr_name: str = "AC frequency"
_unit = FREQUENCY_HERTZ
_attr_native_unit_of_measurement = UnitOfFrequency.HERTZ
_div_mul_prefix = "ac_frequency"
@ -363,7 +352,7 @@ class ElectricalMeasurementPowerFactor(ElectricalMeasurement, id_suffix="power_f
_attr_device_class: SensorDeviceClass = SensorDeviceClass.POWER_FACTOR
_attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor
_attr_name: str = "Power factor"
_unit = PERCENTAGE
_attr_native_unit_of_measurement = PERCENTAGE
@MULTI_MATCH(
@ -378,7 +367,7 @@ class Humidity(Sensor):
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_attr_name: str = "Humidity"
_divisor = 100
_unit = PERCENTAGE
_attr_native_unit_of_measurement = PERCENTAGE
@MULTI_MATCH(channel_names=CHANNEL_SOIL_MOISTURE)
@ -390,7 +379,7 @@ class SoilMoisture(Sensor):
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_attr_name: str = "Soil moisture"
_divisor = 100
_unit = PERCENTAGE
_attr_native_unit_of_measurement = PERCENTAGE
@MULTI_MATCH(channel_names=CHANNEL_LEAF_WETNESS)
@ -402,7 +391,7 @@ class LeafWetness(Sensor):
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_attr_name: str = "Leaf wetness"
_divisor = 100
_unit = PERCENTAGE
_attr_native_unit_of_measurement = PERCENTAGE
@MULTI_MATCH(channel_names=CHANNEL_ILLUMINANCE)
@ -413,7 +402,7 @@ class Illuminance(Sensor):
_attr_device_class: SensorDeviceClass = SensorDeviceClass.ILLUMINANCE
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_attr_name: str = "Illuminance"
_unit = LIGHT_LUX
_attr_native_unit_of_measurement = LIGHT_LUX
def formatter(self, value: int) -> float:
"""Convert illumination data."""
@ -433,19 +422,19 @@ class SmartEnergyMetering(Sensor):
_attr_name: str = "Instantaneous demand"
unit_of_measure_map = {
0x00: POWER_WATT,
0x00: UnitOfPower.WATT,
0x01: VOLUME_FLOW_RATE_CUBIC_METERS_PER_HOUR,
0x02: VOLUME_FLOW_RATE_CUBIC_FEET_PER_MINUTE,
0x03: f"100 {VOLUME_FLOW_RATE_CUBIC_METERS_PER_HOUR}",
0x04: f"US {VOLUME_GALLONS}/{TIME_HOURS}",
0x05: f"IMP {VOLUME_GALLONS}/{TIME_HOURS}",
0x06: f"BTU/{TIME_HOURS}",
0x07: f"l/{TIME_HOURS}",
0x08: "kPa", # gauge
0x09: "kPa", # absolute
0x0A: f"1000 {VOLUME_GALLONS}/{TIME_HOURS}",
0x04: f"US {UnitOfVolume.GALLONS}/{UnitOfTime.HOURS}",
0x05: f"IMP {UnitOfVolume.GALLONS}/{UnitOfTime.HOURS}",
0x06: f"BTU/{UnitOfTime.HOURS}",
0x07: f"l/{UnitOfTime.HOURS}",
0x08: UnitOfPressure.KPA, # gauge
0x09: UnitOfPressure.KPA, # absolute
0x0A: f"1000 {UnitOfVolume.GALLONS}/{UnitOfTime.HOURS}",
0x0B: "unitless",
0x0C: f"MJ/{TIME_SECONDS}",
0x0C: f"MJ/{UnitOfTime.SECONDS}",
}
def formatter(self, value: int) -> int | float:
@ -481,17 +470,17 @@ class SmartEnergySummation(SmartEnergyMetering, id_suffix="summation_delivered")
_attr_name: str = "Summation delivered"
unit_of_measure_map = {
0x00: ENERGY_KILO_WATT_HOUR,
0x01: VOLUME_CUBIC_METERS,
0x02: VOLUME_CUBIC_FEET,
0x03: f"100 {VOLUME_CUBIC_FEET}",
0x04: f"US {VOLUME_GALLONS}",
0x05: f"IMP {VOLUME_GALLONS}",
0x00: UnitOfEnergy.KILO_WATT_HOUR,
0x01: UnitOfVolume.CUBIC_METERS,
0x02: UnitOfVolume.CUBIC_FEET,
0x03: f"100 {UnitOfVolume.CUBIC_FEET}",
0x04: f"US {UnitOfVolume.GALLONS}",
0x05: f"IMP {UnitOfVolume.GALLONS}",
0x06: "BTU",
0x07: VOLUME_LITERS,
0x08: "kPa", # gauge
0x09: "kPa", # absolute
0x0A: f"1000 {VOLUME_CUBIC_FEET}",
0x07: UnitOfVolume.LITERS,
0x08: UnitOfPressure.KPA, # gauge
0x09: UnitOfPressure.KPA, # absolute
0x0A: f"1000 {UnitOfVolume.CUBIC_FEET}",
0x0B: "unitless",
0x0C: "MJ",
}
@ -531,7 +520,7 @@ class Pressure(Sensor):
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_attr_name: str = "Pressure"
_decimals = 0
_unit = PRESSURE_HPA
_attr_native_unit_of_measurement = UnitOfPressure.HPA
@MULTI_MATCH(channel_names=CHANNEL_TEMPERATURE)
@ -543,7 +532,7 @@ class Temperature(Sensor):
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_attr_name: str = "Temperature"
_divisor = 100
_unit = TEMP_CELSIUS
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
@MULTI_MATCH(channel_names=CHANNEL_DEVICE_TEMPERATURE)
@ -555,7 +544,7 @@ class DeviceTemperature(Sensor):
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_attr_name: str = "Device temperature"
_divisor = 100
_unit = TEMP_CELSIUS
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
_attr_entity_category = EntityCategory.DIAGNOSTIC
@ -569,7 +558,7 @@ class CarbonDioxideConcentration(Sensor):
_attr_name: str = "Carbon dioxide concentration"
_decimals = 0
_multiplier = 1e6
_unit = CONCENTRATION_PARTS_PER_MILLION
_attr_native_unit_of_measurement = CONCENTRATION_PARTS_PER_MILLION
@MULTI_MATCH(channel_names="carbon_monoxide_concentration")
@ -582,7 +571,7 @@ class CarbonMonoxideConcentration(Sensor):
_attr_name: str = "Carbon monoxide concentration"
_decimals = 0
_multiplier = 1e6
_unit = CONCENTRATION_PARTS_PER_MILLION
_attr_native_unit_of_measurement = CONCENTRATION_PARTS_PER_MILLION
@MULTI_MATCH(generic_ids="channel_0x042e", stop_on_match_group="voc_level")
@ -596,7 +585,7 @@ class VOCLevel(Sensor):
_attr_name: str = "VOC level"
_decimals = 0
_multiplier = 1e6
_unit = CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
_attr_native_unit_of_measurement = CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
@MULTI_MATCH(
@ -613,7 +602,7 @@ class PPBVOCLevel(Sensor):
_attr_name: str = "VOC level"
_decimals = 0
_multiplier = 1
_unit = CONCENTRATION_PARTS_PER_BILLION
_attr_native_unit_of_measurement = CONCENTRATION_PARTS_PER_BILLION
@MULTI_MATCH(channel_names="pm25")
@ -625,7 +614,7 @@ class PM25(Sensor):
_attr_name: str = "Particulate matter"
_decimals = 0
_multiplier = 1
_unit = CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
_attr_native_unit_of_measurement = CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
@MULTI_MATCH(channel_names="formaldehyde_concentration")
@ -637,7 +626,7 @@ class FormaldehydeConcentration(Sensor):
_attr_name: str = "Formaldehyde concentration"
_decimals = 0
_multiplier = 1e6
_unit = CONCENTRATION_PARTS_PER_MILLION
_attr_native_unit_of_measurement = CONCENTRATION_PARTS_PER_MILLION
@MULTI_MATCH(channel_names=CHANNEL_THERMOSTAT, stop_on_match_group=CHANNEL_THERMOSTAT)
@ -817,7 +806,7 @@ class TimeLeft(Sensor, id_suffix="time_left"):
_attr_device_class: SensorDeviceClass = SensorDeviceClass.DURATION
_attr_icon = "mdi:timer"
_attr_name: str = "Time left"
_unit = TIME_MINUTES
_attr_native_unit_of_measurement = UnitOfTime.MINUTES
@MULTI_MATCH(channel_names="ikea_airpurifier")
@ -828,7 +817,7 @@ class IkeaDeviceRunTime(Sensor, id_suffix="device_run_time"):
_attr_device_class: SensorDeviceClass = SensorDeviceClass.DURATION
_attr_icon = "mdi:timer"
_attr_name: str = "Device run time"
_unit = TIME_MINUTES
_attr_native_unit_of_measurement = UnitOfTime.MINUTES
@MULTI_MATCH(channel_names="ikea_airpurifier")
@ -839,7 +828,7 @@ class IkeaFilterRunTime(Sensor, id_suffix="filter_run_time"):
_attr_device_class: SensorDeviceClass = SensorDeviceClass.DURATION
_attr_icon = "mdi:timer"
_attr_name: str = "Filter run time"
_unit = TIME_MINUTES
_attr_native_unit_of_measurement = UnitOfTime.MINUTES
class AqaraFeedingSource(types.enum8):
@ -887,6 +876,6 @@ class AqaraPetFeederWeightDispensed(Sensor, id_suffix="weight_dispensed"):
SENSOR_ATTR = "weight_dispensed"
_attr_name: str = "Weight dispensed today"
_unit = UnitOfMass.GRAMS
_attr_native_unit_of_measurement = UnitOfMass.GRAMS
_attr_state_class: SensorStateClass = SensorStateClass.TOTAL_INCREASING
_attr_icon: str = "mdi:weight-gram"