|
| 1 | +"""Quirk for aqara lumi.sensor_occupy.agl1.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from zigpy import types |
| 8 | +from zigpy.quirks.v2 import ( |
| 9 | + NumberDeviceClass, |
| 10 | + QuirkBuilder, |
| 11 | + SensorDeviceClass, |
| 12 | + SensorStateClass, |
| 13 | +) |
| 14 | +from zigpy.quirks.v2.homeassistant import EntityType, UnitOfLength |
| 15 | +from zigpy.zcl.clusters.general import DeviceTemperature |
| 16 | +from zigpy.zcl.clusters.measurement import OccupancySensing |
| 17 | +from zigpy.zcl.clusters.security import IasZone |
| 18 | +from zigpy.zcl.foundation import BaseAttributeDefs, ZCLAttributeDef |
| 19 | + |
| 20 | +from zhaquirks import LocalDataCluster |
| 21 | +from zhaquirks.xiaomi import XiaomiAqaraE1Cluster |
| 22 | + |
| 23 | + |
| 24 | +class AqaraMotion(types.enum8): |
| 25 | + """Aqara motion attribute values.""" |
| 26 | + |
| 27 | + Idle = 0x02 |
| 28 | + Moving = 0x03 |
| 29 | + Still = 0x04 |
| 30 | + |
| 31 | + |
| 32 | +class AqaraMotionSensitivity(types.enum8): |
| 33 | + """Aqara motion sensitivity attribute values.""" |
| 34 | + |
| 35 | + Low = 0x01 |
| 36 | + Medium = 0x02 |
| 37 | + High = 0x03 |
| 38 | + |
| 39 | + |
| 40 | +class AqaraOccupancy(types.enum8): |
| 41 | + """Aqara occupancy attribute values.""" |
| 42 | + |
| 43 | + Unoccupied = 0x00 |
| 44 | + Occupied = 0x01 |
| 45 | + |
| 46 | + |
| 47 | +class IasZoneLocal(LocalDataCluster, IasZone): |
| 48 | + """Virtual cluster for IasZone.""" |
| 49 | + |
| 50 | + _CONSTANT_ATTRIBUTES = { |
| 51 | + IasZone.AttributeDefs.zone_type.id: IasZone.ZoneType.Motion_Sensor |
| 52 | + } |
| 53 | + _VALID_ATTRIBUTES = {IasZone.AttributeDefs.zone_status.id} |
| 54 | + |
| 55 | + |
| 56 | +class OccupancySensingLocal(LocalDataCluster, OccupancySensing): |
| 57 | + """Virtual cluster for OccupancySensing.""" |
| 58 | + |
| 59 | + _VALID_ATTRIBUTES = {OccupancySensing.AttributeDefs.occupancy.id} |
| 60 | + |
| 61 | + |
| 62 | +class OppleCluster(XiaomiAqaraE1Cluster): |
| 63 | + """Aqara manufacturer cluster for the presence sensor FP1E.""" |
| 64 | + |
| 65 | + class AttributeDefs(BaseAttributeDefs): |
| 66 | + """Manufacturer specific attributes.""" |
| 67 | + |
| 68 | + # The configurable maximum detection distance in millimeters (default 600 = 6 meters). |
| 69 | + approach_distance = ZCLAttributeDef( |
| 70 | + id=0x015B, |
| 71 | + type=types.uint32_t, |
| 72 | + access="rw", |
| 73 | + is_manufacturer_specific=True, |
| 74 | + ) |
| 75 | + |
| 76 | + # Detected motion |
| 77 | + motion = ZCLAttributeDef( |
| 78 | + id=0x0160, |
| 79 | + type=AqaraMotion, |
| 80 | + access="rp", |
| 81 | + is_manufacturer_specific=True, |
| 82 | + ) |
| 83 | + |
| 84 | + # Distance to the detected motion in millimeters |
| 85 | + motion_distance = ZCLAttributeDef( |
| 86 | + id=0x015F, |
| 87 | + type=types.uint32_t, |
| 88 | + access="rp", |
| 89 | + is_manufacturer_specific=True, |
| 90 | + ) |
| 91 | + |
| 92 | + # The configurable detection sensitivity |
| 93 | + motion_sensitivity = ZCLAttributeDef( |
| 94 | + id=0x010C, |
| 95 | + type=AqaraMotionSensitivity, |
| 96 | + access="rw", |
| 97 | + is_manufacturer_specific=True, |
| 98 | + ) |
| 99 | + |
| 100 | + # Detected occupancy |
| 101 | + occupancy = ZCLAttributeDef( |
| 102 | + id=0x0142, |
| 103 | + type=AqaraOccupancy, |
| 104 | + access="rp", |
| 105 | + is_manufacturer_specific=True, |
| 106 | + ) |
| 107 | + |
| 108 | + # Trigger AI spatial learning (write 1) |
| 109 | + reset_no_presence_status = ZCLAttributeDef( |
| 110 | + id=0x0157, |
| 111 | + type=types.uint8_t, |
| 112 | + access="w", |
| 113 | + is_manufacturer_specific=True, |
| 114 | + ) |
| 115 | + |
| 116 | + # Trigger device restart (write 0) |
| 117 | + restart_device = ZCLAttributeDef( |
| 118 | + id=0x00E8, |
| 119 | + type=types.Bool, |
| 120 | + access="w", |
| 121 | + is_manufacturer_specific=True, |
| 122 | + ) |
| 123 | + |
| 124 | + def _update_attribute(self, attrid: int, value: Any) -> None: |
| 125 | + super()._update_attribute(attrid, value) |
| 126 | + if attrid == self.AttributeDefs.occupancy.id: |
| 127 | + self.endpoint.occupancy.update_attribute( |
| 128 | + OccupancySensing.AttributeDefs.occupancy.id, |
| 129 | + OccupancySensing.Occupancy.Occupied |
| 130 | + if value == AqaraOccupancy.Occupied |
| 131 | + else OccupancySensing.Occupancy.Unoccupied, |
| 132 | + ) |
| 133 | + elif attrid == self.AttributeDefs.motion.id: |
| 134 | + self.endpoint.ias_zone.update_attribute( |
| 135 | + IasZone.AttributeDefs.zone_status.id, |
| 136 | + IasZone.ZoneStatus.Alarm_1 if value == AqaraMotion.Moving else 0, |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +( |
| 141 | + QuirkBuilder("aqara", "lumi.sensor_occupy.agl1") |
| 142 | + .friendly_name(manufacturer="Aqara", model="Presence Sensor FP1E") |
| 143 | + .adds(DeviceTemperature) |
| 144 | + .adds(OccupancySensingLocal) |
| 145 | + .adds(IasZoneLocal) |
| 146 | + .replaces(OppleCluster) |
| 147 | + .number( |
| 148 | + OppleCluster.AttributeDefs.approach_distance.name, |
| 149 | + OppleCluster.cluster_id, |
| 150 | + min_value=0, |
| 151 | + max_value=6, |
| 152 | + step=0.1, |
| 153 | + unit=UnitOfLength.METERS, |
| 154 | + multiplier=0.01, |
| 155 | + device_class=NumberDeviceClass.DISTANCE, |
| 156 | + translation_key="approach_distance", |
| 157 | + fallback_name="Approach distance", |
| 158 | + ) |
| 159 | + .sensor( |
| 160 | + OppleCluster.AttributeDefs.motion_distance.name, |
| 161 | + OppleCluster.cluster_id, |
| 162 | + unit=UnitOfLength.METERS, |
| 163 | + multiplier=0.01, |
| 164 | + device_class=SensorDeviceClass.DISTANCE, |
| 165 | + state_class=SensorStateClass.MEASUREMENT, |
| 166 | + translation_key="motion_distance", |
| 167 | + fallback_name="Motion distance", |
| 168 | + ) |
| 169 | + .enum( |
| 170 | + OppleCluster.AttributeDefs.motion_sensitivity.name, |
| 171 | + AqaraMotionSensitivity, |
| 172 | + OppleCluster.cluster_id, |
| 173 | + translation_key="motion_sensitivity", |
| 174 | + fallback_name="Motion sensitivity", |
| 175 | + ) |
| 176 | + .write_attr_button( |
| 177 | + OppleCluster.AttributeDefs.reset_no_presence_status.name, |
| 178 | + 1, |
| 179 | + OppleCluster.cluster_id, |
| 180 | + translation_key="reset_no_presence_status", |
| 181 | + fallback_name="Presence status reset", |
| 182 | + ) |
| 183 | + .write_attr_button( |
| 184 | + OppleCluster.AttributeDefs.restart_device.name, |
| 185 | + 0, |
| 186 | + OppleCluster.cluster_id, |
| 187 | + entity_type=EntityType.DIAGNOSTIC, |
| 188 | + translation_key="restart_device", |
| 189 | + fallback_name="Restart device", |
| 190 | + ) |
| 191 | + .add_to_registry() |
| 192 | +) |
0 commit comments