impress_sig_mesh_hacs/custom_components/sigmesh_gateway/device_tracker.py
impressionyang 19774a4a9f fix: 修复平台加载和 OptionsFlow 错误
主要修复:
1. 将平台文件从 platforms/ 移到集成根目录
   - sensor.py, binary_sensor.py, switch.py, light.py, device_tracker.py
2. 修复 OptionsFlow config_entry 属性错误
   - 移除手动设置的 config_entry
3. 添加集成图标 icons/icon.png
4. manifest.json 添加 integration_type: device

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 09:39:35 +08:00

86 lines
2.5 KiB
Python

"""SigMesh Gateway 设备追踪平台."""
from __future__ import annotations
import logging
from typing import Any
from homeassistant import config_entries
from homeassistant.components.device_tracker import SourceType, TrackerEntity
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[SigMeshDeviceTracker] = []
for device in coordinator.data.values():
entities.append(SigMeshDeviceTracker(coordinator, device))
async_add_entities(entities)
class SigMeshDeviceTracker(CoordinatorEntity, TrackerEntity):
"""SigMesh 设备追踪实体."""
_attr_has_entity_name = True
_attr_source_type = SourceType.BLUETOOTH_LE
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}_tracker_{self._mac}"
self._attr_name = f"Tracker {self._mac}"
@property
def mac_address(self) -> str:
"""返回 MAC 地址."""
return self._device.mac_address
@property
def source_type(self) -> SourceType:
"""返回源类型."""
return SourceType.BLUETOOTH_LE
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""返回额外状态属性."""
return {
"mac_address": self._device.mac_address,
"model_id": self._device.model_id,
"last_update": self._device.last_update,
}
@property
def device_info(self) -> DeviceInfo:
"""返回设备信息."""
return DeviceInfo(
identifiers={(DOMAIN, self._device.mac_address)},
name=f"SigMesh Device {self._device.mac_address}",
manufacturer="SigMesh",
)