Skip to content

Add support for Broadlink BG1 devices#36449

Closed
b4dpxl wants to merge 1 commit intohome-assistant:devfrom
b4dpxl:broadlinkbg1
Closed

Add support for Broadlink BG1 devices#36449
b4dpxl wants to merge 1 commit intohome-assistant:devfrom
b4dpxl:broadlinkbg1

Conversation

@b4dpxl
Copy link
Copy Markdown
Contributor

@b4dpxl b4dpxl commented Jun 4, 2020

Proposed change

This adds support for the British General Broadlink BG1 switches. Replaces PR #33399

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example entry for configuration.yaml:

# Example configuration.yaml
switch:
- platform: custom_broadlink
  host: 192.168.0.123
  mac: "24:df:a7:aa:bb:cc"
  type: bg1
  friendly_name: "Hall socket"
  timeout: 2

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

@b4dpxl
Copy link
Copy Markdown
Contributor Author

b4dpxl commented Jun 4, 2020

@felipediel this replaces #33399, after I managed to completely bork my dev branch :|

@b4dpxl b4dpxl mentioned this pull request Jun 4, 2020
20 tasks
@Goobiegoobers
Copy link
Copy Markdown

The code above works great for a single socket but have you tried with two sockets? Installing a second socket on the BG app shows as a sub device and doesn’t appear to have an IP address. It connected instantly to Alexa so there must be a way but I’m not clever enough to figure it out.

@b4dpxl
Copy link
Copy Markdown
Contributor Author

b4dpxl commented Jun 5, 2020

The code above works great for a single socket but have you tried with two sockets? Installing a second socket on the BG app shows as a sub device and doesn’t appear to have an IP address. It connected instantly to Alexa so there must be a way but I’m not clever enough to figure it out.

@Goobiegoobers No, as I only have a single device. Does it not show up in your router's DHCP logs?

@Goobiegoobers
Copy link
Copy Markdown

Goobiegoobers commented Jun 5, 2020

No it’s not there, and on the app it shows the mac but says IP unavailable, looks like it talks through the “main” device somehow.
I will try and power cycle it in a bit to see if it appears

@Goobiegoobers
Copy link
Copy Markdown

Goobiegoobers commented Jun 6, 2020

After further investigation I’ve found a second device definitely does not have its on IP address, it connects through what ever device is set up first. Quite a nice idea from BG to save on router devices I think.

I tried syncing one and then disconnecting it to sync the other but even with the “master” unplugged it still creates the 2nd as a “sub device”.

@b4dpxl
Copy link
Copy Markdown
Contributor Author

b4dpxl commented Jun 6, 2020

After further investigation I’ve found a second device definitely does not have its on IP address, it connects through what ever device is set up first. Quite a nice idea from BG to save on router devices I think.

I tried syncing one and then disconnecting it to sync the other but even with the “master” unplugged it still creates the 2nd as a “sub device”.

Unless these are doing something really odd, it must be connecting to your wireless network? There's nothing in the broadlink library I can see about connecting without it. Have you tried doing a discovery with the library to see what happens?

import broadlink
devices = broadlink.discover(timeout=5)
print(devices)

@felipediel
Copy link
Copy Markdown
Contributor

It looks like they're creating a mesh network. If confirmed, things will start to get complicated.

@b4dpxl
Copy link
Copy Markdown
Contributor Author

b4dpxl commented Jun 6, 2020

It looks like they're creating a mesh network. If confirmed, things will start to get complicated.

If so, you should be able to see the network with a wifi scanner.

@Goobiegoobers
Copy link
Copy Markdown

Goobiegoobers commented Jun 6, 2020

There must be a way to find it, as soon as it connected in the BG app I had a notification from Alexa saying device found.
@b4dpxl - apologies for my ignorance, where do I put the import broadlink code?

@felipediel
Copy link
Copy Markdown
Contributor

It looks like they're creating a mesh network. If confirmed, things will start to get complicated.

If so, you should be able to see the network with a wifi scanner.

Yes. Sorry, I don't have the devices to test.

@felipediel
Copy link
Copy Markdown
Contributor

@Goobiegoobers Type python3 in the command line.

@b4dpxl
Copy link
Copy Markdown
Contributor Author

b4dpxl commented Jun 6, 2020

It looks like they're creating a mesh network. If confirmed, things will start to get complicated.

If so, you should be able to see the network with a wifi scanner.

Yes. Sorry, I don't have the devices to test.

I only have one, and I don't see any evidence of a hidden network, though I've not done a thorough scan yet; I'll dig out one of my decent wireless cards at some point and have a scan.

@Goobiegoobers
Copy link
Copy Markdown

I’m running HASS.OS so don’t have a command line. Is there another way to do it? Like I said I have two here plugged in and am happy to be a guinea pig to find the solution.

