rytilahti / python-miio

Python library & console tool for controlling Xiaomi smart appliances
https://python-miio.readthedocs.io
GNU General Public License v3.0
3.62k stars 547 forks source link

Is the API Now Deprecated or Are the Help Docs Very Wrong or Out of Date? #1717

Open RichieRogers opened 1 year ago

RichieRogers commented 1 year ago

Hi, I've got a couple of Xiaomi G1 vacuums, which I can connect to via the python-miio CLI, but I can't figure out how to use the API. Even a basic bit of code from the docs:

from miio import DeviceFactory

dev = DeviceFactory.create("192.168.x.xxx", "my-token-here")
print(f"Status: {dev.status()}")

Returns the following:

Traceback (most recent call last):
  File "c:\pythontest\xiaomi\test.py", line 2, in <module>
    from miio import DeviceFactory
  File "C:\Users\User\AppData\Roaming\Python\Python311\site-packages\miio\__init__.py", line 12, in <module>
    from miio.device import Device, DeviceStatus  # isort: skip
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Roaming\Python\Python311\site-packages\miio\device.py", line 8, in <module>
    import click
ModuleNotFoundError: No module named 'click'

I've also had a look at https://github.com/rytilahti/python-miio/issues/817, but that seems to be designed for HomeAssistant, so package names/locations don't match up.

UPDATE: Actually persevered and can get the model returned with this:

from miio.device import Device 
dev = Device("192.168.x.xxx", "my-token-here")
print(f"Status: {dev.model}")

Although that is on linux, Windows seems to still generate the errors above. So, probably still need help/advice, but will also keep on prodding the code :)

Thanks, Richie

rytilahti commented 1 year ago

Hi, the repository is currently in flux and the release version differs quite a bit from the current git master, that's probably why you are seeing issues with the naming/locations of integrations. This should not be the problem you are encountering, though. For some reason, your Windows installation does not seem to have click installed, which is causing that error. Have you tried pip install click already?

The code you gave above should work at least in the current git master (devicefactory should return a G1Vacuum instance, if you don't pass force_generic_miot=True which may give different set of functionality), but you could also do this:

from miio import G1Vacuum

dev = G1Vacuum(host, token)
print(dev.status())
RichieRogers commented 1 year ago

Hi, Thanks for replying so quickly. The Windows issue was down to it crapping out on the install with netifaces. So did pip install python-miio --no-deps This, obviously, didn't install the dependencies. So, taking a guess I did pip install click. This installed and then threw a warning about a load of other dependencies, which I installed: pip install android_backup pip install appdirs pip install attrs pip install construct pip install croniter pip install cryptography pip install defusedxml pip install micloud pip install PyYAML pip install tqdm pip install zeroconf

Now my test works on Windows (my test machine)

Your code that you suggested above works: <G1Status battery=100 charge_state=G1ChargeState.FullyCharged clean_area=0 clean_time=0:00:00 error=No error error_code=0 fan_speed=G1FanSpeed.Standard filter_life_level=68 filter_time_left=4 days, 6:05:00 main_brush_life_level=84 main_brush_time_left=10 days, 12:05:00 mop_state=G1MopState.Off operating_mode=G1VacuumMode.GlobalClean side_brush_life_level=63 side_brush_time_left=3 days, 23:18:00 state=G1State.Charging water_level=G1WaterLevel.Level2>

I can now start playing around with sending commands (well, in the morning, it can wait now). Thanks for your help, Richie

RichieRogers commented 1 year ago

Hi, So, I can now connect to my vacs. One, hopefully last, thing. What's the API way of doing "miiocli cloud list" and how would I pass in username and password? Thanks, Richie

rytilahti commented 1 year ago

Hi,

here's the doc for the cloud interface, something like this would work:

from miio import CloudInterface
cloud = CloudInterface(username, password)
devs = cloud.get_devices("us")

The devs will contain a dictionary of information from the cloud, ip and token are probably the most interesting ones that you can pass to the factory or directly to your device constructors.

This reminded me, that it might be a good idea to extend the interface to allow constructing devices directly from those info objects to simplify creation :-)

RichieRogers commented 1 year ago

Hi, Cheers, that's worked great. Many thanks for your help, Richie

rytilahti commented 1 year ago

Sure thing! It's always interesting to hear that the lib is used also outside the homeassistant ecosystem :-)