feat: 升级至v2.0.0,新增人员/人脸管理服务与摄像头支持

- 新增 create_person、delete_person、create_face、delete_face 服务
- 支持通过摄像头实体获取图片 (camera_entity_id 参数)
- 添加配置选项支持,可通过 Options Flow 修改配置
- 传感器采用 CoordinatorEntity 模式,优化数据更新机制
- 改进重试逻辑与错误处理
- 重构代码结构,清理冗余代码
This commit is contained in:
2026-05-01 23:14:39 +08:00
parent f462bb1750
commit d081a51aa0
12 changed files with 1711 additions and 1000 deletions
+20 -46
View File
@@ -4,12 +4,9 @@
import logging
from typing import Dict, Any
from homeassistant.components.http import StaticPathConfig
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
DOMAIN,
@@ -17,7 +14,8 @@ from .const import (
CONF_SECRET_ID,
CONF_SECRET_KEY,
CONF_REGION,
CONF_PERSON_GROUP_ID
CONF_PERSON_GROUP_ID,
get_entry_value,
)
from .tencent_cloud_client import TencentCloudClient
from .face_recognition import FaceRecognition
@@ -25,30 +23,24 @@ from .services import async_setup_services, async_unload_services
_LOGGER = logging.getLogger(__name__)
# 添加传感器平台
PLATFORMS.append("sensor")
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""设置配置项"""
_LOGGER.info("设置腾讯云人脸识别插件")
# 获取配置
secret_id = entry.data.get(CONF_SECRET_ID)
secret_key = entry.data.get(CONF_SECRET_KEY)
region = entry.data.get(CONF_REGION, "ap-shanghai")
person_group_id = entry.data.get(CONF_PERSON_GROUP_ID, "Hass")
# 创建腾讯云客户端
region = get_entry_value(entry, CONF_REGION, "ap-shanghai")
person_group_id = get_entry_value(entry, CONF_PERSON_GROUP_ID, "Hass")
try:
client = TencentCloudClient(secret_id, secret_key, region)
except Exception as ex:
_LOGGER.error("创建腾讯云客户端失败: %s", ex)
_LOGGER.error("创建腾讯云客户端失败")
raise ConfigEntryNotReady(f"创建腾讯云客户端失败: {ex}")
# 创建人脸识别实例
face_recognition = FaceRecognition(hass, client)
# 存储配置和实例
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
"entry": entry,
"client": client,
@@ -58,46 +50,28 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"region": region,
"person_group_id": person_group_id
}
# 设置服务
await async_setup_services(hass)
# 设置平台
# 转发配置项到平台
for platform in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setups(entry, [platform])
)
# 不再注册前端面板,改为使用配置流管理
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""卸载配置项"""
_LOGGER.info("卸载腾讯云人脸识别插件")
# 注销自定义面板
# 注意:在新版本的 Home Assistant 中,面板注册方式已更改
# 不需要手动注销面板,系统会自动处理
# 卸载平台
unload_ok = True
for platform in PLATFORMS:
result = await hass.config_entries.async_forward_entry_unload(entry, platform)
unload_ok = unload_ok and result
unload_ok = await hass.config_entries.async_forward_entry_unload(entry, "sensor")
if unload_ok:
# 卸载服务
await async_unload_services(hass)
# 移除数据
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""移除配置项"""
_LOGGER.info("移除腾讯云人脸识别插件配置")
_LOGGER.info("移除腾讯云人脸识别插件配置")