python-broadlink icon indicating copy to clipboard operation
python-broadlink copied to clipboard

Device discovery filter

Open horse315 opened this issue 8 years ago • 6 comments

There's no way to filter device discovering by MAC, IP or type( Or any other API to create certain device instance by MAC or IP (but not both).

horse315 avatar Mar 24 '18 16:03 horse315

I can't get what you are trying to say what you want to achieve.

You can always discover all devices and then check if device ip/mac matches or you can create a totally new device by type as its done in the cli.

So maybe give a sample of what you wanna do

Nightreaver avatar Mar 28 '18 08:03 Nightreaver

In my case, I need an API to create an instance of a certain device by MAC, coz IP's are dynamic in my network. If I could call discover() with specific MAC, it could reduce the discovering time (compared to discovering of ALL devices).

horse315 avatar Mar 28 '18 11:03 horse315

I am not sure if I understood correctly. So you want the discover to concentrate on a specific device mac, so save time. Because discovering every device might take longer ?

From my understanding, discovery doesn't work that way. The broadlink "discover" will send a broadcast and receive and collect all the data it receives in response to that broadcast. There is nothing what will speedup the process - except reducing the timeout. Analyzing the data to get mac etc is already the last part.

The Thing that is taking time is the "broadlink_discovery" cli tool, because that will also talk to each device, authenticate etc.

You can skip that by just using the native broadlink.discover() , then you have direct access to the device list which does not take any longer the timeout you define. Then just pick the device you need. And you can lower the timeout if it works for you...

Nightreaver avatar Mar 28 '18 13:03 Nightreaver

Agreed with Nightreaver.
Another idea is to find the hostname of your device and obtain the IP address by simply looking up the hostname. You can then .gendevice() your device manually.

If you insist on looking up with MAC address there are ways to do that. However, I doubt they'd be much quicker than simply .discover()ing.

ptd006 avatar Oct 30 '18 09:10 ptd006

Below is an explicit example, which I have successfully tested with my broadlink central heating controller.

You need to know the MAC address, hostname, and dev type for the code below.

mac_addr = bytearray(b'\xxx\xxx\xxx\xxx') # you are supposed to know this ... 
hostname = "BroadLink_OEM-xx-xx-xx-xx" # and this

import socket
ip_addr = socket.gethostbyaddr(hostname)[2][0]

dev = broadlink.gendevice(0x4EAD, (ip_addr, 80), mac_addr)

dev.auth()
print('Current temperature: '+ str(dev.get_temp()) + 'C')

ptd006 avatar Oct 30 '18 19:10 ptd006

Fixed with https://github.com/mjg59/python-broadlink/pull/427. Now you can use broadlink.xdiscover() to create your own filter. Closing.

felipediel avatar Feb 17 '21 04:02 felipediel