修复内容: 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 <noreply@anthropic.com>
98 lines
3.0 KiB
Python
98 lines
3.0 KiB
Python
"""SigMesh Gateway 开关平台."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.components.switch import SwitchEntity
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
from .const import DOMAIN
|
|
from .coordinator import SigMeshGatewayCoordinator
|
|
from .protocol_parser import DeviceState
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: config_entries.ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""设置开关平台."""
|
|
coordinator: SigMeshGatewayCoordinator = hass.data[DOMAIN][entry.entry_id][
|
|
"coordinator"
|
|
]
|
|
|
|
entities: list[SigMeshSwitch] = []
|
|
|
|
if coordinator.data is None:
|
|
_LOGGER.debug("协调器数据为空,等待设备数据")
|
|
return
|
|
|
|
for device in coordinator.data.values():
|
|
# 检查是否为开关设备
|
|
if device.model_id == 0x1000: # OnOff Server 模型
|
|
entities.append(SigMeshSwitch(coordinator, device))
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
class SigMeshSwitch(CoordinatorEntity, SwitchEntity):
|
|
"""SigMesh 开关实体."""
|
|
|
|
_attr_has_entity_name = True
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: SigMeshGatewayCoordinator,
|
|
device: DeviceState,
|
|
) -> None:
|
|
"""初始化开关."""
|
|
super().__init__(coordinator)
|
|
self._device = device
|
|
self._mac = device.mac_address
|
|
|
|
self._attr_unique_id = f"{DOMAIN}_switch_{self._mac}"
|
|
self._attr_name = f"Switch {self._mac}"
|
|
|
|
@property
|
|
def is_on(self) -> bool | None:
|
|
"""返回开关状态."""
|
|
return self._device.states.get("onoff")
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
"""打开开关."""
|
|
# TODO: 发送 Mesh 命令
|
|
_LOGGER.info("打开开关:%s", self._mac)
|
|
# await self.coordinator.send_onoff_command(self._mac, True)
|
|
await self.coordinator.async_request_refresh()
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
"""关闭开关."""
|
|
_LOGGER.info("关闭开关:%s", self._mac)
|
|
# await self.coordinator.send_onoff_command(self._mac, False)
|
|
await self.coordinator.async_request_refresh()
|
|
|
|
@property
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
|
"""返回额外状态属性."""
|
|
return {
|
|
"mac_address": self._device.mac_address,
|
|
"model_id": self._device.model_id,
|
|
}
|
|
|
|
@property
|
|
def device_info(self) -> DeviceInfo:
|
|
"""返回设备信息."""
|
|
return DeviceInfo(
|
|
identifiers={(DOMAIN, self._device.mac_address)},
|
|
name=f"SigMesh Device {self._device.mac_address}",
|
|
manufacturer="SigMesh",
|
|
)
|