From ab69223d75fa3387436dea0a59c2cafaf15a5eea Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 22 May 2025 11:30:53 +0200 Subject: [PATCH] Improve type hints in openalpr_cloud (#145429) Co-authored-by: Joost Lekkerkerker --- .../openalpr_cloud/image_processing.py | 47 ++++++++----------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/homeassistant/components/openalpr_cloud/image_processing.py b/homeassistant/components/openalpr_cloud/image_processing.py index 2bdf9947fe2..f541ee0b515 100644 --- a/homeassistant/components/openalpr_cloud/image_processing.py +++ b/homeassistant/components/openalpr_cloud/image_processing.py @@ -6,6 +6,7 @@ import asyncio from base64 import b64encode from http import HTTPStatus import logging +from typing import Any import aiohttp import voluptuous as vol @@ -72,7 +73,8 @@ async def async_setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """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 = { "secret_key": config[CONF_API_KEY], "tasks": "plate", @@ -84,7 +86,7 @@ async def async_setup_platform( OpenAlprCloudEntity( 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 @property - def state(self): + def state(self) -> str | None: """Return the state of the entity.""" - confidence = 0 - plate = None + confidence = 0.0 + plate: str | None = None # search high plate for i_pl, i_co in self.plates.items(): @@ -112,7 +114,7 @@ class ImageProcessingAlprEntity(ImageProcessingEntity): return plate @property - def extra_state_attributes(self): + def extra_state_attributes(self) -> dict[str, Any]: """Return device specific state attributes.""" return {ATTR_PLATES: self.plates, ATTR_VEHICLES: self.vehicles} @@ -156,35 +158,26 @@ class ImageProcessingAlprEntity(ImageProcessingEntity): class OpenAlprCloudEntity(ImageProcessingAlprEntity): """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.""" super().__init__() self._params = params - self._camera = camera_entity - self._confidence = confidence + self._attr_camera_entity = camera_entity + self._attr_confidence = confidence if name: - self._name = name + self._attr_name = name else: - self._name = f"OpenAlpr {split_entity_id(camera_entity)[1]}" + self._attr_name = f"OpenAlpr {split_entity_id(camera_entity)[1]}" - @property - 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): + async def async_process_image(self, image: bytes) -> None: """Process image. This method is a coroutine.