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 Xiaomi Air purifier 3C #899

Merged
merged 11 commits into from
Feb 7, 2021
Merged

Add support for Xiaomi Air purifier 3C #899

merged 11 commits into from
Feb 7, 2021

Conversation

arturdobo
Copy link
Contributor

This PR adds support for Air purifier 3C (zhimi.airpurifier.mb4) which according to specification provides only a subset of i.e. mb3 functionalities.

@pawelnguyen
Copy link

I bought a 3C and just found out it doesn't work with home assistant. LMK if I can help and test this on a real device.

Looking at the code - feature set seems correct, I can confirm that this model doesn't provide temperature/humidity.

Thanks for working on this!

Copy link
Owner

@rytilahti rytilahti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks straightforward to me, thanks for the PR!

A couple of minor issues should be changed and then this is ready to be merged.

miio/airpurifier_miot.py Outdated Show resolved Hide resolved
miio/airpurifier_miot.py Outdated Show resolved Hide resolved
@syssi
Copy link
Collaborator

syssi commented Jan 20, 2021

Cool! Let's merge this PR as soon the minor issues are fixed.

@jagub2
Copy link

jagub2 commented Jan 27, 2021

Howdy, I tried to use these changes, but I'm struggling to get it working.

 ~/python-miio/miio   add-mb4-support ± python3 cli.py airpurifiermb4 --ip x --token y info
Model: zhimi.airpurifier.mb4
Hardware version: esp32
Firmware version: 2.0.8
 ~/python-miio/miio   add-mb4-support ± python3 cli.py airpurifiermb4 --ip x --token y status
Traceback (most recent call last):
  File "/home/jagub2/python-miio/miio/cli.py", line 51, in <module>
    create_cli()
  File "/home/jagub2/python-miio/miio/cli.py", line 47, in create_cli
    return cli(auto_envvar_prefix="MIIO")
  File "/home/jagub2/python-miio/miio/../miio/click_common.py", line 59, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/jagub2/python-miio/miio/../miio/click_common.py", line 285, in wrap
    result_msg = result_msg_fmt.format(**kwargs)
AttributeError: 'AirPurifierMB4Status' object has no attribute 'favorite_rpm'
 ✘  ~/python-miio/miio   add-mb4-support ± python3 cli.py airpurifiermb4 --ip x --token y set_favorite_rpm 300
Setting favorite motor speed '300' rpm
[{'did': 'favorite_rpm', 'siid': 9, 'piid': 3, 'code': 0}]
 ~/python-miio/miio   add-mb4-support ± python3 cli.py airpurifiermb4 --ip x --token y status
Traceback (most recent call last):
  File "/home/jagub2/python-miio/miio/cli.py", line 51, in <module>
    create_cli()
  File "/home/jagub2/python-miio/miio/cli.py", line 47, in create_cli
    return cli(auto_envvar_prefix="MIIO")
  File "/home/jagub2/python-miio/miio/../miio/click_common.py", line 59, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/jagub2/python-miio/miio/../miio/click_common.py", line 285, in wrap
    result_msg = result_msg_fmt.format(**kwargs)
AttributeError: 'AirPurifierMB4Status' object has no attribute 'favorite_rpm'

set_favorite_rpm makes it beep but it still fails on status command. All other set_ commands work, too besides set_led_brightness_level, for that one I get:

Traceback (most recent call last):
  File "/home/jagub2/python-miio/miio/cli.py", line 49, in <module>
    create_cli()
  File "/home/jagub2/python-miio/miio/cli.py", line 45, in create_cli
    return cli(auto_envvar_prefix="MIIO")
  File "/home/jagub2/python-miio/miio/click_common.py", line 59, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/jagub2/python-miio/miio/click_common.py", line 277, in wrap
    msg = msg_fmt.format(**kwargs)
KeyError: 'brightness'

When I hardcode favorite_rpm in default_output in AirPurifierMB4's __init__, status command starts working:

Power: on
AQI: 4 μg/m³
Mode: OperationMode.Favorite
LED brightness level: 3
Buzzer: False
Child lock: True
Filter life remaining: 99 %
Filter hours used: 29
Motor speed: 300 rpm
Favorite RPM: 300 rpm

I checked out that project and exported PYTHONPATH to use this module without installing.

Am I doing something wrong? Am I missing something? Thanks in advance.

@arturdobo
Copy link
Contributor Author

@jagub2 I'm going to apply the suggestions above tomorrow, so I'll check what's wrong then. I tested it using HA but maybe omitted something on the CLI 🤔 Thanks for letting me know

@arturdobo
Copy link
Contributor Author

@jagub2 could you pull changes, install and check everything is ok now?

@jagub2
Copy link

jagub2 commented Jan 28, 2021

@arturdobo I confirm, everything works great now. Thank you!

@mbatorowicz
Copy link

mbatorowicz commented Feb 7, 2021

Hello, I also have the 3C version. Can you please explain step by step what I have to do to make it work with HA. I mean the modification of a component. I have the token out.

Copy link
Owner

@rytilahti rytilahti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of minor issues to be fixed, then this is ready to go! 👍 Please also update the supported devices listing in README!

