diff --git a/config/config-sample.yaml b/config/config-sample.yaml index 39b3faf..5ee8909 100644 --- a/config/config-sample.yaml +++ b/config/config-sample.yaml @@ -8,6 +8,8 @@ mqtt: gateway: password: passw0rd polling_interval: 2 + polling_models: + - motion sids: # motion diff --git a/src/main.py b/src/main.py index 77775f9..46d33fe 100644 --- a/src/main.py +++ b/src/main.py @@ -29,23 +29,26 @@ def process_gateway_messages(gateway, client): except Exception as 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 while True: try: - for device in gateway.XIAOMI_DEVICES['binary_sensor']: - model = device.get("model", "") - if (model!="motion"): - continue - sid = device['sid'] + for device_type in gateway.XIAOMI_DEVICES: + devices = gateway.XIAOMI_DEVICES[device_type] + for device in devices: + model = device.get("model", "") + 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) - data = json.loads(sensor_resp['data']) - state = data.get("status", None) - short_id = sensor_resp['short_id'] - if (device['data'] != data or first): - device['data'] = data - client.publish(model, sid, data) + sensor_resp = gateway.get_from_hub(sid) + data = json.loads(sensor_resp['data']) + state = data.get("status", None) + short_id = sensor_resp['short_id'] + if (device['data'] != data or first): + device['data'] = data + client.publish(model, sid, data) first = False except Exception as 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') gateway_pass = yamlparser.get_gateway_password(config) polling_interval = config['gateway'].get("polling_interval", 2) + polling_models = config['gateway'].get("polling_models", ['motion']) _LOGGER.info("Init mqtt client.") client = mqtt.Mqtt(config) @@ -88,7 +92,7 @@ if __name__ == "__main__": t2.daemon = True 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.start() diff --git a/src/xiaomihub.py b/src/xiaomihub.py index 26830d2..352fac8 100644 --- a/src/xiaomihub.py +++ b/src/xiaomihub.py @@ -231,7 +231,6 @@ class XiaomiDevice(): self._sid = device['sid'] self._name = '{}_{}'.format(name, self._sid) self.parse_data(device['data']) - self._last_event_id = -1 self.xiaomi_hub = xiaomi_hub xiaomi_hub.XIAOMI_HA_DEVICES[self._sid].append(self) @@ -249,6 +248,4 @@ class XiaomiDevice(): return True def parse_data(self, data): - print(data) - # self._last_event_id = data return True \ No newline at end of file