From 311cfbbe7e1ae3e8d9ec229d5126ce809adfe5d0 Mon Sep 17 00:00:00 2001 From: impressionyang Date: Thu, 16 Apr 2026 09:48:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=92=8C=20OptionsFlow=20=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复内容: 1. 所有平台文件添加 coordinator.data None 检查 - sensor.py, binary_sensor.py, switch.py, light.py, device_tracker.py 2. 修复 async_get_options_flow 签名 - 改为实例方法,移除 config_entry 参数 - 移除 @staticmethod 装饰器 3. OptionsFlow 添加正确的 __init__ 方法 Co-Authored-By: Claude Opus 4.6 --- custom_components/sigmesh_gateway/binary_sensor.py | 4 ++++ custom_components/sigmesh_gateway/config_flow.py | 10 ++++++---- custom_components/sigmesh_gateway/device_tracker.py | 4 ++++ custom_components/sigmesh_gateway/light.py | 4 ++++ custom_components/sigmesh_gateway/sensor.py | 6 +++++- custom_components/sigmesh_gateway/switch.py | 4 ++++ 6 files changed, 27 insertions(+), 5 deletions(-) diff --git a/custom_components/sigmesh_gateway/binary_sensor.py b/custom_components/sigmesh_gateway/binary_sensor.py index 3e5d717..cd37120 100644 --- a/custom_components/sigmesh_gateway/binary_sensor.py +++ b/custom_components/sigmesh_gateway/binary_sensor.py @@ -34,6 +34,10 @@ async def async_setup_entry( entities: list[SigMeshBinarySensor] = [] + if coordinator.data is None: + _LOGGER.debug("协调器数据为空,等待设备数据") + return + for device in coordinator.data.values(): # 检测是否存在开关状态 if device.states.get("onoff") is not None: diff --git a/custom_components/sigmesh_gateway/config_flow.py b/custom_components/sigmesh_gateway/config_flow.py index 2ec74b3..1b4e78b 100644 --- a/custom_components/sigmesh_gateway/config_flow.py +++ b/custom_components/sigmesh_gateway/config_flow.py @@ -6,7 +6,6 @@ from typing import Any import voluptuous as vol from homeassistant import config_entries -from homeassistant.core import callback from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers import selector @@ -65,18 +64,21 @@ class SigMeshGatewayConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): errors=errors, ) - @staticmethod @callback def async_get_options_flow( - config_entry: config_entries.ConfigEntry, + self, ) -> SigMeshGatewayOptionsFlow: """获取选项流程.""" - return SigMeshGatewayOptionsFlow(config_entry) + return SigMeshGatewayOptionsFlow() class SigMeshGatewayOptionsFlow(config_entries.OptionsFlow): """SigMesh Gateway 选项流程.""" + def __init__(self) -> None: + """初始化选项流程.""" + self._errors: dict[str, str] = {} + async def async_step_init(self, user_input: dict[str, Any] | None = None) -> FlowResult: """管理选项.""" if user_input is not None: diff --git a/custom_components/sigmesh_gateway/device_tracker.py b/custom_components/sigmesh_gateway/device_tracker.py index d61b4ca..c2067a2 100644 --- a/custom_components/sigmesh_gateway/device_tracker.py +++ b/custom_components/sigmesh_gateway/device_tracker.py @@ -31,6 +31,10 @@ async def async_setup_entry( entities: list[SigMeshDeviceTracker] = [] + if coordinator.data is None: + _LOGGER.debug("协调器数据为空,等待设备数据") + return + for device in coordinator.data.values(): entities.append(SigMeshDeviceTracker(coordinator, device)) diff --git a/custom_components/sigmesh_gateway/light.py b/custom_components/sigmesh_gateway/light.py index 41b4d6b..977fc26 100644 --- a/custom_components/sigmesh_gateway/light.py +++ b/custom_components/sigmesh_gateway/light.py @@ -37,6 +37,10 @@ async def async_setup_entry( entities: list[SigMeshLight] = [] + if coordinator.data is None: + _LOGGER.debug("协调器数据为空,等待设备数据") + return + for device in coordinator.data.values(): # 检查是否为灯光设备 if device.model_id in (0x1300, 0x1307, 0x130C, 0x130D): diff --git a/custom_components/sigmesh_gateway/sensor.py b/custom_components/sigmesh_gateway/sensor.py index d925184..c7da7f8 100644 --- a/custom_components/sigmesh_gateway/sensor.py +++ b/custom_components/sigmesh_gateway/sensor.py @@ -36,7 +36,11 @@ async def async_setup_entry( # 创建传感器实体 entities: list[SigMeshSensor] = [] - # 从协调器获取设备 + # 从协调器获取设备(检查 data 是否为 None) + if coordinator.data is None: + _LOGGER.debug("协调器数据为空,等待设备数据") + return + for device in coordinator.data.values(): # 根据设备状态创建相应的传感器 if device.states.get("property_id") is not None: diff --git a/custom_components/sigmesh_gateway/switch.py b/custom_components/sigmesh_gateway/switch.py index f1ada79..90395d3 100644 --- a/custom_components/sigmesh_gateway/switch.py +++ b/custom_components/sigmesh_gateway/switch.py @@ -31,6 +31,10 @@ async def async_setup_entry( entities: list[SigMeshSwitch] = [] + if coordinator.data is None: + _LOGGER.debug("协调器数据为空,等待设备数据") + return + for device in coordinator.data.values(): # 检查是否为开关设备 if device.model_id == 0x1000: # OnOff Server 模型