@Goobiegoobers
Copy link
Copy Markdown

Goobiegoobers commented Jun 7, 2020

Ughh mine don’t work at all now haha. I’ve reset everything to as it was and I can’t even get one working on it’s own now 😞. Is it because I updated the firmware to v57053?
They are connected and working on everything except HA

@Snakesms
Copy link
Copy Markdown

Snakesms commented Jun 7, 2020

The code above works great for a single socket but have you tried with two sockets? Installing a second socket on the BG app shows as a sub device and doesn’t appear to have an IP address. It connected instantly to Alexa so there must be a way but I’m not clever enough to figure it out.

It might be that you can add 2nd socket as a sub devise for the 1st one, but its definitely not the default behaviour.

When I've added 2nd socket it's appeared as a 1st one exactly the same as a primary one in the app.
IP address is not visible in BG app for any devise in my case (1st or 2nd), but both easily searchable by MAC in the router.

Regarding this particular PR, I'm going to test it once figured out how to :)

@Goobiegoobers
Copy link
Copy Markdown

Goobiegoobers commented Jun 7, 2020

The code above works great for a single socket but have you tried with two sockets? Installing a second socket on the BG app shows as a sub device and doesn’t appear to have an IP address. It connected instantly to Alexa so there must be a way but I’m not clever enough to figure it out.

It might be that you can add 2nd socket as a sub devise for the 1st one, but its definitely not the default behaviour.
When I've added 2nd socket it's appeared as a 1st one exactly the same as a primary one in the app.
IP address is not visible in BG app for any devise in my case (1st or 2nd), but both easily searchable by MAC in the router.
Regarding this particular PR, I'm going to test it once figured out how to :)

Snakesms, what firmware do you have running on your sockets? im on v57053 on ios and it does not give me a choice when adding a 2nd socket, it automatically sets it as a sub device.
Mine work fine away from HA but I’ve completely lost comms from HA now, I completely started again on BG and HA custom_broadlink but it still doesn’t work. so frustrated 😂.
I’ve restored HA to a version from the other day where the socket definitely worked. So it must be the new socket firmware

@Snakesms
Copy link
Copy Markdown

Snakesms commented Jun 7, 2020

Ughh mine don’t work at all now haha. I’ve reset everything to as it was and I can’t even get one working on it’s own now 😞. Is it because I updated the firmware to v57053?
They are connected and working on everything except HA

I'm thinking there is something "wrong" in the latest Firmware, as one of my sockets used to work with the old firmware and become unavailable with the new one. I've got another socket with old firmware and avoiding firmware upgrade. Going to test if it works with old firmware or broken too

New one is: v57053
Old one is: v46042
And to be clear - I'm on Android not IOS so might be a difference between apps

@Goobiegoobers
Copy link
Copy Markdown

Keep me posted!

@Snakesms
Copy link
Copy Markdown

Snakesms commented Jun 7, 2020

It looks like they're creating a mesh network. If confirmed, things will start to get complicated.

If so, you should be able to see the network with a wifi scanner.

Yes. Sorry, I don't have the devices to test.

I only have one, and I don't see any evidence of a hidden network, though I've not done a thorough scan yet; I'll dig out one of my decent wireless cards at some point and have a scan.

I did try to update 2 files preset in this PR in my running 110.5 version of HomeAssistant.
Result in the log is constantly:
"Platform broadlink not ready yet. Retrying in 60 seconds"

Respective config is switch section is:

  • platform: broadlink
    host: 192.168.X.X
    mac: "c8:f7:42:XX:XX:XX"
    type: bg1
    friendly_name: "Test"
    timeout: 2

Not sure if I'm missing anything and if it's possible to get some debugging info in the stable version of HomeAssistant to get more data or not

@b4dpxl
Copy link
Copy Markdown
Contributor Author

b4dpxl commented Jun 7, 2020

Not sure if I'm missing anything and if it's possible to get some debugging info in the stable version of HomeAssistant to get more data or not

You can change the debug level in the configuration.yaml:

logger:
  logs:
    homeassistant.components.broadlink: debug

@Goobiegoobers
Copy link
Copy Markdown

@Snakesms that config was working for me (before last night), does it not work for your socket with the earlier firmware either?

@felipediel
Copy link
Copy Markdown
Contributor

I figured out what is going on. Check this out.

@Snakesms
Copy link
Copy Markdown

Snakesms commented Jun 7, 2020

Not sure if I'm missing anything and if it's possible to get some debugging info in the stable version of HomeAssistant to get more data or not

You can change the debug level in the configuration.yaml:

logger:
  logs:
    homeassistant.components.broadlink: debug

Thanks, I'll try to update and catch something useful

@Snakesms
Copy link
Copy Markdown

Snakesms commented Jun 7, 2020

