Improve type hints in microsoft_face_detect (#145421)

* Improve type hints in microsoft_face_detect

* Improve
This commit is contained in:
epenet 2025-05-22 11:52:26 +02:00 committed by GitHub
parent ca914d8e4f
commit a54c8a88ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,6 +3,7 @@
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
import voluptuous as vol
@ -11,9 +12,10 @@ from homeassistant.components.image_processing import (
ATTR_GENDER,
ATTR_GLASSES,
PLATFORM_SCHEMA as IMAGE_PROCESSING_PLATFORM_SCHEMA,
FaceInformation,
ImageProcessingFaceEntity,
)
from homeassistant.components.microsoft_face import DATA_MICROSOFT_FACE
from homeassistant.components.microsoft_face import DATA_MICROSOFT_FACE, MicrosoftFace
from homeassistant.const import CONF_ENTITY_ID, CONF_NAME, CONF_SOURCE
from homeassistant.core import HomeAssistant, split_entity_id
from homeassistant.exceptions import HomeAssistantError
@ -54,43 +56,40 @@ async def async_setup_platform(
) -> None:
"""Set up the Microsoft Face detection platform."""
api = hass.data[DATA_MICROSOFT_FACE]
attributes = config[CONF_ATTRIBUTES]
attributes: list[str] = config[CONF_ATTRIBUTES]
source: list[dict[str, str]] = config[CONF_SOURCE]
async_add_entities(
MicrosoftFaceDetectEntity(
camera[CONF_ENTITY_ID], api, attributes, camera.get(CONF_NAME)
)
for camera in config[CONF_SOURCE]
for camera in source
)
class MicrosoftFaceDetectEntity(ImageProcessingFaceEntity):
"""Microsoft Face API entity for identify."""
def __init__(self, camera_entity, api, attributes, name=None):
def __init__(
self,
camera_entity: str,
api: MicrosoftFace,
attributes: list[str],
name: str | None,
) -> None:
"""Initialize Microsoft Face."""
super().__init__()
self._api = api
self._camera = camera_entity
self._attr_camera_entity = camera_entity
self._attributes = attributes
if name:
self._name = name
self._attr_name = name
else:
self._name = f"MicrosoftFace {split_entity_id(camera_entity)[1]}"
self._attr_name = f"MicrosoftFace {split_entity_id(camera_entity)[1]}"
@property
def camera_entity(self):
"""Return camera entity id from process pictures."""
return self._camera
@property
def name(self):
"""Return the name of the entity."""
return self._name
async def async_process_image(self, image):
async def async_process_image(self, image: bytes) -> None:
"""Process image.
This method is a coroutine.
@ -112,12 +111,14 @@ class MicrosoftFaceDetectEntity(ImageProcessingFaceEntity):
if not face_data:
face_data = []
faces = []
faces: list[FaceInformation] = []
for face in face_data:
face_attr = {}
face_attr = FaceInformation()
for attr in self._attributes:
if TYPE_CHECKING:
assert attr in SUPPORTED_ATTRIBUTES
if attr in face["faceAttributes"]:
face_attr[attr] = face["faceAttributes"][attr]
face_attr[attr] = face["faceAttributes"][attr] # type: ignore[literal-required]
if face_attr:
faces.append(face_attr)