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, start: datetime.datetime,
end: datetime.datetime, end: datetime.datetime,
max_diff: float, max_diff: float,
): ) -> None:
statistics: list[StatisticData] = [] statistics: list[StatisticData] = []
now = start now = start
sum_ = 0.0 sum_ = 0.0

View File

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

View File

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

View File

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

View File

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