Custom python library name collision

I’m running home assistant on a docker hosted on a rasp-pi.
To control my lights I require to use this python library (there is no built-in support for my lights besides this one).
To get the library to install on the home-assistant docker I added it to one of my custom components’ manifest requirements.
So far so good only that when I try to use the library (via a script I wrote and added to /config/python_scripts) I can’t seem to import the right one. The thing is, the library I installed uses the name ‘broadlink’ and from the errors I get, I suspect home-assistant is importing the built-in broadlink integration when my script executes (I see error prints from it).
Anyone has a suggestion on how to solve this?

Please post your logs :slight_smile:

not sure which logs I should post but this is the errors I get when HA boots:

2021-04-11 00:46:06 ERROR (SyncWorker_2) [homeassistant.components.command_line] Command failed: /config/python_scripts/control_bulb.py kitchen 1
2021-04-11 00:46:06 ERROR (SyncWorker_2) [homeassistant.components.command_line.switch] Command failed: /config/python_scripts/control_bulb.py kitchen 1
2021-04-11 00:46:19 ERROR (SyncWorker_4) [homeassistant.components.command_line] Command failed: /config/python_scripts/control_bulb.py kitchen 0
2021-04-11 00:46:19 ERROR (SyncWorker_4) [homeassistant.components.command_line.switch] Command failed: /config/python_scripts/control_bulb.py kitchen 0
2021-04-11 00:46:23 ERROR (MainThread) [homeassistant.components.broadlink.config_flow] Unsupported device: 0x60c7. If it worked before, please open an issue at https://github.com/home-assistant/core/issues
2021-04-11 00:46:23 ERROR (MainThread) [homeassistant.components.broadlink.config_flow] Unsupported device: 0x60c7. If it worked before, please open an issue at https://github.com/home-assistant/core/issues
2021-04-11 00:46:23 ERROR (MainThread) [homeassistant.components.broadlink.config_flow] Unsupported device: 0x60c7. If it worked before, please open an issue at https://github.com/home-assistant/core/issues
2021-04-11 00:46:23 ERROR (MainThread) [homeassistant.components.broadlink.config_flow] Unsupported device: 0x60c7. If it worked before, please open an issue at https://github.com/home-assistant/core/issues

Python script im running:

#!/usr/bin/python3

import sys
import broadlink

dev_id=24775
dev_port=80
dev_info_dic = {
	"table" : ('192.168.XXX.XXX', bytearray.fromhex('XXXXXXXXXX')),
	"door" : ('192.168.XXX.XXX', bytearray.fromhex('XXXXXXXXXX')),
	"kitchen" : ('192.168.XXX.XXX', bytearray.fromhex('XXXXXXXXXX')),
	"couch" : ('192.168.XXX.XXX', bytearray.fromhex('XXXXXXXXXX'))
}

device_name = sys.argv[1]
device_state = int(sys.argv[2])
try:
	dev_info = dev_info_dic[device_name]
except KeyError:
	print("Unkown device name {}".format(device_name))
	exit(-1)

if (device_state != 0 and device_state != 1):
	print("Unkown device state {}".format(device_state))
	exit(-1)

dev = broadlink.gendevice(dev_id, (dev_info[0], dev_port), dev_info[1])
dev.auth()
dev.set_state(pwr=device_state)

Did pip complain about conflicts when you installed broadlink? Normally if you want to import something from another component it would look like this import homeassistant.component.broadlink.

Using import broadlink should import the right package. :smiley:

not really sure, I added the ‘broadlink’ library to one of the custom components requirements and rebooted HA. It took a lot of time to come back but when it did I just assumed it installed successfully as I didn’t see any error logs.
Any suggestion on how to verify this?

What do you mean by adding to one of the custom_components requirement?

If you want to run a script you will have to install a library by using pip.

maybe I missed something but as I’m running HA on a docker I understood it’s not recommended to modify the docker image directly (is that even possible?) as these changes might be easily deleted by an update (or something else).
Adding it as a requirement to one of the custom components HA lunches should achieve this and is more robust. But I might be wrong…

It is correct that installing a library in the docker image will, when the next time you pull the image, remove it. But this can be easily circumvented by having a script that fetches and installs the libraries again.

I don’t think it is a good idea to edit another custom_components requirements to include your library. This will create a risk of breaking other things.

First of all the library you are linking to is the same as the one the integration uses. So it is installed if you have installed the Broadlink integration.

Can you go into more details of why the integration is not supporting your device but the underlying library is? :thinking:

Thats a good question… I’m not sure… When I use the integration I get the same error as I get when using my script:

2021-04-11 01:56:14 ERROR (MainThread) [homeassistant.components.broadlink.config_flow] Unsupported device: 0x60c7. If it worked before, please open an issue at https://github.com/home-assistant/core/issues

Looking at https://github.com/home-assistant/core/blob/42156bafe0e2e0da3b7327a5f72353da25f00a97/homeassistant/components/broadlink/const.py it doesn’t seem to support lights at all.
The code committed into home-assistant/core is different from the one found on python-broadlink repo. I am able to control my lights when I’m running the python-broadlink repo library (on my rasp-pi).

What device is you trying to use?

A smart light bulb

What is the device name? There is a link on the github page of the python library you want to use.

A simple Python API for controlling Broadlink devices. At present, the following devices are supported:

  • Universal remotes : RM home, RM mini 3, RM plus, RM pro, RM pro+, RM4 mini, RM4 pro, RM4C mini, RM4S
  • Smart plugs : SP mini, SP mini 3, SP mini+, SP1, SP2, SP2-BR, SP2-CL, SP2-IN, SP2-UK, SP3, SP3-EU, SP3S-EU, SP3S-US, SP4L-AU, SP4L-EU, SP4L-UK, SP4M, SP4M-US
  • Power strips : MP1-1K3S2U, MP1-1K4S, MP2
  • Wi-Fi controlled switches : MCB1, SC1, SCB1E
  • Environment sensors : A1
  • Alarm kits : S2KIT
  • Light bulbs : LB1, SB800TD

Other devices with Broadlink DNA:

  • Smart plugs : Ankuoo NEO, Ankuoo NEO PRO, BG AHC/U-01, Efergy Ego
  • Outlets : BG 800, BG 900
  • Curtain motors : Dooya DT360E-45/20
  • Thermostats : Hysen HY02B05H

What device is it from the list you have?

Have you seen this? This is the best way to use script like yours in HA. :smiley:

It’s the LB1 (light bulb)

Do you need to have the Broadlink integration installed? If not, remove it. Somehow the Config_flow process of the Broadlink integration becomes triggered while you do not want that to happen.

Ok this was my bad, the prints are due to the Broadlink integration discovering my “unsupported” light bulbs on boot. They are not related to the command_line switch I set to call my python script.

Once I fixed my command_line switch to actually run my python script I got the following error:

2021-04-13 00:21:52 ERROR (SyncWorker_4) [homeassistant.components.python_script.control_bulb.py] Error executing script: __import__ not found
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 221, in execute
exec(compiled.code, restricted_globals)
File "control_bulb.py", line 3, in <module>
ImportError: __import__ not found

After further reading it appears the running python scripts through HA does not support import.

As you both mentioned the Broadlink integration should support my devices (it should be the same as the python library I’m using). It appears that the library works with my bulbs while the integration isn’t so I’ll open an issue on HA’s core github to see if that can be fixed.
Thanks all for the help!