Improve type hints in demo and dependencies (#78022)

This commit is contained in:
epenet 2022-09-08 12:46:41 +02:00 committed by GitHub
parent ff356205bf
commit 03f4eb84a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 26 deletions

View File

@ -260,7 +260,7 @@ async def _insert_sum_statistics(
start: datetime.datetime,
end: datetime.datetime,
max_diff: float,
):
) -> None:
statistics: list[StatisticData] = []
now = start
sum_ = 0.0

View File

@ -3,10 +3,7 @@ from __future__ import annotations
from homeassistant.components.camera import Image
from homeassistant.components.image_processing import (
ATTR_AGE,
ATTR_CONFIDENCE,
ATTR_GENDER,
ATTR_NAME,
FaceInformation,
ImageProcessingFaceEntity,
)
from homeassistant.components.openalpr_local.image_processing import (
@ -87,14 +84,14 @@ class DemoImageProcessingFace(ImageProcessingFaceEntity):
def process_image(self, image: Image) -> None:
"""Process image."""
demo_data = [
{
ATTR_CONFIDENCE: 98.34,
ATTR_NAME: "Hans",
ATTR_AGE: 16.0,
ATTR_GENDER: "male",
},
{ATTR_NAME: "Helena", ATTR_AGE: 28.0, ATTR_GENDER: "female"},
{ATTR_CONFIDENCE: 62.53, ATTR_NAME: "Luna"},
FaceInformation(
confidence=98.34,
name="Hans",
age=16.0,
gender="male",
),
FaceInformation(name="Helena", age=28.0, gender="female"),
FaceInformation(confidence=62.53, name="Luna"),
]
self.process_faces(demo_data, 4)

View File

@ -2,7 +2,7 @@
import asyncio
from datetime import timedelta
import logging
from typing import final
from typing import Final, TypedDict, final
import voluptuous as vol
@ -40,7 +40,7 @@ SERVICE_SCAN = "scan"
EVENT_DETECT_FACE = "image_processing.detect_face"
ATTR_AGE = "age"
ATTR_CONFIDENCE = "confidence"
ATTR_CONFIDENCE: Final = "confidence"
ATTR_FACES = "faces"
ATTR_GENDER = "gender"
ATTR_GLASSES = "glasses"
@ -70,6 +70,18 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend(
PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE.extend(PLATFORM_SCHEMA.schema)
class FaceInformation(TypedDict, total=False):
"""Face information."""
confidence: float
name: str
age: float
gender: str
motion: str
glasses: str
entity_id: str
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the image processing."""
component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)
@ -142,9 +154,9 @@ class ImageProcessingEntity(Entity):
class ImageProcessingFaceEntity(ImageProcessingEntity):
"""Base entity class for face image processing."""
def __init__(self):
def __init__(self) -> None:
"""Initialize base face identify/verify entity."""
self.faces = []
self.faces: list[FaceInformation] = []
self.total_faces = 0
@property
@ -182,14 +194,14 @@ class ImageProcessingFaceEntity(ImageProcessingEntity):
"""Return device specific state attributes."""
return {ATTR_FACES: self.faces, ATTR_TOTAL_FACES: self.total_faces}
def process_faces(self, faces, total):
def process_faces(self, faces: list[FaceInformation], total: int) -> None:
"""Send event with detected faces and store data."""
run_callback_threadsafe(
self.hass.loop, self.async_process_faces, faces, total
).result()
@callback
def async_process_faces(self, faces, total):
def async_process_faces(self, faces: list[FaceInformation], total: int) -> None:
"""Send event with detected faces and store data.
known are a dict in follow format:

View File

@ -144,13 +144,13 @@ class MailboxEntity(Entity):
class Mailbox:
"""Represent a mailbox device."""
def __init__(self, hass, name):
def __init__(self, hass: HomeAssistant, name: str) -> None:
"""Initialize mailbox object."""
self.hass = hass
self.name = name
@callback
def async_update(self):
def async_update(self) -> None:
"""Send event notification of updated mailbox."""
self.hass.bus.async_fire(EVENT)

View File

@ -102,9 +102,9 @@ async def async_setup_platform(
class ImageProcessingAlprEntity(ImageProcessingEntity):
"""Base entity class for ALPR image processing."""
def __init__(self):
def __init__(self) -> None:
"""Initialize base ALPR entity."""
self.plates = {}
self.plates: dict[str, float] = {}
self.vehicles = 0
@property
@ -130,18 +130,18 @@ class ImageProcessingAlprEntity(ImageProcessingEntity):
"""Return device specific state attributes."""
return {ATTR_PLATES: self.plates, ATTR_VEHICLES: self.vehicles}
def process_plates(self, plates, vehicles):
def process_plates(self, plates: dict[str, float], vehicles: int) -> None:
"""Send event with new plates and store data."""
run_callback_threadsafe(
self.hass.loop, self.async_process_plates, plates, vehicles
).result()
@callback
def async_process_plates(self, plates, vehicles):
def async_process_plates(self, plates: dict[str, float], vehicles: int) -> None:
"""Send event with new plates and store data.
plates are a dict in follow format:
{ 'plate': confidence }
{ '<plate>': confidence }
This method must be run in the event loop.
"""