Polling device type is configurable now.

This commit is contained in:
2017-02-02 19:46:30 +03:00
parent 96073ac022
commit 45b15578a7
3 changed files with 20 additions and 17 deletions
+2
View File
@@ -8,6 +8,8 @@ mqtt:
gateway: gateway:
password: passw0rd password: passw0rd
polling_interval: 2 polling_interval: 2
polling_models:
- motion
sids: sids:
# motion # motion
+18 -14
View File
@@ -29,23 +29,26 @@ def process_gateway_messages(gateway, client):
except Exception as e: except Exception as e:
_LOGGER.error('Error while sending from gateway to mqtt: ', str(e)) _LOGGER.error('Error while sending from gateway to mqtt: ', str(e))
def read_motion_data(gateway, client, polling_interval): def read_motion_data(gateway, client, polling_interval, polling_models):
first = True first = True
while True: while True:
try: try:
for device in gateway.XIAOMI_DEVICES['binary_sensor']: for device_type in gateway.XIAOMI_DEVICES:
model = device.get("model", "") devices = gateway.XIAOMI_DEVICES[device_type]
if (model!="motion"): for device in devices:
continue model = device.get("model", "")
sid = device['sid'] if (model not in polling_models):
continue
sid = device['sid']
_LOGGER.debug("Polling " + str(model) + " with sid: " + str(sid))
sensor_resp = gateway.get_from_hub(sid) sensor_resp = gateway.get_from_hub(sid)
data = json.loads(sensor_resp['data']) data = json.loads(sensor_resp['data'])
state = data.get("status", None) state = data.get("status", None)
short_id = sensor_resp['short_id'] short_id = sensor_resp['short_id']
if (device['data'] != data or first): if (device['data'] != data or first):
device['data'] = data device['data'] = data
client.publish(model, sid, data) client.publish(model, sid, data)
first = False first = False
except Exception as e: except Exception as e:
_LOGGER.error('Error while sending from mqtt to gateway: ', str(e)) _LOGGER.error('Error while sending from mqtt to gateway: ', str(e))
@@ -71,6 +74,7 @@ if __name__ == "__main__":
config=yamlparser.load_yaml('config/config.yaml') config=yamlparser.load_yaml('config/config.yaml')
gateway_pass = yamlparser.get_gateway_password(config) gateway_pass = yamlparser.get_gateway_password(config)
polling_interval = config['gateway'].get("polling_interval", 2) polling_interval = config['gateway'].get("polling_interval", 2)
polling_models = config['gateway'].get("polling_models", ['motion'])
_LOGGER.info("Init mqtt client.") _LOGGER.info("Init mqtt client.")
client = mqtt.Mqtt(config) client = mqtt.Mqtt(config)
@@ -88,7 +92,7 @@ if __name__ == "__main__":
t2.daemon = True t2.daemon = True
t2.start() t2.start()
t3 = threading.Thread(target=read_motion_data, args=[gateway, client, polling_interval]) t3 = threading.Thread(target=read_motion_data, args=[gateway, client, polling_interval, polling_models])
t3.daemon = True t3.daemon = True
t3.start() t3.start()
-3
View File
@@ -231,7 +231,6 @@ class XiaomiDevice():
self._sid = device['sid'] self._sid = device['sid']
self._name = '{}_{}'.format(name, self._sid) self._name = '{}_{}'.format(name, self._sid)
self.parse_data(device['data']) self.parse_data(device['data'])
self._last_event_id = -1
self.xiaomi_hub = xiaomi_hub self.xiaomi_hub = xiaomi_hub
xiaomi_hub.XIAOMI_HA_DEVICES[self._sid].append(self) xiaomi_hub.XIAOMI_HA_DEVICES[self._sid].append(self)
@@ -249,6 +248,4 @@ class XiaomiDevice():
return True return True
def parse_data(self, data): def parse_data(self, data):
print(data)
# self._last_event_id = data
return True return True