Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'lumi.gateway.mieu01' subdevices #728

Open
javicalle opened this issue Jun 13, 2020 · 118 comments
Open

Add support for 'lumi.gateway.mieu01' subdevices #728

javicalle opened this issue Jun 13, 2020 · 118 comments

Comments

@javicalle
Copy link
Contributor

The EU version of the gateway is supported in the current versión of the library and can be intergated in HA.

Some tests had been made to evaluate subdevices resolution with no success:

(venv)$ python /tmp/test_miio_gateway/gateway_devices.py
Traceback (most recent call last):
  File "/tmp/test_miio_gateway/gateway_devices.py", line 5, in <module>
    gateway.discover_devices()
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/gateway.py", line 168, in discover_devices
    devices_raw = self.get_prop("device_list")
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/gateway.py", line 207, in get_prop
    return self.send("get_device_prop", ["lumi.0", property])
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/device.py", line 147, in send
    command, parameters, retry_count, extra_parameters=extra_parameters
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 203, in send
    self._handle_error(payload["error"])
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 263, in _handle_error
    raise DeviceError(error)
miio.exceptions.DeviceError: {'code': -5005, 'message': 'params error'}

where gateway_devices.py:

from miio import Gateway

gateway = Gateway("192.168.1.IP", "tokentokentoken")

gateway.discover_devices()
devices = gateway.devices

for dev in devices:
    dev.update()
    print(dev)

@starkillerOG sugest (home-assistant/core#22078) a new approach with the get_device_list command.

@javicalle javicalle changed the title Add support for lumi.gateway.mieu01 subdevices Add support for 'lumi.gateway.mieu01' subdevices Jun 13, 2020
@starkillerOG
Copy link
Contributor

@javicalle alright lets see if we can get this working:

  1. try testing this code as a start:
from miio import Gateway
gateway = Gateway("192.168.1.IP", "tokentokentoken")
print(gateway.info())

That will print basic information about your gateway and that will confirm that the communication is okay. (although since it works with the HomeAssistant integration this schould not be a problem)

  1. open the gateway.py file of the miio library that schould be located in your virtuel python enviroment (in my case venv) at a path simular to: venv/lib/python3.7/site-packages/miio/gateway.py

  2. scroll down to line 151 and add this function in between the def devices(self): and def discover_devices(self): functions (actually the postion where you add it does not matter that much):

    @property
    def test(self):
        return self.send("get_device_list")
  1. now try running this test code:
from miio import Gateway
gateway = Gateway("192.168.1.IP", "tokentokentoken")
print(gateway.test())

Hopefully that will print a list of your subdevices..... fingers crossed....

@starkillerOG
Copy link
Contributor

@javicalle actually now that I think of it, steps 2 till 4 can simply be replaced by running this test code:

from miio import Gateway
gateway = Gateway("192.168.1.IP", "tokentokentoken")
print(gateway.send("get_device_list"))

@javicalle
Copy link
Contributor Author

Here are the results (some values have been obfuscated):

lumi.gateway.mieu01 v3.5.8_147 (AA:BB:CC:DD:EE:FF) @ 192.168.0.166 - token: 12345678901234567890123456789012

and the gateway.send("get_device_list") part:

Got error when receiving: timed out
Traceback (most recent call last):
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 182, in send
    data, addr = s.recvfrom(1024)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 182, in send
    data, addr = s.recvfrom(1024)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 182, in send
    data, addr = s.recvfrom(1024)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 182, in send
    data, addr = s.recvfrom(1024)
socket.timeout: timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/tmp/test_miio_gateway/gateway_devices.py", line 8, in <module>
    print(gateway.send("get_device_list"))
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/device.py", line 147, in send
    command, parameters, retry_count, extra_parameters=extra_parameters
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 226, in send
    extra_parameters=extra_parameters,
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 226, in send
    extra_parameters=extra_parameters,
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 226, in send
    extra_parameters=extra_parameters,
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 230, in send
    raise DeviceException("No response from the device") from ex
miio.exceptions.DeviceException: No response from the device

@javicalle
Copy link
Contributor Author

Also tested with same results:

gateway = Gateway("192.168.0.166", "12345678901234567890123456789012", lazy_discover=False, start_id=1000)

@starkillerOG
Copy link
Contributor

Also tested with same results:

gateway = Gateway("192.168.0.166", "12345678901234567890123456789012", lazy_discover=False, start_id=1000)

That was indeed what I was going to sugest ;)

