From b0428dc95a7403381db14630bf3ce348582515a3 Mon Sep 17 00:00:00 2001 From: Necroneco Date: Thu, 27 Mar 2025 15:45:46 +0800 Subject: [PATCH] feat: make submersion-state, contact-state, occupancy-status as binary_sensor (#905) --- custom_components/xiaomi_home/binary_sensor.py | 4 ++++ .../xiaomi_home/miot/specs/specv2entity.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/custom_components/xiaomi_home/binary_sensor.py b/custom_components/xiaomi_home/binary_sensor.py index aca45d8..8019104 100644 --- a/custom_components/xiaomi_home/binary_sensor.py +++ b/custom_components/xiaomi_home/binary_sensor.py @@ -89,4 +89,8 @@ class BinarySensor(MIoTPropertyEntity, BinarySensorEntity): @property def is_on(self) -> bool: """On/Off state. True if the binary sensor is on, False otherwise.""" + if self.spec.name == 'contact-state': + return self._value is False + elif self.spec.name == 'occupancy-status': + return bool(self._value) return self._value is True diff --git a/custom_components/xiaomi_home/miot/specs/specv2entity.py b/custom_components/xiaomi_home/miot/specs/specv2entity.py index 7adbcc6..a14d098 100644 --- a/custom_components/xiaomi_home/miot/specs/specv2entity.py +++ b/custom_components/xiaomi_home/miot/specs/specv2entity.py @@ -48,6 +48,7 @@ Conversion rules of MIoT-Spec-V2 instance to Home Assistant entity. from homeassistant.components.sensor import SensorDeviceClass from homeassistant.components.sensor import SensorStateClass from homeassistant.components.event import EventDeviceClass +from homeassistant.components.binary_sensor import BinarySensorDeviceClass from homeassistant.const import ( CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, @@ -454,12 +455,28 @@ SPEC_PROP_TRANS_MAP: dict = { 'format': {'int', 'float'}, 'access': {'read'} }, + 'binary_sensor': { + 'format': {'bool', 'int'}, + 'access': {'read'} + }, 'switch': { 'format': {'bool'}, 'access': {'read', 'write'} } }, 'properties': { + 'submersion-state': { + 'device_class': BinarySensorDeviceClass.MOISTURE, + 'entity': 'binary_sensor' + }, + 'contact-state': { + 'device_class': BinarySensorDeviceClass.DOOR, + 'entity': 'binary_sensor' + }, + 'occupancy-status': { + 'device_class': BinarySensorDeviceClass.OCCUPANCY, + 'entity': 'binary_sensor', + }, 'temperature': { 'device_class': SensorDeviceClass.TEMPERATURE, 'entity': 'sensor',