Skip to content

Commit

Permalink
Modernize Image Processing typing (#83405)
Browse files Browse the repository at this point in the history
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
  • Loading branch information
frenck and epenet authored Dec 6, 2022
1 parent c507ad8 commit d715aa6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
36 changes: 33 additions & 3 deletions homeassistant/components/image_processing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import asyncio
from dataclasses import dataclass
from datetime import timedelta
import logging
from typing import Any, Final, TypedDict, final
Expand All @@ -21,7 +22,7 @@
from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import make_entity_service_schema
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.typing import ConfigType
from homeassistant.util.async_ import run_callback_threadsafe
Expand Down Expand Up @@ -119,20 +120,49 @@ async def async_scan_service(service: ServiceCall) -> None:
return True


@dataclass
class ImageProcessingEntityDescription(EntityDescription):
"""A class that describes sensor entities."""

device_class: ImageProcessingDeviceClass | None = None
camera_entity: str | None = None
confidence: float | None = None


class ImageProcessingEntity(Entity):
"""Base entity class for image processing."""

_attr_device_class: ImageProcessingDeviceClass | str | None
entity_description: ImageProcessingEntityDescription
_attr_device_class: ImageProcessingDeviceClass | None
_attr_camera_entity: str | None
_attr_confidence: float | None
timeout = DEFAULT_TIMEOUT

@property
def camera_entity(self) -> str | None:
"""Return camera entity id from process pictures."""
if hasattr(self, "_attr_camera_entity"):
return self._attr_camera_entity
if hasattr(self, "entity_description"):
return self.entity_description.camera_entity
return None

@property
def confidence(self) -> float | None:
"""Return minimum confidence for do some things."""
"""Return minimum confidence to do some things."""
if hasattr(self, "_attr_confidence"):
return self._attr_confidence
if hasattr(self, "entity_description"):
return self.entity_description.confidence
return None

@property
def device_class(self) -> ImageProcessingDeviceClass | None:
"""Return the class of this entity."""
if hasattr(self, "_attr_device_class"):
return self._attr_device_class
if hasattr(self, "entity_description"):
return self.entity_description.device_class
return None

def process_image(self, image: bytes) -> None:
Expand Down
4 changes: 4 additions & 0 deletions pylint/plugins/hass_enforce_type_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,10 @@ class ClassTypeHintMatch:
function_name="confidence",
return_type=["float", None],
),
TypeHintMatch(
function_name="device_class",
return_type=["ImageProcessingDeviceClass", None],
),
TypeHintMatch(
function_name="process_image",
arg_types={1: "bytes"},
Expand Down

0 comments on commit d715aa6

Please sign in to comment.