miio/airpurifier_miot.py Outdated Show resolved Hide resolved
miio/airpurifier_miot.py Outdated Show resolved Hide resolved
miio/airpurifier_miot.py Outdated Show resolved Hide resolved
@arturdobo
Copy link
Contributor Author

@rytilahti all suggestions have been applied. Regarding README how to add information about the new air purifier? As a new entry in the list or somehow different? I see there is a generic entry Xiaomi Mi Air Purifier, but no idea how many models it covers

@rytilahti
Copy link
Owner

@arturdobo Maybe just adding (3C, ..) to the end of the entry, I think we want to have a yaml-based registry of supported devices & their model information at some point, but until then it's manual updates to the readme :(

@arturdobo
Copy link
Contributor Author

@mbatorowicz I'm definitively not a python developer so I did it a hacky way at the beginning of development by replacing the class in the python-miio lib. But once this PR is merged you should be able to just update the library version and test.

@rytilahti I added a whole list of air purifiers I use and I'm sure python-miio supports them.

@rytilahti
Copy link
Owner

LGTM, let's get it merged. Thanks again! 🎉

@rytilahti I added a whole list of air purifiers I use and I'm sure python-miio supports them.

Great! Oh, you have multiple purifiers? Could you create a separate PR to add a list of model identifiers to the docstrings of the respective implementation classes?

@rytilahti rytilahti added the miot label Feb 7, 2021
@rytilahti rytilahti merged commit 91ecc98 into rytilahti:master Feb 7, 2021
@arturdobo
Copy link
Contributor Author

Yeah! One air purifier per room, but I wanted to replace the old 2 model with 3C model so temporarily I have one more than needed 😄

Sure, I'll add those models to appropriate docstrings

@gh0st125
Copy link

gh0st125 commented Feb 12, 2021

I am still not able to integrate my Air Purifier 3C into Home Assistant. The Entity always states "not available"

image

This is what i have in my configuration.yaml:

`fan:

  • platform: xiaomi_miio
    host: !secret xiaomi_ip
    token: !secret xiaomi_token
    name: "Luftreiniger"`

what am I missing?

@arturdobo
Copy link
Contributor Author

@gh0st125 is it your only change in HA? If yes, it won't work like that. More details here

@gh0st125
Copy link

gh0st125 commented Feb 12, 2021

yeah, these are the only changes so far.

Feel free to provide a pull request for this custom component as first step to support your device. I'm trying to refactor the component to be merged as official HA component again some day.

@arturdobo So I have to do a pull request? How do I do that? Sorry, I'm not really familiar with this kind of integrations...

@arturdobo
Copy link
Contributor Author

arturdobo commented Feb 12, 2021

Once @rytilahti releases a new version of python-miio the device can be added to HA. The way new Xiaomi devices were added so far is not allowed anymore and the new devices need to be added the new way meaning a device needs to be split into multiple entities, one per measurement (i.e. humidity, air quality, etc). Any PR that adds devices (or even features to already added devices) the old way are immediately closed in HA 😉

@gh0st125
Copy link

ahh okay thanks, so I have to wait for a new release. That's fine for me, it's not something that I have to have right now :D Thanks for the clarification!

@arturdobo
Copy link
Contributor Author

I'll try to prepare some PR in HA and see how it will work 😉

@calas80
Copy link

calas80 commented Feb 17, 2021

I have to wait for a new release. Thanks

@starkillerOG
Copy link
Contributor

@arturdobo I am currently working on implementing Config Flow for all Miio platforms.
Switches are done, Vacuums almost done (only needs to be merged).
Lights are up next, but I could also do the fan platform (Air Purifier and Humidifier) first if you like.

Probably you will want to wait untill the Config Flow is done before implementing this new device, but of course it is up to you.

@arturdobo
Copy link
Contributor Author

it'd be great if you could.

@starkillerOG
Copy link
Contributor

@arturdobo just made the PR: home-assistant/core#46866

@abrilevskiy
Copy link

Could you please clarify - can zhimi.airpurifier.mb4 be added to HomeAssistant? I tried with the latest Dev without success

@starkillerOG
Copy link
Contributor

@abrilevskiy the config flow PR is merged, but the zhimi.airpurifier.mb4 has not been implemented.
However now that the config flow PR is merged, someone could proceed with making a PR for the zhimi.airpurifier.mb4.

@arturdobo
Copy link
Contributor Author

arturdobo commented Mar 6, 2021

@rytilahti could you please release the new python-miio version? It'd allow adding new devices to HA then

@jddipqd
Copy link

jddipqd commented Apr 12, 2021

Is 3C supported now?
I'm running 0.5.5.2 but still can't get it to work.

miiocli airpurifier --ip xxx --token xxx info

Model: zhimi.airpurifier.mb4
Hardware version: esp32
Firmware version: 2.0.8
miiocli airpurifier --ip xxx --token xxx status

Error: {'code': -5000, 'message': 'method not found'}

The same command is working for 2H.

@rytilahti
Copy link
Owner

I think it should be supported as per this PR? Feel free to open a separate issue if that's not the case.

@arturdobo
Copy link
Contributor Author

The argument you provide is wrong, airpurifiermb4 should be instead

@jddipqd
Copy link

jddipqd commented Apr 12, 2021

Thank you, it's working with airpurifiermb4.

Repository owner locked as resolved and limited conversation to collaborators Apr 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.