@Snakesms that config was working for me (before last night), does it not work for your socket with the earlier firmware either?

Nope it doesn't.
"Test" socket is the one with old firmware.

@Goobiegoobers
Copy link
Copy Markdown

Can you ping the second socket?

@g189ffin
Copy link
Copy Markdown

g189ffin commented Aug 8, 2020

Yea I am able to ping to it but I think I'm having that issue that you had @Goobiegoobers with authentication failing in python. How did you fix it?

I used AP mode to connect it to the network otherwise the smart setup just takes over and creates the new device as a sub.

@Goobiegoobers
Copy link
Copy Markdown

Message from Felipedel solved it for me (2 months ago)

#36449 (comment)

@g189ffin
Copy link
Copy Markdown

g189ffin commented Aug 8, 2020

I'm in guys!! I used the broadlink.setup(ssid, password, mode) to set up my device

https://github.com/mjg59/python-broadlink/blob/master/README.md#example-use

By using that I was able to connect my device to the network. The issue I was facing was that if:

  • I used the BG Home app in smart mode, it sets the second socket as a sub
  • I used the BG Home app in AP mode, when I run devices[0].auth() I get False. This works with the app very well but not with Home Assistant

When I finally used the python library to set it up, it was the barebones I needed it to do i.e. connect it to the network and when i ran devices[0].auth, I got True and now its connected on Home Assistant but not in the app. It is also not controllable through google home unless you link Home assistant as your integration into Google Home which is fine for my use.

@Snakesms
Copy link
Copy Markdown

Snakesms commented Aug 8, 2020

Hi g189ffin,
Based on my experience, there is no way to add socket to both: Ha & App.
So you have to chose what you prefer.
The best way probably would be to add to HA and export it to Google Home later as HA integration and not directly from the BG app

Regards

@dodavies
Copy link
Copy Markdown

how can I enable this functionality for my 822/HC-01 double uk socket.
https://www.bgelectrical.uk/wiring-devices/moulded/smart-sockets#822/HC-01
the device shows in the BG app after a fight and in my config I have

switch:

  • platform: broadlink
    host: 192.168.1.231
    mac: '24:df:a7:a4:da:19'
    type: bg1
    friendly_name: "Living Room Socket"
    timeout: 2

I am using hassio on a rpi two do I enable this custom_broadlink?
is there any plan to just enable it per the normal Broadlink integration ?

@mebrew17
Copy link
Copy Markdown

Hi I am a HA noobie. I have followed eugene eschava's broadlink mqtt scheme but have only got to : " root@UbuntuDesktop:/home/mebrew/broadlink/broadlink-mqtt# python3 mqtt.py
[2020-08-25 16:24:18,023] DEBUG Connected to 'BG1' Broadlink device at '192.168.1.107' (MAC 13:4b:97:42:f7:c8) and started listening for commands at MQTT topic having prefix 'broadlink/'
[2020-08-25 16:24:18,555] DEBUG Connected to MQTT broker, subscribing to topic broadlink/# "
but can get no further.
Any tips on how to proceed and configure into Home Assisatnt.

thank you

@felipediel
Copy link
Copy Markdown
Contributor

felipediel commented Aug 25, 2020

@b4dpxl I think we are ready for this update. Please rebase it --onto dev branch so I can help you.

How to rebase:

  1. Go to your local branch, e.g:
    cd Projects/home-assistant
    git checkout broadlinkbg1

  2. If you don't have an upstream yet:
    git remote add upstream https://github.com/home-assistant/core.git

  3. Fetch upstream:
    git fetch upstream

  4. Check your changes. How many commits?
    git log

  5. Rebase
    git rebase --onto upstream/dev HEAD~NUMBER_OF_COMMITS

  6. Open the folder in VS code and solve conflicts.
    Use git add . and git rebase --continue to continue.

  7. Add BG1 to:

  1. Implement a BroadlinkUpdateManager to handle updates.

  2. Test your changes locally.
    source venv/bin/activate
    hass -c config

  3. Test if local tests pass:
    pytest tests/components/broadlink

  4. Commit your changes and force push to origin.
    git add .
    git commit -m 'Add support for BG1 devices'
    git push origin broadlinkbg1 --force

Ping me when you are done.

@b4dpxl
Copy link
Copy Markdown
Contributor Author

b4dpxl commented Aug 26, 2020

@b4dpxl I think we are ready for this update. Please rebase it --onto dev branch so I can help you.

Many thanks for the detailed instructions @felipediel - It's going to be a couple of weeks before I can look at this, but I'll get it in ASAP.

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Sep 4, 2020

I'm going to mark this as a draft since its going to be a few weeks before the author can come back to this. Please mark this ready for review once its ready for review.

@bdraco bdraco marked this pull request as draft September 4, 2020 14:23
@dodavies
Copy link
Copy Markdown

