修复 1: Lovelace 卡片配置错误 - 重写 sigmesh-gateway-panel.js 遵循 HA 卡片规范 - 添加 setConfig 方法 - 添加 getCardSize 方法 - 添加静态方法 (getStubConfig, getConfigElement) - 使用 ha-card 包装器和 shadow DOM - 修复 hass 设置和渲染逻辑 修复 2: 过时常量警告 - light.py: 将 ATTR_COLOR_TEMP 替换为 ATTR_COLOR_TEMP_KELVIN - 修复 HA Core 2026.1 兼容性警告 部署步骤: 1. cp custom_components/sigmesh_gateway/sigmesh-gateway-panel.js ~/.homeassistant/www/community/sigmesh_gateway/ 2. cp -r custom_components/sigmesh_gateway ~/.homeassistant/custom_components/ 3. ha core restart
129 lines
4.0 KiB
Python
129 lines
4.0 KiB
Python
"""SigMesh Gateway 灯光平台."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.components.light import (
|
|
ATTR_BRIGHTNESS,
|
|
ATTR_COLOR_TEMP_KELVIN,
|
|
ATTR_RGB_COLOR,
|
|
ColorMode,
|
|
LightEntity,
|
|
)
|
|
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[SigMeshLight] = []
|
|
|
|
if coordinator.data is None:
|
|
_LOGGER.debug("协调器数据为空,等待设备数据")
|
|
return
|
|
|
|
for device in coordinator.data.values():
|
|
# 检查是否为灯光设备
|
|
if device.model_id in (0x1300, 0x1307, 0x130C, 0x130D):
|
|
entities.append(SigMeshLight(coordinator, device))
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
class SigMeshLight(CoordinatorEntity, LightEntity):
|
|
"""SigMesh 灯光实体."""
|
|
|
|
_attr_has_entity_name = True
|
|
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
|
_attr_color_mode = ColorMode.BRIGHTNESS
|
|
|
|
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}_light_{self._mac}"
|
|
self._attr_name = f"Light {self._mac}"
|
|
|
|
# 根据模型 ID 设置支持的功能
|
|
if device.model_id in (0x1307, 0x130C, 0x130D):
|
|
self._attr_supported_color_modes = {
|
|
ColorMode.COLOR_TEMP,
|
|
ColorMode.RGB,
|
|
}
|
|
self._attr_color_mode = ColorMode.COLOR_TEMP
|
|
self._attr_min_color_temp_kelvin = 2000
|
|
self._attr_max_color_temp_kelvin = 6500
|
|
|
|
@property
|
|
def is_on(self) -> bool | None:
|
|
"""返回灯光状态."""
|
|
return self._device.states.get("onoff", False)
|
|
|
|
@property
|
|
def brightness(self) -> int | None:
|
|
"""返回亮度值 (0-255)。"""
|
|
lightness = self._device.states.get("lightness", 0)
|
|
# 将 0-65535 映射到 0-255
|
|
return int(lightness / 257) if lightness else 0
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
"""打开灯光."""
|
|
_LOGGER.info("打开灯光:%s, 参数:%s", self._mac, kwargs)
|
|
|
|
# TODO: 发送 Mesh 命令
|
|
# if ATTR_BRIGHTNESS in kwargs:
|
|
# brightness = kwargs[ATTR_BRIGHTNESS]
|
|
# lightness = brightness * 257
|
|
# await self.coordinator.send_lightness_command(self._mac, lightness)
|
|
|
|
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,
|
|
"lightness": self._device.states.get("lightness"),
|
|
}
|
|
|
|
@property
|
|
def device_info(self) -> DeviceInfo:
|
|
"""返回设备信息."""
|
|
return DeviceInfo(
|
|
identifiers={(DOMAIN, self._device.mac_address)},
|
|
name=f"SigMesh Light {self._device.mac_address}",
|
|
manufacturer="SigMesh",
|
|
)
|