From 71a0274b1223f3f766202c94855b925105390a9a Mon Sep 17 00:00:00 2001 From: Daniel Lashua Date: Mon, 8 Oct 2018 05:08:46 -0500 Subject: [PATCH] Add Support for Xiaomi Vibration Sensor (#16422) --- .../components/binary_sensor/xiaomi_aqara.py | 38 +++++++++++++++++++ .../components/sensor/xiaomi_aqara.py | 12 ++++++ 2 files changed, 50 insertions(+) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index 63a7c6b68a3..2b4934ff824 100644 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -68,6 +68,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None): 'dual_channel', hass, gateway)) elif model in ['cube', 'sensor_cube', 'sensor_cube.aqgl01']: devices.append(XiaomiCube(device, hass, gateway)) + elif model in ['vibration', 'vibration.aq1']: + devices.append(XiaomiVibration(device, 'Vibration', + 'status', gateway)) + else: + _LOGGER.warning('Unmapped Device Model %s', model) + add_entities(devices) @@ -314,6 +320,38 @@ class XiaomiSmokeSensor(XiaomiBinarySensor): return False +class XiaomiVibration(XiaomiBinarySensor): + """Representation of a Xiaomi Vibration Sensor.""" + + def __init__(self, device, name, data_key, xiaomi_hub): + """Initialize the XiaomiVibration.""" + self._last_action = None + super().__init__(device, name, xiaomi_hub, data_key, None) + + @property + def device_state_attributes(self): + """Return the state attributes.""" + attrs = {ATTR_LAST_ACTION: self._last_action} + attrs.update(super().device_state_attributes) + return attrs + + def parse_data(self, data, raw_data): + """Parse data sent by gateway.""" + value = data.get(self._data_key) + if value not in ('vibrate', 'tilt', 'free_fall'): + _LOGGER.warning("Unsupported movement_type detected: %s", + value) + return False + + self.hass.bus.fire('xiaomi_aqara.movement', { + 'entity_id': self.entity_id, + 'movement_type': value + }) + self._last_action = value + + return True + + class XiaomiButton(XiaomiBinarySensor): """Representation of a Xiaomi Button.""" diff --git a/homeassistant/components/sensor/xiaomi_aqara.py b/homeassistant/components/sensor/xiaomi_aqara.py index 31366fe0097..33517e957b9 100644 --- a/homeassistant/components/sensor/xiaomi_aqara.py +++ b/homeassistant/components/sensor/xiaomi_aqara.py @@ -41,6 +41,15 @@ def setup_platform(hass, config, add_entities, discovery_info=None): elif device['model'] in ['gateway', 'gateway.v3', 'acpartner.v3']: devices.append(XiaomiSensor(device, 'Illumination', 'illumination', gateway)) + elif device['model'] in ['vibration']: + devices.append(XiaomiSensor(device, 'Bed Activity', + 'bed_activity', gateway)) + devices.append(XiaomiSensor(device, 'Tilt Angle', + 'final_tilt_angle', gateway)) + devices.append(XiaomiSensor(device, 'Coordination', + 'coordination', gateway)) + else: + _LOGGER.warning("Unmapped Device Model ") add_entities(devices) @@ -84,6 +93,9 @@ class XiaomiSensor(XiaomiDevice): value = data.get(self._data_key) if value is None: return False + if self._data_key in ['coordination', 'status']: + self._state = value + return True value = float(value) if self._data_key in ['temperature', 'humidity', 'pressure']: value /= 100