monster1025 / aqara-mqtt

Aqara (Xiaomi) Gateway to MQTT bridge (I use it for home assistant integration)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: Should sensor data stream in automaticly?

groggemans opened this issue · comments

I'm having several issues, but I'm probably just doing it wrong.

I'm trying to get all sensor data from my aqara sensors into rabbitmq (using the mqtt plugin). I'm not using Home Assistant.

I'm currently running the x64 container. It connects to the gateway and detects my 14 sensors, which I've added to the config file. (It was not clear at first where I could find the sids but after running locally with debug output I got the necessary info)

There are a few mqtt messages for the sensor types in polling_models, but not for all sensors of those models. Other sensor models don't generate messages at all.

These are my sensor models:

  • 86sw1
  • sensor_wleak.aq1
  • sensor_motion.aq2
  • sensor_magnet.aq2
  • sensor_switch.aq2
  • weather.v1

I've also noticed that most of my sensor types are not defined in the code. But the code that should tell that these are not supported is never executed.

device_type = None
            if model in sensors:
                device_type = 'sensor'
            elif model in binary_sensors:
                device_type = 'binary_sensor'
            elif model in switches:
                device_type = 'switch'
            else:
                device_type = 'sensor' #not really matters

            if device_type == None:
                _LOGGER.error('Unsupported devices : {0}'.format(model))
            else:
                self.XIAOMI_DEVICES[device_type].append(xiaomi_device)

The code logging an error will never be reached because the device_type is always set by the last else in the model checking if. Happy to do a PR to make it log the error, or to remove the obsolete code.

If it's ok I'll also do a PR to add the missing sensor models. But I was wondering if it wouldn't be more logical to regex match the model string to find the sensor type. (.*motion.*, .*switch.*,...).

I dit a small local test with my sensor types added, but still no mqtt messages for sensors not in the polling list.

Thank you for your work and your time!

PS:
Is there a reason why you don't auto add the detected sensors and make the config with a recognizable name optional? (it would be nice for my use case)

Hi.

It was not clear at first where I could find the sids but after running locally with debug output I got the necessary info

The main logic of gateway to change SID to 'nice' name when it found in config. If it can't find sesor sid in config - it will send raw sid value instead of nice name.

There are a few mqtt messages for the sensor types in polling_models

Usually you don't need polling, i have writed this routine when I have problems with connection to aqara gateway (my raspberry pi was connected to Xiaomi\Aqara gw via wifi connection and some of my packets was dropped, but when i switched to ethernet - I didn't have any packet miss). But if you need polling of this models - you can freely add it to arrays.
This scheme is not perfect because I have taken code that works with aqara gw from lazcad plugin, and I want to rewrite it later (I need to remove all device type checking, and maybe rewrite all communication logic, because it didn't support multi-threading requests) - but didn't have any free time for now =(

These are my sensor models:

All un-supported device types must work with 'default' sensor type('sensor'). And yes - code bellow will never be executed:

            if device_type == None:
                _LOGGER.error('Unsupported devices : {0}'.format(model))

I dit a small local test with my sensor types added, but still no mqtt messages for sensors not in the polling list.

It must work without polling (at least battery reports - if no 'events' occur in sensors, but for example - if you press button on 'sensor_switch.aq2' - it must generate click event, you can't 'poll' switches, it can be read only while 'listening' for events).
Try to switch logging level to DEBUG and click on button - you must see click events in log.

But also - if you have not known 'models' - i think you will need to add your models to code bellow, because it change 'state' values to more accurate (for example xiaomi motion sensor sends 'motion=true' - and after motion is compleate it sends 'no_motion' property with time -> we need to fix it to motion=false):

    event_based_sensors = ["switch", "cube"]
    motion_sensors = ["motion", "sensor_motion.aq2"]
    magnet_sensors = ["magnet"]

Is there a reason why you don't auto add the detected sensors and make the config with a recognizable name optional? (it would be nice for my use case)

It must work 'out-of-box', you will get sids and original model instead of 'human-readable' names =)

P.S. PR are always welcome =)

Oke, thanks for the reply! I'll try to do some more debugging.

Let me first create a PR to add my sensors models and to remove the dead code.

A little more debugging showed me that my local firewall was one of the issues. After disabling the firewall messages from the gateway started coming in. Can you tell me some more about which ports should be open? or on which port this application will listen?

Messages are now coming in, but still aren't send to mqtt. I'll try to do some more debugging to figure this out.

It will listen on UDP:

    MULTICAST_PORT = 9898
    GATEWAY_DISCOVERY_PORT = 4321

and also it will connect to 1883 (? - as described in config) TCP - MQTT.

Port 9898 seems to be enough. Also solved a few other issues at my end.

I think I can get it going now. Thanks for your assistance!