dodavies commented Sep 8, 2020

Does this then mean it will appear in the beta within hass io when the author carries out the requested tasks and then eventually in the non beta ?

@fryc88
Copy link
Copy Markdown

fryc88 commented Sep 21, 2020

Good. I'm really waiting for it and keeping my fingers crossed to get it working! That's really great job - i think it's best quality of the socket for their price.

@b4dpxl
Copy link
Copy Markdown
Contributor Author

b4dpxl commented Sep 26, 2020

@b4dpxl I think we are ready for this update. Please rebase it --onto dev branch so I can help you.

How to rebase:

  1. Go to your local branch, e.g:
    cd Projects/home-assistant
    git checkout broadlinkbg1
  2. If you don't have an upstream yet:
    git remote add upstream https://github.com/home-assistant/core.git
  3. Fetch upstream:
    git fetch upstream
  4. Check your changes. How many commits?
    git log
  5. Rebase
    git rebase --onto upstream/dev HEAD~NUMBER_OF_COMMITS
  6. Open the folder in VS code and solve conflicts.
    Use git add . and git rebase --continue to continue.
  7. Add BG1 to:
  1. Implement a BroadlinkUpdateManager to handle updates.
  2. Test your changes locally.
    source venv/bin/activate
    hass -c config
  3. Test if local tests pass:
    pytest tests/components/broadlink
  4. Commit your changes and force push to origin.
    git add .
    git commit -m 'Add support for BG1 devices'
    git push origin broadlinkbg1 --force

Ping me when you are done.

Hi @felipediel. git rebase ... HEAD~27280 gave an error about "invalid upstream", although get rebase ... <last_commit_id_from_git_log> worked. But what am I supposed to do here?

        added by them:   app/DeviceTracker.py
        deleted by us:   app/EventBus.py
        deleted by us:   app/actor/HueTrigger.py
        deleted by us:   app/observer/TomatoDeviceScanner.py
        deleted by us:   app/util.py
        deleted by us:   start.py

what do I git add, and what do I git rm?

Thanks

@felipediel
Copy link
Copy Markdown
Contributor

You need to use HEAD~NUMBER_OF_COMMITS. Let's say your only commit is "Added support for BG1 devices". So you git rebase --onto upstream/dev HEAD~1. I suggest you to git log before to check the commits.

@b4dpxl
Copy link
Copy Markdown
Contributor Author

b4dpxl commented Sep 26, 2020

what am I actually looking for in git log? I've only ever used git for my personal projects, with just me working on it.

After a git fetch upstream there are lots of commits in git log, some older and some newer than mine (which isn't in the log). How do I determine NUMBER_OF_COMMITS?

@felipediel
Copy link
Copy Markdown
Contributor

It may be easier for you to copy the bg1 class to an updated branch and start over.

@dodavies
Copy link
Copy Markdown

how does this get resolved, I'm super keen to have this in beta and stable?

@dodavies
Copy link
Copy Markdown

still keen to understand how this gets resolved as this is a key integration in the UK

@b4dpxl
Copy link
Copy Markdown
Contributor Author

b4dpxl commented Oct 12, 2020

still keen to understand how this gets resolved as this is a key integration in the UK

I need time to rework it given the changes to the core module. However that’s not likely to be any time soon due to personal circumstances. If someone else wants to have a go it should be relatively straight forward.

@mebrew17
Copy link
Copy Markdown

mebrew17 commented Oct 12, 2020 via email

@mebrew17
Copy link
Copy Markdown

mebrew17 commented Oct 12, 2020 via email

@dodavies
Copy link
Copy Markdown

If someone wants to give me steps to get this past the post I'm happy to do so as from what I can tell its a matter of git stuff not code stuff ?

@b4dpxl
Copy link
Copy Markdown
Contributor Author

b4dpxl commented Oct 23, 2020

If someone wants to give me steps to get this past the post I'm happy to do so as from what I can tell its a matter of git stuff not code stuff ?

It needed code changes. I should be in a position to push a new PR over the weekend.

@dodavies
Copy link
Copy Markdown

That is excellent news, with a colleague we tried but got as far as Implement a BroadlinkUpdateManager to handle updates.
But now I am lost with no code skill.
I look forward to your PR and thanks again for doing it !

@JimZAH
Copy link
Copy Markdown

JimZAH commented Oct 24, 2020

I also would love to see this integration make it. The sockets are fairly priced for the quality and it means I can remove bulky Sonoff plugs. Sadly I don't have the skill set in Python to assist.

@b4dpxl b4dpxl mentioned this pull request Oct 24, 2020
20 tasks
@b4dpxl
Copy link
Copy Markdown
Contributor Author

b4dpxl commented Oct 24, 2020

#42314 created to replace this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.