Improve type hints in openalpr_cloud (#145429)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
epenet 2025-05-22 11:30:53 +02:00 committed by GitHub
parent 69f0f38a09
commit ab69223d75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,6 +6,7 @@ import asyncio
from base64 import b64encode from base64 import b64encode
from http import HTTPStatus from http import HTTPStatus
import logging import logging
from typing import Any
import aiohttp import aiohttp
import voluptuous as vol import voluptuous as vol
@ -72,7 +73,8 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the OpenALPR cloud API platform.""" """Set up the OpenALPR cloud API platform."""
confidence = config[CONF_CONFIDENCE] confidence: float = config[CONF_CONFIDENCE]
source: list[dict[str, str]] = config[CONF_SOURCE]
params = { params = {
"secret_key": config[CONF_API_KEY], "secret_key": config[CONF_API_KEY],
"tasks": "plate", "tasks": "plate",
@ -84,7 +86,7 @@ async def async_setup_platform(
OpenAlprCloudEntity( OpenAlprCloudEntity(
camera[CONF_ENTITY_ID], params, confidence, camera.get(CONF_NAME) camera[CONF_ENTITY_ID], params, confidence, camera.get(CONF_NAME)
) )
for camera in config[CONF_SOURCE] for camera in source
) )
@ -99,10 +101,10 @@ class ImageProcessingAlprEntity(ImageProcessingEntity):
self.vehicles = 0 self.vehicles = 0
@property @property
def state(self): def state(self) -> str | None:
"""Return the state of the entity.""" """Return the state of the entity."""
confidence = 0 confidence = 0.0
plate = None plate: str | None = None
# search high plate # search high plate
for i_pl, i_co in self.plates.items(): for i_pl, i_co in self.plates.items():
@ -112,7 +114,7 @@ class ImageProcessingAlprEntity(ImageProcessingEntity):
return plate return plate
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, Any]:
"""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}
@ -156,35 +158,26 @@ class ImageProcessingAlprEntity(ImageProcessingEntity):
class OpenAlprCloudEntity(ImageProcessingAlprEntity): class OpenAlprCloudEntity(ImageProcessingAlprEntity):
"""Representation of an OpenALPR cloud entity.""" """Representation of an OpenALPR cloud entity."""
def __init__(self, camera_entity, params, confidence, name=None): def __init__(
self,
camera_entity: str,
params: dict[str, Any],
confidence: float,
name: str | None,
) -> None:
"""Initialize OpenALPR cloud API.""" """Initialize OpenALPR cloud API."""
super().__init__() super().__init__()
self._params = params self._params = params
self._camera = camera_entity self._attr_camera_entity = camera_entity
self._confidence = confidence self._attr_confidence = confidence
if name: if name:
self._name = name self._attr_name = name
else: else:
self._name = f"OpenAlpr {split_entity_id(camera_entity)[1]}" self._attr_name = f"OpenAlpr {split_entity_id(camera_entity)[1]}"
@property async def async_process_image(self, image: bytes) -> None:
def confidence(self):
"""Return minimum confidence for send events."""
return self._confidence
@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):
"""Process image. """Process image.
This method is a coroutine. This method is a coroutine.