Well it does look like your gateway is recognizing the "get_device_list" command.
If I run the same command on my gateway.v3 I get a replay of:
miio.exceptions.DeviceError: {'code': -32601, 'message': 'Method not found.'}

Could you try running some random fake command like "test123" and see if you then also get the Method not found error, that would comfirm that your gateway recognizes the "get_device_list" command.

How many subdevices do you have connected to your gateway?
The timeout error seems to be very simular to the issue we are currently having with PR: #650

Unfortuanately I do not yet have an idea how to resolve that timeout issue....
Is your gateway updated to the latest firmware available?

@starkillerOG
Copy link
Contributor

@rezmus do you have an idea where this timeout is comming from, is it indeed because too many devices are connected and the response message grows too big for the gateway?

Do you have any ideas how to resolve that?
(sorry for involving you, but you seem to know a lot about these gateways)

@rezmus
Copy link

rezmus commented Jun 13, 2020

lumi.gateway.miXXXX and lumi.gateway.aqhm0X do not provide a way to get subdevices list. you need to define them yourself.

@javicalle
Copy link
Contributor Author

javicalle commented Jun 13, 2020

I get the same error.

Now the file content is:

from miio import Gateway

gateway = Gateway("192.168.0.166", "12345678901234567890123456789012", lazy_discover=False, start_id=1000)

print(gateway.info())
print('-----')

print(gateway.send("no_method"))

And there are 5 devices connected to gateway.

@starkillerOG
Copy link
Contributor

@javicalle apperently you cannot obtain the device list on the 'lumi.gateway.mieu01' according to @rezmus which is quite a dissapointment...
We could at some point see if sniffing traffic using bluestack and wireshark provides us with some way to discover which devices are connected, but since @rezmus knows a lot about the firmware I guess there is no command.

Although I do find it strage that the gateway does not simply give the "method not found" as response to the "get_device_list" so maybe there is still some hope...

For now lets first test if the subdevices actually work the same way, otherwise it is going to be a big effort to get 'lumi.gateway.mieu01' support.

@javicalle do you know the sid and devicetype of your 5 connected subdevices?
For me I can see them In the MiHome app -->gateway--> ... --> About --> Hub info (the same place where I can find the gateway token).

If so you can try:

from miio import gateway

hub = gateway.Gateway("192.168.0.166", "12345678901234567890123456789012", lazy_discover=False, start_id=1000)


sid = "lumi.SidSidSidSid"
device_type = 19
subdevice_cls = gateway.AqaraHT


dev_info = gateway.SubDeviceInfo(sid, device_type, 1, 1, 3)
dev = subdevice_cls(hub, dev_info)

dev.update()
print(dev)

where sid is the sid of the subdevice
dev_type is the number representing the subdevice type (can be looked up from the model in the gateway.py file in the list on lines 39 to 90 class DeviceType(IntEnum):)
and the subdevice class can also be found in this same list, in the example I used the AqaraHT temperature sensor (lumi.weather.v1)

@javicalle
Copy link
Contributor Author

javicalle commented Jun 14, 2020

I only have 3 different types of devices:

  • Mi Window and Door sensor
  • Mi Motion Sensor
  • Mi Wireless Switch

My "Xiaomi Home" app don't have this option (no 'About' option available), and I have get the device info from others sources.

The modified app gives to me this info:

Did:		lumi.158d000396cb70
Name:		Mi Window and Door Sensor
Model:		lumi.sensor_magnet.v2

Did:		lumi.158d0003a40c79
Name:		Mi Motion Sensor 2
Model:		lumi.sensor_motion.v2

Did:		lumi.158d0003a476f6
Name:		Mi Wireless Switch
Model:		lumi.sensor_switch.v2

If my interpretation is correct, it does not fit with any of the currently implemented models.

Anyway, I have tested the 2 available implementations with the motion sensor:

  • Motion = 2 # lumi.sensor_motion
  • AqaraMotion = 52 # lumi.sensor_motion.aq2

