Skip to content

Commit

Permalink
Motion & Magnet integration
Browse files Browse the repository at this point in the history
 - pyAqara v0.4 compatible
 - motion & magnet works now
  • Loading branch information
fooxy committed Dec 20, 2016
1 parent 78330a9 commit 5fbcd43
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 48 deletions.
48 changes: 0 additions & 48 deletions aqara.py

This file was deleted.

41 changes: 41 additions & 0 deletions custom_components/aqara.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from homeassistant.components.discovery import load_platform
from homeassistant.const import (EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP)
from homeassistant.helpers.entity import Entity

import logging

_LOGGER = logging.getLogger(__name__)

DOMAIN = 'aqara'

REQUIREMENTS = ['https://github.com/fooxy/homeassisitant-pyAqara/archive/v0.4-alpha.zip#pyAqara==0.4']

AQARA_COMPONENTS = [
'binary_sensor', 'sensor'
]

def setup(hass, config):
"""Your controller/hub specific code."""

from pyAqara.gateway import AqaraGateway
gateway = AqaraGateway()
gateway.initGateway()
gateway.listen(timeout=5)

hass.data['AqaraGateway']= gateway

def _stop(event):
"""Stop the listener queue and clean up."""
print('stop event')
nonlocal gateway
gateway.stop()
gateway = None
_LOGGER.info("Waiting for long poll to Aqara Gateway to time out")

hass.bus.listen(EVENT_HOMEASSISTANT_STOP, _stop)

for component in AQARA_COMPONENTS:
load_platform(hass, component, DOMAIN, {}, config)

return True
98 changes: 98 additions & 0 deletions custom_components/binary_sensor/aqara.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from homeassistant.helpers.entity import Entity
from homeassistant.components.binary_sensor import BinarySensorDevice
import logging
import json

# DOMAIN = 'aqara'

_LOGGER = logging.getLogger(__name__)


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the sensor platform."""

# # get the gateway object from the hub component
gateway = hass.data['AqaraGateway']
devices = gateway.sidsData

sensorItems = []
for device in devices:
if device['model'] in ['motion', 'magnet']:
sensorItems.append(AqaraBinarySensor(gateway, device['sid'], device['sid'], device['model'],device['data']))

if len(sensorItems)> 0:
add_devices(sensorItems)
return True
else:
return False

class AqaraBinarySensor(BinarySensorDevice,Entity):
"""Representation of a Binary Sensor."""

def __init__(self, aqaraGateway,deviceName,deviceSID,deviceModel,deviceData):
self.gateway = aqaraGateway
self.deviceName = deviceName
self.deviceSID = deviceSID
self.deviceModel = deviceModel
self.deviceData = deviceData
self.uniqueID = '{} {}'.format(deviceModel,deviceSID)

self.gateway.register(self.deviceSID, self._update_callback)

status = deviceData['status']
if deviceModel == 'magnet':
if status =='close':
self._state = True
else:
self._state = False
elif deviceModel == 'motion':
if status =='motion':
self._state = True
else:
self._state = False
else:
self._state = False

def _update_callback(self, model,sid, cmd, data):
if sid == self.deviceSID:
if 'status' in data:
self.pushUpdate(model, data['status'])

@property
def should_poll(self):
"""No polling needed for a demo light."""
return False

@property
def unique_id(self):
"""Return the unique id"""
return self.uniqueID

@property
def name(self):
"""Return the name of the sensor."""
return self.uniqueID

@property
def sensor_class(self):
"""Return the class of this sensor, from SENSOR_CLASSES."""
return self.deviceModel

@property
def is_on(self):
"""Return true if binary sensor is on."""
return self._state

def pushUpdate(self,model,status):
if model == 'magnet':
if status =='close':
self._state = True
else:
self._state = False
elif model == 'motion':
if status =='motion':
self._state = True
else:
self._state = False

super().update_ha_state()
92 changes: 92 additions & 0 deletions custom_components/sensor/aqara.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from homeassistant.const import TEMP_CELSIUS
from homeassistant.helpers.entity import Entity
import logging
import json

# DOMAIN = 'aqara'

_LOGGER = logging.getLogger(__name__)
SENSOR_TYPES = ['temperature', 'humidity']


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the sensor platform."""

# # get the gateway object from the hub component
gateway = hass.data['AqaraGateway']
devices = gateway.sidsData

sensorItems = []
for variable in SENSOR_TYPES:
for device in devices:
if device['model'] == 'sensor_ht':
sensorItems.append(AqaraSensor(gateway, device['sid'], device['sid'], device['model'], variable,device['data']))

if len(sensorItems)> 0:
add_devices(sensorItems)
return True
else:
return False

class AqaraSensor(Entity):
"""Representation of a Binary Sensor."""

def __init__(self, aqaraGateway,deviceName,deviceSID,deviceModel,deviceVariable,deviceData):
self.gateway = aqaraGateway
self.deviceName = deviceName
self.deviceSID = deviceSID
self.deviceModel = deviceModel
self.deviceVariable = deviceVariable
self.uniqueID = '{} {}'.format(deviceVariable, deviceSID)

self.gateway.register(self.uniqueID, self._update_callback)

if deviceVariable == 'temperature':
self._state = float(deviceData['temperature'])/100
elif deviceVariable == 'humidity':
self._state = float(deviceData['humidity'])/100
else:
self._state = None

def _update_callback(self, model,sid, cmd, data):
if sid == self.deviceSID:
self.pushUpdate(data)
else:
_LOGGER.error('Issue to update the sensor ',sid)

@property
def should_poll(self):
"""No polling needed for a demo light."""
return False

@property
def unique_id(self):
"""Return the unique id"""
return self.uniqueID

@property
def name(self):
"""Return the name of the sensor."""
return self.uniqueID

@property
def state(self):
"""Return the state of the sensor."""
return self._state

@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
if self.deviceVariable == "temperature":
return TEMP_CELSIUS
elif self.deviceVariable == "humidity":
return '%'

def pushUpdate(self,data):
if self.deviceVariable == 'temperature':
if 'temperature' in data:
self._state = float(data['temperature'])/100
elif self.deviceVariable == 'humidity':
if 'humidity' in data:
self._state = float(data['humidity'])/100
super().update_ha_state()
Binary file added img/IMG_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/IMG_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/IMG_04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/IMG_05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/IMG_06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/IMG_07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5fbcd43

Please sign in to comment.