diff --git a/aqara.py b/aqara.py deleted file mode 100644 index 9dbcf03..0000000 --- a/aqara.py +++ /dev/null @@ -1,48 +0,0 @@ -import logging - -DOMAIN = 'Aqara' - -_LOGGER = logging.getLogger(__name__) - -REQUIREMENTS = ['https://github.com/fooxy/homeassisitant-pyAqara/archive/v0.3-alpha.zip#pyAqara==0.3'] - - -def setup_platform(hass, config, add_devices_callback, discovery_info=None): - """Setup the sensor platform.""" - from pyAqara.sensor import Sensor, HTSensorData, MagnetData, MotionData - from pyAqara.gateway import AqaraGateway - # get the gateway and init it's ip / port via whoami - gateway = AqaraGateway() - gateway.initGateway() - - # get all sensors and format them for HASS - sensorItems = [] - dynamicDevices = gateway.get_devicesList() - for device in dynamicDevices: - deviceResponse = gateway.get_read(device) - model = deviceResponse['model'] - sid = deviceResponse['sid'] - # TODO: find a way to get the configured name from the gateway - if model == 'sensor_ht': - temperatureSensorData = HTSensorData(gateway) - sensorItems.append( - Sensor(temperatureSensorData, sid, sid, 'temperature')) - humiditySensorData = HTSensorData(gateway) - sensorItems.append( - Sensor(humiditySensorData, sid, sid, 'humidity')) - elif model == 'magnet': - sensorData = MagnetData(gateway) - sensorItems.append(Sensor(sensorData, sid, sid, 'magnet')) - # elif model == 'switch': - # TODO: figure out how switches work - # sensorData = SwitchData(gateway) - # sensorItems.append(Sensor(sensorData, sid, sid, 'switch')) - elif model == 'motion': - sensorData = MotionData(gateway) - sensorItems.append(Sensor(sensorData, sid, sid, 'motion')) - # TODO: implement support for other sensors - # elif model == - # 'plug/ctrl_neutral1/ctrl_neutral2/gateway' - - add_devices_callback(sensorItems) - return True \ No newline at end of file diff --git a/custom_components/aqara.py b/custom_components/aqara.py new file mode 100644 index 0000000..b607efd --- /dev/null +++ b/custom_components/aqara.py @@ -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 \ No newline at end of file diff --git a/custom_components/binary_sensor/aqara.py b/custom_components/binary_sensor/aqara.py new file mode 100644 index 0000000..7e5dd2c --- /dev/null +++ b/custom_components/binary_sensor/aqara.py @@ -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() diff --git a/custom_components/sensor/aqara.py b/custom_components/sensor/aqara.py new file mode 100644 index 0000000..c3b18fb --- /dev/null +++ b/custom_components/sensor/aqara.py @@ -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() \ No newline at end of file diff --git a/img/IMG_01.png b/img/IMG_01.png new file mode 100644 index 0000000..2d41cd3 Binary files /dev/null and b/img/IMG_01.png differ diff --git a/img/IMG_02.png b/img/IMG_02.png new file mode 100644 index 0000000..b87cb8d Binary files /dev/null and b/img/IMG_02.png differ diff --git a/img/IMG_04.png b/img/IMG_04.png new file mode 100644 index 0000000..04fdfc7 Binary files /dev/null and b/img/IMG_04.png differ diff --git a/img/IMG_05.png b/img/IMG_05.png new file mode 100644 index 0000000..ffc83a4 Binary files /dev/null and b/img/IMG_05.png differ diff --git a/img/IMG_06.png b/img/IMG_06.png new file mode 100644 index 0000000..67993ea Binary files /dev/null and b/img/IMG_06.png differ diff --git a/img/IMG_07.png b/img/IMG_07.png new file mode 100644 index 0000000..a152b56 Binary files /dev/null and b/img/IMG_07.png differ