(I had to overwrite my venv's gateway class with the last one from the repository)

In both cases I have had the same error:

(venv)$ python /tmp/test_miio_gateway/gateway_subdevices.py
lumi.gateway.mieu01 v3.5.8_147 (AA:BB:CC:DD:EE:FF) @ 192.168.0.166 - token: 12345678901234567890123456789012
-----
Got error when receiving: timed out
Traceback (most recent call last):
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 182, in send
    data, addr = s.recvfrom(1024)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 182, in send
    data, addr = s.recvfrom(1024)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 182, in send
    data, addr = s.recvfrom(1024)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 182, in send
    data, addr = s.recvfrom(1024)
socket.timeout: timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/gateway.py", line 777, in send
    return self._gw.send(command, [self.sid])
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/device.py", line 147, in send
    command, parameters, retry_count, extra_parameters=extra_parameters
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 226, in send
    extra_parameters=extra_parameters,
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 226, in send
    extra_parameters=extra_parameters,
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 226, in send
    extra_parameters=extra_parameters,
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/miioprotocol.py", line 230, in send
    raise DeviceException("No response from the device") from ex
miio.exceptions.DeviceException: No response from the device

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/tmp/test_miio_gateway/gateway_subdevices.py", line 20, in <module>
    print(dev)
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/gateway.py", line 725, in __repr__
    self.get_battery(),
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/gateway.py", line 850, in get_battery
    self._battery = self.send("get_battery").pop()
  File "/opt/DevelopmentSpace/hassio/home-assistant/venv/lib/python3.7/site-packages/miio/gateway.py", line 781, in send
    ) from ex
miio.gateway.GatewayException: Got an exception while sending command get_battery

I continue doing some tests

@javicalle
Copy link
Contributor Author

I have modified my gateway.py this way:

