-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
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
Implement config flow in the Broadlink integration #36914
Conversation
Hey there @Danielhiversen, mind taking a look at this pull request as its been labeled with a integration ( |
This PR is a great improvement. I had to work on many things which could not be done separately without causing pain to the users. So the PR ended up getting really big. But don't panic: many of these lines are just messages for the config flow 😄 I performed local tests and everything is working 🚀 @balloob @MartinHjelmare @Danielhiversen Do you have time to review? |
@felipediel Please, don't ping/mention for reviews to random people. That is not appreciated, instead have some patience while we working on the currently 400 open PRs in the organization. |
@frenck Thanks for the advice. I understand this and I am not pushing. Those guys are not random. I chose to torture them because they are familiar with this integration and I appreciate their reviews. If I were you, I wouldn't stare at this code for long. You can be the next 😄 @MartinHjelmare Thanks for helping me with this dinossaur. Tonight I will start working on the updates. |
@MartinHjelmare Do you think this is a good idea? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we ready?
Yes. |
) * Implement config flow in the Broadlink integration * General improvements to the Broadlink config flow * Remove unnecessary else after return * Fix translations * Rename device to device_entry * Add tests for the config flow * Improve docstrings * Test we do not accept more than one config entry per device * Improve helpers * Allow empty packets * Allow multiple config files for switches related to the same device * Rename mock_device to mock_api * General improvements * Make new attempts before marking the device as unavailable * Let the name be the template for the entity_id * Handle OSError * Test network unavailable in the configuration flow * Rename lock attribute * Update manifest.json * Import devices from platforms * Test import flow * Add deprecation warnings * General improvements * Rename deprecate to discontinue * Test device setup * Add type attribute to mock api * Test we handle an update failure at startup * Remove BroadlinkDevice from tests * Remove device.py from .coveragerc * Add tests for the config flow * Add tests for the device * Test device registry and update listener * Test MAC address validation * Add tests for the device * Extract domains and types to a helper function * Do not patch integration details * Add tests for the device * Set device classes where appropriate * Set an appropriate connection class * Do not set device class for custom switches * Fix tests and improve code readability * Use RM4 to test authentication errors * Handle BroadlinkException in the authentication
) * Implement config flow in the Broadlink integration * General improvements to the Broadlink config flow * Remove unnecessary else after return * Fix translations * Rename device to device_entry * Add tests for the config flow * Improve docstrings * Test we do not accept more than one config entry per device * Improve helpers * Allow empty packets * Allow multiple config files for switches related to the same device * Rename mock_device to mock_api * General improvements * Make new attempts before marking the device as unavailable * Let the name be the template for the entity_id * Handle OSError * Test network unavailable in the configuration flow * Rename lock attribute * Update manifest.json * Import devices from platforms * Test import flow * Add deprecation warnings * General improvements * Rename deprecate to discontinue * Test device setup * Add type attribute to mock api * Test we handle an update failure at startup * Remove BroadlinkDevice from tests * Remove device.py from .coveragerc * Add tests for the config flow * Add tests for the device * Test device registry and update listener * Test MAC address validation * Add tests for the device * Extract domains and types to a helper function * Do not patch integration details * Add tests for the device * Set device classes where appropriate * Set an appropriate connection class * Do not set device class for custom switches * Fix tests and improve code readability * Use RM4 to test authentication errors * Handle BroadlinkException in the authentication
Please open a terminal on your computer, install the python-broadlink library ( import broadlink as blk
devs = blk.discover(timeout=5)
print([(d.host[0], hex(d.devtype)) for d in devs])
devs = blk.discover(discover_ip_address="192.168.0.17", timeout=5) # Your device IP address
print([(d.host[0], hex(d.devtype)) for d in devs]) What is the output? |
Use your computer. I suggest you to create a venv. python3 -m venv venv
source venv/bin/activate
pip3 install broadlink Now you can type |
Returns empty brackets "[]" |
Even if you try the second option? Are you using a VPN? |
[('192.168.1.48', '0x7540')] |
Oh gosh, are you sure? What is your device type? Please check if the entity_id is correct first. |
Actually it does not find it at all. The device is RM PRO. The script sees it anyway. device type '0x279d'. the config is under switch domain:
|
Should I manually add the existing device and re-learn all of my commands(IR/RF)? |
You don't need to relearn the commands. Did you add the device via config flow? Did you check the MAC address? |
After re-configure via Integrations, it is working. Sorry for bothering you. Do you know when the MP2 change will be released? in next patch or later? I am asking because it is connected to a lot of automations I have. :( |
I have no control over releases. I can help you to workaround:
ssh USERNAME@HOST
docker exec -it homeassistant /bin/bash
cd /usr/local/lib/python3.8/site-packages/broadlink Now you are in the python-broadlink library folder. You can manually add your type to the file |
I am using HASSIO but not docker installation. |
Please open an issue if you suspect a bug. If you need help please use our help channels: Merged PRs should not be used for support or bug reports. Thanks! |
Breaking change
1. Devices are now configured via configuration flow
To set up a Broadlink device, click Configuration in the sidebar and click Integrations.
The devices will be imported from your configuration files to that page. If you see your device there, click Configure. If not, click the + icon in the lower right, click Broadlink, enter the host and follow the instructions to complete the setup.
The name you choose will serve as a template for the entities. You can change the entity name and id in the entity settings on the frontend. You may need to change some names or ids to make everything look the same as it was before this update.
2. Discontinue
broadlink.learn
andbroadlink.send
servicesremote.learn_command
andremote.send_command
are now registered automatically. Now you can useremote.send_command
to send base64 codes.instead of
broadlink.learn
use
remote.learn_command
instead of
broadlink.send
use
remote.send_command
keep it clean
3. Discontinue all platforms, except switch
Entities are now registered automatically. The only exception is the switch platform, which continues to exist for RM switches. The config schema has changed. The host and type are no longer required and the name serves as a template for the entity id.
instead of:
use this:
The above example creates
switch.sony_tv
andswitch.lg_tv
to be controlled using the device with the MAC address 34:ea:34:b4:5d:2c. This device needs to be configured first via configuration flow.When you finish configuring the devices you can delete all your Broadlink configuration files except the RM switches. These switches are the only platform that still exists in yaml. They won't be imported. If you delete the file, they are gone.
The problem
Many users are having trouble configuring Broadlink devices. Broadlink recently launched an RM mini 3 - the infamous 0x5f36 - that is identifical to the others, but communicates differently, and the worse: it has a lock. I recently added support for these devices, but since we don't have device discovery, the user needs to specify the device type, so that the library can control it correctly. And if the device is locked, he needs to factory reset the device to unlock it.
But how can the user find out the device type? And how can he find out that the device is locked for authentication and needs a factory reset? The short answer is "read the docs", but the right answer is "he shouldn't have to do that".
We shouldn't be getting the device type from the user. The device knows its type and sends it in response when we say "hello". But we are skipping this step and going straight to authentication.
If we said hello, we could obtain valuable information in response, such as name, type, class, MAC address, model, manufacturer and lock. We are demanding a lot of these information from the user. He should not have to worry about these technical details.
Things could be simpler. We are breaking etiquette and making they pay for it. This PR comes to fix this.
Proposed change
This PR implements a config flow in the Broadlink integration.
In this flow, we say "hello" to the device and we create a config entry with the information we extract from the response.
The user no longer needs to provide the device type and MAC address.
The user no longer needs to set up platforms. We forward entry setup to related domains according to the device type.
The user gets notified when the device is locked, and a config flow is created to assist with unlocking.
Information such as name, model, manufacturer and firmware version are now available on the frontend.
Now the device is a singleton shared by the entities. This will improve communication as we will only have one connection per device.
Devices and entities are registered and associated. The entities are renamed when the device is renamed, updated when the device is updated, and removed when the device is removed. Everything happens in a coordinated and harmonious way.
Type of change
Additional information
Checklist
black --fast homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.The integration reached or maintains the following Integration Quality Scale: