💡

【python-yeelight】discover_bulbs() が [] になる問題を解決する

2024/09/16に公開

How to solve discover_bulbs() returns empty list

yeelight.discover_bulbs() does not work?

>>> import yeelight
>>> yeelight.discover_bulbs()
[]

too many hackers are confused..

Yeelight のバルブが検出できない問題
見つからなくてハマる人多数(自分もハマった)

My solution

import yeelight
import netifaces

def get_interface_name(addr):
    netifs = netifaces.interfaces()
    for name in netifs:
        nameif = netifaces.ifaddresses(name)
        for key in nameif:
            ifaddr = nameif[key][0]
            if addr == ifaddr.get('broadcast'):
                return name
    return None

name = get_interface_name('192.168.0.255')
bulbs = yeelight.discover_bulbs(interface=name)

print(len(bulbs))    # -> 2

Path to a solution

Read the source

https://gitlab.com/stavros/python-yeelight/-/blob/master/yeelight/ssdp_discover.py

hmm, specify interface name..
なるほど、ネットワークが複数ある場合はインターフェイス名を指定しろ、と

How many networks are?

>>> import netifaces
>>> netifs = netifaces.interfaces()
>>> netifs
['{C4D1D99E-DEB7-40F8-8C90-FD37DFD772E5}', '{2E13672A-D0B7-4F3F-9951-D6E29DB1FFCA}', '{EA3FF5C8-E070-45BE-8103-2A8521322507}', '{E74A4821-9009-4608-8548-82AE30178014}', '{0859027E-8881-46A5-A72C-3D3013AF47D4}', '{EB962E58-004A-11EB-929F-806E6F6E6963}', '{B5FB18F3-7A20-4A62-864A-E29EC6470750}']

wow..(Windows)

Find network have the broadcast address

>>> for name in netifs:
...   nameif = netifaces.ifaddresses(name)
...   for key in nameif:
...     name, nameif[key][0].get('broadcast')
...
('{C4D1D99E-DEB7-40F8-8C90-FD37DFD772E5}', None)
('{C4D1D99E-DEB7-40F8-8C90-FD37DFD772E5}', '2400:2413:9200:5900:ffff:ffff:ffff:ffff')
('{C4D1D99E-DEB7-40F8-8C90-FD37DFD772E5}', '192.168.0.255')
('{2E13672A-D0B7-4F3F-9951-D6E29DB1FFCA}', None)
('{2E13672A-D0B7-4F3F-9951-D6E29DB1FFCA}', 'fe80::ffff:ffff:ffff:ffff%7')
('{2E13672A-D0B7-4F3F-9951-D6E29DB1FFCA}', '192.168.56.255')
('{EA3FF5C8-E070-45BE-8103-2A8521322507}', None)
('{EA3FF5C8-E070-45BE-8103-2A8521322507}', 'fe80::ffff:ffff:ffff:ffff%21')
('{EA3FF5C8-E070-45BE-8103-2A8521322507}', '192.168.33.255')
('{E74A4821-9009-4608-8548-82AE30178014}', None)
('{E74A4821-9009-4608-8548-82AE30178014}', 'fe80::ffff:ffff:ffff:ffff%20')
('{0859027E-8881-46A5-A72C-3D3013AF47D4}', None)
('{0859027E-8881-46A5-A72C-3D3013AF47D4}', 'fe80::ffff:ffff:ffff:ffff%3')
('{EB962E58-004A-11EB-929F-806E6F6E6963}', None)
('{EB962E58-004A-11EB-929F-806E6F6E6963}', '::1')
('{EB962E58-004A-11EB-929F-806E6F6E6963}', '127.255.255.255')
('{B5FB18F3-7A20-4A62-864A-E29EC6470750}', None)
('{B5FB18F3-7A20-4A62-864A-E29EC6470750}', 'fe80::ffff:ffff:ffff:ffff%25')
('{B5FB18F3-7A20-4A62-864A-E29EC6470750}', '172.29.95.255')

return interface name match the broadcast address..
ブロードキャストアドレスが一致する名前を返してあげればよさそう

it's solved.
というわけで解決です

Discussion