class DeviceType(IntEnum):
    """DeviceType matching using the values provided by Xiaomi."""

    Unknown = -1
    Gateway = 0  # lumi.0
    Switch = 1  # lumi.sensor_switch
    Motion = 2  # lumi.sensor_motion
    MotionV2 = 222 # lumi.sensor_motion.v2 for testing purposes
    .../...
    @command()
    def discover_devices(self):
        """
        Discovers SubDevices
        and returns a list of the discovered devices.
        """
        # from https://github.com/aholstenson/miio/issues/26
        device_type_mapping = {
            DeviceType.Switch: Switch,
            DeviceType.Motion: Motion,
            DeviceType.MotionV2: MotionV2, # probably not necessary
class MotionV2(SubDevice):
    """Subdevice Motion specific properties and methods."""

    properties = []
    _zigbee_model = "lumi.sensor_motion.v2"
    _model = "RTCGQ01LM"
    _name = "Motion sensor v2"

The _model value is written on the physical device, so I'm sure it's correct.

Now, my test class is the next one:

from miio import gateway

hub = gateway.Gateway("192.168.0.166", "12345678901234567890123456789012", lazy_discover=False, start_id=1000)

print(hub.info())
print('-----')

sid = "lumi.158d0003a40c79"
device_type = 222
subdevice_cls = gateway.MotionV2

dev_info = gateway.SubDeviceInfo(sid, device_type, 1, 1, 3)
dev = subdevice_cls(hub, dev_info)

dev.update()
print(dev)
print('+++++')

With these changes I get exactly the same error.

@rezmus
Copy link

rezmus commented Jun 14, 2020

get_battery method is not supported by these hubs.

@javicalle
Copy link
Contributor Author

I just commented the print(dev) commands and no errors are generated.
But I can't find a send or get_property to validate it's really working against device.

@starkillerOG
Copy link
Contributor

@javicalle the motion sensor, door sensor and button do not have specific properties you can get. (only subscriptions to events but that still needs to be implemented).

Besides the get_battery there only are two other general commands known at this point:

dev.get_property("fw_ver") (get the firmware version of the subdevice)
dev.send("remove_device") (unpair the device from the gateway, may not want to try this...)

You could try those...

In general there are 5 functions to interact with the device:

dev.send("test_command")
dev.send_arg("test_command", ["argument1", "argument2"])
dev.get_property("property")
dev.get_property_exp(["property"])
dev.set_property("property", "value")

So you can play around with those functions, but you will need to know or be very lucky to guess the commands/properties....

If you truly want to figure out commands/properties you will need to sniff the trafic of the MiHome app and see if you can discover commands/properties that work with this gateway.

I was able to sniff trafic using BlueStack (an android emulator for windows), install MiHome App and then capture packets using wireshark --> export as json and use the miio tool https://github.com/aholstenson/miio to decode the packets.

@rezmus
Copy link

rezmus commented Jun 15, 2020

dev.get_property("voltage")
dev.get_property("lqi")

@starkillerOG
Copy link
Contributor

dev.get_property("voltage")
dev.get_property("lqi")

@rezmus on my gateway 'lumi.gateway.v3' those two give my empty responses [], the same as if I issue a random non existing property like dev.get_property("this_does_not_exist")

@javicalle but you can certainly try those on your gateway.

@javicalle
Copy link
Contributor Author

My results:

  • dev.get_property("fw_ver") --> ERROR
  • dev.get_property("voltage") --> OK: [2995]
  • dev.get_property("lqi") --> ERROR

@rezmus
Copy link

rezmus commented Jun 15, 2020

aqara hubs can read lqi, mi hubs can't.

@starkillerOG
Copy link
Contributor

@rezmus my hub 'lumi.gateway.v3' which is to my knowladge an chinese aqara hub gives an empty response on dev.get_property("lqi").

@javicalle intresting that the voltage works on 'lumi.gateway.mieu01' but does not on 'lumi.gateway.v3'

At this point it is pretty clear that the 'lumi.gateway.mieu01' supports different commands than the 'lumi.gateway.v3'. So someone who has acces to a 'lumi.gateway.mieu01' will have to do some development to get it supported with one of the biggest isues beeing there is no way to get the device_list.

If the device_list was the only problem I could have made a workaround by letting users configure a manual device_list, but since there are more commands that differ, that method is going to run into problems. withouth having acces to the device ('lumi.gateway.mieu01') I can therefore not easily implement support.... I am sorry @javicalle.

Of course it would be awesom if you could implement support @javicalle and I am open to helping and giving feedback on PR's for that, but that will be some effort.

@rezmus
Copy link

rezmus commented Jun 15, 2020

lumi.gateway.v3 - mi cn (called 2nd gen)

lumi.gateway.miXXXX - mi global hubs
lumi.gateway.aqhm0X - aqara hubs

in contrast to mi cn, aqara and mi global have almost similar hardware/firmware and run embedded linux (arm), but aqara is way better supported (new devices).

all 3 hubs should work more or less the same for core methods to get/set props and invoke actions on subdevices, however they may vary on the list of supported models and minor stuff.

@starkillerOG
Copy link
Contributor

This schould prevent some errors on the 'lumi.gateway.mieu01': #732

@rezmus do you have any sugestion to get a list of connected devices for 'lumi.gateway.mieu01' (does not matter if we get zigbee_id or the integer mapping values)?
Would it be hard/possible to get the device list from the cloud (is there some python libary to do this)?

@rezmus
Copy link

rezmus commented Jun 17, 2020

i took a look at firmware and i doubt there is such method. also never seen xiaomi cloud api reverse engineered. i think only solution is custom mi home version which dumps whole devices list to file.

@rytilahti
Copy link
Owner

rytilahti commented Jun 17, 2020

There are some pieces of the cloud api used by openhab (namely for the token extraction, https://www.openhab.org/addons/bindings/miio/#tokens), so it could be possible to obtain other information the same way.

I don't know about other open source implementations for the API, porting the cloud access would require some serious effort, especially to keep the details similar to openhab's to avoid getting blocked by the servers. As like to keep this library offline-only, such functionality should be externalized to another project.

@starkillerOG
Copy link
Contributor

Thanks for the hint @rytilahti!
I looked at the source code of the openhab miio addon and indeed they figured out a cloud connection using the acount name and password that you also use in the app.
From the looks of it there is already code to get the tokens and device list (not only for the gateway but also for other general miio devices)
The source code is here:

Unfortunately the code is written in java which I don't know.
I don't have time to deceiver the java and make a python lib from it, but someone will probably have to do that to get the 'lumi.gateway.mieu01' properly supported.

@javicalle do you happen to know java?

@starkillerOG
Copy link
Contributor

@rytilahti I agree that online cloud connection code would be best implemented in a separate python library.
Would it be alright with you if such a separate library would be used within python-miio as a dependency?

@rytilahti
Copy link
Owner

@starkillerOG I don't think it's a good idea to make python-miio to be directly dependent, if such library would be created. I think a proper way would be to make it possible to feed a list of available devices to the gateway class, then it wouldn't be dependent on any potential issues with the cloud access. The device list could simply be a simple ini/json configuration file with list of devices (and other things, if needed), which could also be manually filled, e.g., based on the information shown by the official app.

@tbarbette
Copy link

tbarbette commented Mar 16, 2021

Ticked, and I have DE too (my locale is BE). I actually re-installed the gateway from the informations given by micloud (ip and token) as that gives you the token very easily so it's the same. One thing maybe, in the micloud json, the gateway is the second device as the first one is a Xiaomi camera.

@starkillerOG
Copy link
Contributor

@tbarbette could you try running this python code (preferably inside the virtual enviroment of HomeAssistant)?

from micloud import MiCloud

mc = MiCloud("USERNAME", "PASSWORD")
mc.login()
token = mc.get_token() # to get your cloud service token.
device_list = mc.get_devices(country="de") # get list of devices
print(device_list )

And then see if the gateway is listed in the dict

@starkillerOG
Copy link
Contributor

@tbarbette the order in which the devices are listed is not importend, a for loop over the devices is made to see if the gateway can be found:

# find the gateway
for device in device_dict:
if device["mac"] == self.mac:
self._did = device["did"]
break
# check if the gateway is found
if self._did is None:
_LOGGER.error(
"Could not find gateway with ip '%s', mac '%s', model '%s' in the cloud device list response",
self.ip,
self.mac,
self.model,
)
return self._devices

@starkillerOG
Copy link
Contributor

@tbarbette Note that this needs to be an exact match of the mac (case and format sensitive).
So maybe the format of the mac is diffrent??? I would need the output of the python script above to compare with the error message to see what is going wrong...

@jonasCr
Copy link

jonasCr commented Mar 16, 2021

Hi,
I'm trying to install it to test but no success as far.
This is the steps I made:

  • Uninstall the official miio integration
  • Put your miio folder in my custom_components folder
  • restart HA
  • I'm able to add my cloud account but then nothing more happend.. :(
    Is there something I'm missing ??
    I tried to restart HA after this but no success

I'm in spain so i put the de server

Thanks for your help

@starkillerOG
Copy link
Contributor

Also I think the "first setup" popup should start by proposing the cloud credentials, and then auto-discover the gateway and the token. So we could do everything directly in home assistant, without needing to go through the token extractor.

I agree, but that is something for a future PR, that is a bit more complicated than you might think:

  • it may not work for users that have internet acces (cloud) blocked, so the old method schould still be supported.
  • Other devices like vacuums etc will also be discoverd in that way and they schould also be setup. However then all those things will end up in one config entry, currently it is not possible to setup multiple devices from one config_entry (all mac's and models schould be stored to be able to still setup the integration if for instance internet is down and cloud is not available at the moment HomeAssistant starts).

@starkillerOG
Copy link
Contributor

@jonasCr do you see any errors in the log?
Do you have one or more of the devices listed here: https://github.com/home-assistant/home-assistant.io/blob/next/source/_integrations/xiaomi_miio.markdown#supported-subdevices ?
(Otherwise nothing will show up because other subdevices are not yet supported)

@jonasCr
Copy link

jonasCr commented Mar 16, 2021

Ook, that's it. Mine are in the Recognized section but not in the supported :( ...
Well, I'm not going to be able to try it... But well done !! That's a big step! Thanks for your job.
Let me know if more device are added

@starkillerOG
Copy link
Contributor

@jonasCr As I see that there is quite some intrest in subdevice support for the Xiaomi Miio Gateway integration, I might see if I can pick up PR #709 to get event driven (sub)devices supported.

@jonasCr
Copy link

jonasCr commented Mar 16, 2021

Yeah, I think it's because here there are selling a kit with the kit and 5 sensors (2 doors, 2 motion and 1 button) and all of them are recognized but not supported, for now. Hopefully the support is coming soon. Until that it's useless for me 😉

@tbarbette
Copy link

@tbarbette could you try running this python code (preferably inside the virtual enviroment of HomeAssistant)?

from micloud import MiCloud

mc = MiCloud("USERNAME", "PASSWORD")
mc.login()
token = mc.get_token() # to get your cloud service token.
device_list = mc.get_devices(country="de") # get list of devices
print(device_list )

And then see if the gateway is listed in the dict

I get the same list than with the micloud client. Today I tried to hit "reload" and I don't get that message anymore... I only have sensors so I can't verify something else was added up. I think I did not select the right server at the first try, maybe the change of server was not saved? Anyway I guess it works :) Thanks! And yes, please support events :p

@airampg
Copy link

airampg commented Apr 13, 2021

@jonasCr As I see that there is quite some intrest in subdevice support for the Xiaomi Miio Gateway integration, I might see if I can pick up PR #709 to get event driven (sub)devices supported.

Is there anything I can help with or do to get event driven sub-devices supported?
I am more than happy to contribute :)
Thanks!

@starkillerOG
Copy link
Contributor

@airampg thanks for the offer, I will look into this soon.
If I need help with testing i will ask here.

@MilorES
Copy link

MilorES commented May 21, 2021

Can't we put the "subdevices" manually? (if I take them out of the cloud? Any tutorials?)

@starkillerOG
Copy link
Contributor

@MilorES the following python script schould do what you want:

from micloud import MiCloud
from miio import gateway

# connect to the gateway
gateway_device = gateway.Gateway(host, token)
gateway_device.info()

# get the dict of subdevices from the cloud
miio_cloud = MiCloud(cloud_username, cloud_password)
miio_cloud.login()
devices_raw = miio_cloud.get_devices(cloud_country)

# supply the dict of subdevices to the gateway
gateway_device.get_devices_from_dict(devices_raw)

@Artredis
Copy link

Artredis commented Dec 5, 2021

Is there any activity to get these devices also supported?
https://www.home-assistant.io/integrations/xiaomi_miio/#recognized-subdevices-not-yet-implemented
For me lumi.sensor_switch and lumi.sensor_motion would be intresting (maybe lumi.sensor_magnet later.)

I would be happy to help and support but unfortunately I am really new to HA. But I am open to new challenges. :)

