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