Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Jul 9, 2023
0 parents commit 2aedfba
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# ResetZHASensor

This project was heavly inspired by [WernerHP's Aqara Motion Sensors AppDaemon app](https://github.com/wernerhp/ha.appdaemon.aqara_motion_sensors). Many thanks to WernerHP for the original idea and implementation.

## Introduction

ResetZHASensor is an AppDaemon app for Home Assistant designed to manage Xiaomi Aqara motion sensors in a Zigbee Home Automation (ZHA) setup. It works in conjunction with a hardware modification that allows the sensors to detect motion every 5 seconds. The app resets the sensor states in ZHA after a specified timeout, enabling the sensors to toggle state every few seconds. This approach helps avoid 'ghost' motion events caused by state desynchronization between Home Assistant and ZHA, ensuring accurate motion detection.

Please note that a hardware modification to the Xiaomi Aqara motion sensors is necessary for this app to function as intended. The hardware modification allows the sensors to detect motion more frequently than they do by default. Without this modification, the sensors would only be able to detect motion every 120 seconds.

## HACS Installation

1. Make sure you have the option "Enable AppDaemon apps discovery & tracking". This is located in: Configuration -> Integrations -> HACS (options).
2. Restart Home Assistant.
3. Go to HACS -> Automation -> search for "ResetZHASensor" and install it.
4. Follow the app configuration section below.

## Manual Installation

Download the `reset_zha_sensor` directory from inside the `apps` directory to your local `apps` directory, then configure the `reset_zha_sensor` module in `apps.yaml`.

## App Configuration

```yaml
reset_zha_sensor:
module: reset_zha_sensor
class: ResetZHASensor
timeout: 5
motion_sensors:
- entity_id: binary_sensor.kitchen_motion_sensor_motion
ieee: '00:15:8d:00:04:05:85:98'
- entity_id: binary_sensor.bathroom_motion_sensor_motion
ieee: '00:15:8d:00:04:05:85:98'
- entity_id: binary_sensor.bathroom_motion_sensor_2_motion
ieee: '00:15:8d:00:05:81:2c:ac'
```
| key | optional | type | default | description |
| --- | --- | --- | --- | --- |
| `module` | False | string | | The module name of the app. |
| `class` | False | string | | The name of the Class. |
| `timeout` | True | int | 5 | Timeout after which motion sensor state is set to off. |
| `motion_sensors` | False | list | | A list of motion sensor entity_ids and their corresponding ieee addresses. |
48 changes: 48 additions & 0 deletions apps/reset_zha_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import hassapi as hass

MODULE = "reset_zha_sensor"
CLASS = "ResetZHASensor"

CONF_TIMEOUT = "timeout"
CONF_MOTION_SENSORS = "motion_sensors"

DEFAULT_TIMEOUT = 5

class ResetZHASensor(hass.Hass):
"""
A class to interact with Aqara motion sensors, sets them to unoccupied after a specific timeout.
"""

def initialize(self):
"""
Initialize the ResetZHASensor app. Listens for state changes in the motion sensors.
"""
self.motion_sensors = self.args.get(CONF_MOTION_SENSORS, [])
self.timeout = self.args.get(CONF_TIMEOUT, DEFAULT_TIMEOUT)

self.log(f"Initializing ResetZHASensor with sensors: {self.motion_sensors} and timeout: {self.timeout}", level="INFO")

for sensor in self.motion_sensors:
self.listen_state(self.motion_detected, sensor['entity_id'])

def motion_detected(self, entity, attribute, old, new, kwargs):
"""
Handles state change of motion sensor, sets state to "on" if new state is "on".
Schedules to set state to "off" after a timeout.
"""
if new == "on":
self.log(f"Motion detected by {entity}. Scheduling reset.", level="INFO")
self.run_in(self.reset_sensor_state, self.timeout, sensor=entity)

def reset_sensor_state(self, kwargs):
"""
Resets the state of the motion sensor in ZHA.
"""
entity = kwargs.get("sensor")
sensor = next((s for s in self.motion_sensors if s['entity_id'] == entity), None)
if sensor is None:
self.log(f"No sensor found for entity {entity}", level="ERROR")
return

self.log(f"Resetting sensor state for {sensor['ieee']}", level="INFO")
self.call_service("zha/set_zigbee_cluster_attribute", ieee=sensor['ieee'], endpoint_id=1, cluster_id=1280, cluster_type="in", attribute=2, value=0)
4 changes: 4 additions & 0 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "ResetZHASensor",
"render_readme": true
}

0 comments on commit 2aedfba

Please sign in to comment.