1 """Support for Xiaomi Aqara sensors."""
3 from __future__
import annotations
10 SensorEntityDescription,
25 from .const
import BATTERY_MODELS, DOMAIN, GATEWAYS_KEY, POWER_MODELS
26 from .entity
import XiaomiDevice
28 _LOGGER = logging.getLogger(__name__)
30 SENSOR_TYPES: dict[str, SensorEntityDescription] = {
33 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
34 device_class=SensorDeviceClass.TEMPERATURE,
35 state_class=SensorStateClass.MEASUREMENT,
39 native_unit_of_measurement=PERCENTAGE,
40 device_class=SensorDeviceClass.HUMIDITY,
41 state_class=SensorStateClass.MEASUREMENT,
45 native_unit_of_measurement=
"lm",
46 state_class=SensorStateClass.MEASUREMENT,
50 native_unit_of_measurement=LIGHT_LUX,
51 device_class=SensorDeviceClass.ILLUMINANCE,
52 state_class=SensorStateClass.MEASUREMENT,
56 native_unit_of_measurement=UnitOfPressure.HPA,
57 device_class=SensorDeviceClass.PRESSURE,
58 state_class=SensorStateClass.MEASUREMENT,
62 native_unit_of_measurement=
"μm",
64 state_class=SensorStateClass.MEASUREMENT,
68 native_unit_of_measurement=UnitOfPower.WATT,
69 device_class=SensorDeviceClass.POWER,
70 state_class=SensorStateClass.MEASUREMENT,
73 key=
"final_tilt_angle",
80 state_class=SensorStateClass.MEASUREMENT,
87 config_entry: ConfigEntry,
88 async_add_entities: AddEntitiesCallback,
90 """Perform the setup for Xiaomi devices."""
91 entities: list[XiaomiSensor | XiaomiBatterySensor] = []
92 gateway = hass.data[DOMAIN][GATEWAYS_KEY][config_entry.entry_id]
93 for device
in gateway.devices[
"sensor"]:
94 if device[
"model"] ==
"sensor_ht":
97 device,
"Temperature",
"temperature", gateway, config_entry
101 XiaomiSensor(device,
"Humidity",
"humidity", gateway, config_entry)
103 elif device[
"model"]
in (
"weather",
"weather.v1"):
106 device,
"Temperature",
"temperature", gateway, config_entry
110 XiaomiSensor(device,
"Humidity",
"humidity", gateway, config_entry)
113 XiaomiSensor(device,
"Pressure",
"pressure", gateway, config_entry)
115 elif device[
"model"] ==
"sensor_motion.aq2":
117 XiaomiSensor(device,
"Illumination",
"lux", gateway, config_entry)
119 elif device[
"model"]
in (
"gateway",
"gateway.v3",
"acpartner.v3"):
122 device,
"Illumination",
"illumination", gateway, config_entry
125 elif device[
"model"]
in (
"vibration",):
128 device,
"Bed Activity",
"bed_activity", gateway, config_entry
133 device,
"Tilt Angle",
"final_tilt_angle", gateway, config_entry
138 device,
"Coordination",
"coordination", gateway, config_entry
142 _LOGGER.warning(
"Unmapped Device Model")
146 for devices
in gateway.devices.values():
147 for device
in devices:
148 if device[
"sid"]
in seen_sids:
150 seen_sids.add(device[
"sid"])
151 if device[
"model"]
in BATTERY_MODELS:
155 if device[
"model"]
in POWER_MODELS:
158 device,
"Load Power",
"load_power", gateway, config_entry
165 """Representation of a XiaomiSensor."""
167 def __init__(self, device, name, data_key, xiaomi_hub, config_entry):
168 """Initialize the XiaomiSensor."""
171 super().
__init__(device, name, xiaomi_hub, config_entry)
174 """Parse data sent by gateway."""
175 if (value := data.get(self.
_data_key_data_key))
is None:
177 if self.
_data_key_data_key
in (
"coordination",
"status"):
181 if self.
_data_key_data_key
in (
"temperature",
"humidity",
"pressure"):
183 elif self.
_data_key_data_key
in (
"illumination",):
184 value =
max(value - 300, 0)
185 if self.
_data_key_data_key ==
"temperature" and (value < -50
or value > 60):
187 if self.
_data_key_data_key ==
"humidity" and (value <= 0
or value > 100):
189 if self.
_data_key_data_key ==
"pressure" and value == 0:
191 if self.
_data_key_data_key
in (
"illumination",
"lux"):
199 """Representation of a XiaomiSensor."""
201 _attr_native_unit_of_measurement = PERCENTAGE
202 _attr_device_class = SensorDeviceClass.BATTERY
205 """Parse data sent by gateway."""
210 if battery_level <= 0
or battery_level > 100:
216 """Parse battery level data sent by gateway."""
def parse_data(self, data, raw_data)
def parse_voltage(self, data)
def __init__(self, device, name, data_key, xiaomi_hub, config_entry)
def parse_data(self, data, raw_data)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)