@starkillerOG
Copy link
Contributor

@Artredis it is still on my to do list, but just did not have the time to get around to it.
It is a bit difficult since it will require a completly new push mechanism to be implemented in python-miio.

@airampg
Copy link

airampg commented Dec 16, 2021

Hey @starkillerOG, thank you again for your amazing job. I was wondering if you are familiar with the custom integrations that use the MIoT-Spec such as:

I just wanted to add that I have tried both and they are able to detect all my ZigBee devices connected to my lumi.gateway.mieu01 using the cloud connection method.
It creates devices and entities for theZigBee devices:

  • lumi.vibration.aq1 (Vibration sensor Aqara)
  • lumi.sensor_magnet.v2 (Window/Door sensor Mijia)
  • lumi.sensor_motion.v2 (Motion sensor Mijia)
  • lumi.sensor_ht.v1 (Temperature and humidity sensor Mijia)

It detects, but does not create a device or integration for:

  • lumi.sensor_switch.v2 (Button Mijia)
  • lumi.sensor_cube.aqgl01 (Magic Cube Aqara)

Comments:

  • Xiaomi MIoT.
    image
    (I think I need to manually adjust some of the parameters as the integration seems to have some issues (e.g. magnetic sensor's states are reversed), but it does work well with the motion ones, which is a bonus for me!)
    image
  • Xiaomi Miot Auto.
    image
    (This one fixed the issue I mentioned with the magnetic sensors, and seems to be somewhat more popular)

Both of them have a noticeable delay (2 to 4 seconds) when changing the state of the sensors, as expected from the usage of the cloud. However, this is better than having nothing when I want to see if a window is open, or similar.
I hope you can tell me your thoughts and if you think there is any future on using the MIoT-Spec for the officially supported Xiaomi Miio
Again, thank you and if you need any help, just give me a shout :)

@starkillerOG
Copy link
Contributor

@airampg I was aware that the status could be retrieved from the cloud.
I personally did not use the MioT integrations, but I knew they were around.

I think both integrations do not allow for local communication with the zigbee devices right?
Also I don't think there is the posiblity for push updates on events (like the motion sensor) rigth?
Could you confirm that?

Local control and push updates were the main reason I did not look into the MioT integrations (since I think they use cloud).

@MilorES
Copy link

MilorES commented Dec 21, 2021

The other I put the new xiaomi miot auto and asked me if I wanted it local or by cloud ... (so if it might be that I use some for local).
But I can be totally wrong.

@airampg
Copy link

airampg commented Dec 21, 2021

@airampg I was aware that the status could be retrieved from the cloud. I personally did not use the MioT integrations, but I knew they were around.

I think both integrations do not allow for local communication with the zigbee devices right? Also I don't think there is the posiblity for push updates on events (like the motion sensor) rigth? Could you confirm that?

Local control and push updates were the main reason I did not look into the MioT integrations (since I think they use cloud).

Thank you for your reply, and sorry for not replying earlier. The SD card of my HA host started getting corrupted a few weeks back and I have had some intense work to do. I have now ordered an SSD... not happening again!
I will check local control and let you know asap.

@starkillerOG
Copy link
Contributor

@airampg I finally found some time to work on the push server.
I have a PR open now that implements the door/window sensor: #1288

I am still working on the PR, but at least you know that there is some real progress now.
Probably am going to finish this within a couple of weeks now that I have picked it up again.
It is looking good.

@starkillerOG
Copy link
Contributor

@airampg the PR #1288 is finished and ready for revieuw/merge.
Maybe this weekend I will have some time to work on writing the HomeAssistant code to actually add the motion sensors/door sensors/ cubes etc.

@starkillerOG
Copy link
Contributor

The miio PR: #1288
HomeAssistant PR: home-assistant/core#64726
and HomeAssistant docs PR: home-assistant/home-assistant.io#21309

Are all tested and ready for revieuw.

Combined these PRs will add support for the following new devices:

| Door sensor | lumi.sensor_magnet.v2 | MCCGQ01LM
| Door sensor | lumi.sensor_magnet.aq2 | MCCGQ11LM
| Motion sensor | lumi.sensor_motion.v2 | RTCGQ01LM
| Motion sensor | lumi.sensor_motion.aq2 | RTCGQ11LM
| Cube | lumi.sensor_cube.v1 | MFKZQ01LM
| Cube | lumi.sensor_cube.aqgl01 | MFKZQ01LM
| Vibration sensor | lumi.vibration.aq1 | DJT11LM
| Button | lumi.sensor_switch.v2 | WXKG01LM
| Button | lumi.sensor_switch.aq2 | WXKG11LM 2015
| Button | lumi.sensor_switch.aq3 | WXKG12LM
| Button | lumi.remote.b1acn01 | WXKG11LM 2018
| Remote switch single | lumi.sensor_86sw1.v1 | WXKG03LM 2016
| Remote switch single | lumi.remote.b186acn01 | WXKG03LM 2018
| D1 remote switch single | lumi.remote.b186acn02 | WXKG06LM
| Remote switch double | lumi.sensor_86sw2.v1 | WXKG02LM 2016
| Remote switch double | lumi.remote.b286acn01 | WXKG02LM 2018
| D1 remote switch double | lumi.remote.b286acn02 | WXKG07LM

@MilorES
Copy link

MilorES commented Feb 22, 2022

How can we help with the work you are doing? Sending any device? any donation? (I didn't find anything)

Thank You

@starkillerOG
Copy link
Contributor

@MilorES thank you for your generous offer.
Currently I am waiting for @rytilahti to provide feedback on this PR: #1288
Please understand that it is a lot of new code so it is a lot of work to look through it all and @rytilahti needs to find some time to have a good look at it.

Regarding the device/donation.
I do not currently have a way of receiving donations, I will look into it, any sugestion @MilorES.
I am always very pleased if someone sends me some smart devices they do not use anymore to test and program with.
I have received some devices from other companies where I have direct contact with the devolpment teams of the smart products.
Unfortunatly Xiaomi is not really willing to support open-source devolpers like me, I have tried but did not get in contact with xiaomi past the customer support so far.
Specifically I would very much like to get my hands on a Roborock S7 vacuum to start programming with xiaomi vacuums, but no luck so far.

@starkillerOG
Copy link
Contributor

@MilorES I just setup github sponsors for my account: https://github.com/sponsors/starkillerOG/ if you are still intrested.

Regarding the 'lumi.gateway.mieu01' subdevices, I am still waiting on a revieuw from @rytilahti, I will give him a gentile reminder in the PR.

@MilorES
Copy link

MilorES commented Jun 1, 2022

Well, I understand that it is complicated, I will wait patiently.

Can I watch something @rytilahti now?

@tbarbette
Copy link

I'd be happy for the official HA integration using this library to support subdevices!

But for others like me who have been patiently waiting for years now (for good reasons, not criticising here), I think it's worth mentioning that, meanwhile the "xiaomi-miot-auto" integration available through HACS has support for subdevices and more or less everything we discussed here, and beyond.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests