You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Yeelight库discover_bulbs()失效求助:过段时间返回空列表

问题描述
  • 基于Yeelight库编写Python脚本控制小米灯具,初始调用discover_bulbs()可正常返回设备信息:
>>> from yeelight import discover_bulbs
>>> discover_bulbs()
[{"capabilities": {"bright": "50",
                   "color_mode": "1",
                   "ct": "2700",
                   "fw_ver": "45",
                   "hue": "359",
                   "id": "0x0000000002dfb19a",
                   "model": "color",
                   "name": "bedroom",
                   "power": "off",
                   "rgb": "16711935",
                   "sat": "100",
                   "support": "get_prop set_default set_power toggle "
                              "set_bright start_cf stop_cf set_scene cron_add "
                              "cron_get cron_del set_ct_abx set_rgb set_hsv "
                              "set_adjust set_music set_name"},
  "ip": "192.168.0.19",
  "port": 55443}]
  • 一段时间后discover_bulbs()返回空列表,重置灯具可暂时恢复,但不久后再次失效。
  • 灯具在官方APP可正常使用,直接指定IP能控制,但IP会不定期变动,且已开启LAN控制。
  • 现有脚本代码:
from yeelight import *
import time
import schedule
# ip=discover_bulbs()[0]['ip']
ip=discover_bulbs()
print(ip)
bulb=Bulb(ip)

def turnOnAt16():
    bulb.turn_on()
    bulb.set_brightness(100)
    bulb.set_color_temp(4100)

def turnOnAt21():
    bulb.turn_on()
    bulb.set_rgb(255,0,0)
    bulb.set_brightness(100)

def main():
    schedule.every().day.at('08:00').do(turnOnAt16)
    schedule.every().day.at('16:00').do(turnOnAt16)
    schedule.every().day.at('21:30').do(turnOnAt21)
    schedule.every().day.at('23:00').do(bulb.turn_off)
    while True:
        schedule.run_pending()
        time.sleep(10)

if __name__=='__main__':
    main()
解决方案

1. 修复脚本IP获取的错误

现有代码中ip=discover_bulbs()直接将设备列表赋值给ip,但Bulb()构造函数需要单个IP字符串或包含IP的字典,这会导致初始化失败。正确写法:

# 获取第一个设备的IP
devices = discover_bulbs()
if not devices:
    raise Exception("未发现Yeelight设备")
ip = devices[0]['ip']
bulb = Bulb(ip)

2. 解决设备发现失效问题

(1)避免仅在脚本启动时执行一次发现

脚本启动时只执行一次设备发现,后续IP变动或发现失效后无法找到设备。建议在每次执行控制任务前重新执行发现,或在控制失败时重试:

def get_bulb():
    devices = discover_bulbs()
    if not devices:
        # 重试3次发现
        for _ in range(3):
            time.sleep(2)
            devices = discover_bulbs()
            if devices:
                break
        if not devices:
            raise Exception("多次尝试后仍未发现设备")
    return Bulb(devices[0]['ip'])

# 定时任务中每次获取最新的Bulb实例
def turnOnAt16():
    bulb = get_bulb()
    bulb.turn_on()
    bulb.set_brightness(100)
    bulb.set_color_temp(4100)

def turnOnAt21():
    bulb = get_bulb()
    bulb.turn_on()
    bulb.set_rgb(255,0,0)
    bulb.set_brightness(100)

def turn_off():
    bulb = get_bulb()
    bulb.turn_off()

(2)给灯具设置静态IP

IP不定期变动是因为路由器DHCP自动分配,在路由器后台给灯具绑定静态IP可彻底解决IP变动和发现失效问题:

  • 登录路由器管理后台(通常为192.168.1.1或192.168.0.1)
  • 找到DHCP设置或静态IP绑定选项
  • 输入灯具的MAC地址(可从官方APP或路由器已连接设备列表查看)和指定的固定IP
  • 保存后,脚本直接使用固定IP初始化:
bulb = Bulb("192.168.0.19") # 替换为你设置的静态IP

(3)延长设备发现超时时间

Yeelight的discover_bulbs()默认超时较短,可通过timeout参数延长超时,提升发现成功率:

devices = discover_bulbs(timeout=10) # 超时设为10秒,默认5秒

3. 优化定时任务稳定性

在定时任务中加入异常捕获,避免单个任务失败导致整个脚本崩溃:

def turnOnAt16():
    try:
        bulb = get_bulb()
        bulb.turn_on()
        bulb.set_brightness(100)
        bulb.set_color_temp(4100)
    except Exception as e:
        print(f"开启任务失败: {e}")

# 其他定时任务同理添加try-except块

内容的提问来源于stack exchange,提问作者blyatscript

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 05:40:38