mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Improve type hints in qrcode (#145430)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
parent
40267760fd
commit
69f0f38a09
@ -21,48 +21,33 @@ def setup_platform(
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the QR code image processing platform."""
|
||||
source: list[dict[str, str]] = config[CONF_SOURCE]
|
||||
add_entities(
|
||||
QrEntity(camera[CONF_ENTITY_ID], camera.get(CONF_NAME))
|
||||
for camera in config[CONF_SOURCE]
|
||||
QrEntity(camera[CONF_ENTITY_ID], camera.get(CONF_NAME)) for camera in source
|
||||
)
|
||||
|
||||
|
||||
class QrEntity(ImageProcessingEntity):
|
||||
"""A QR image processing entity."""
|
||||
|
||||
def __init__(self, camera_entity, name):
|
||||
def __init__(self, camera_entity: str, name: str | None) -> None:
|
||||
"""Initialize QR image processing entity."""
|
||||
super().__init__()
|
||||
|
||||
self._camera = camera_entity
|
||||
self._attr_camera_entity = camera_entity
|
||||
if name:
|
||||
self._name = name
|
||||
self._attr_name = name
|
||||
else:
|
||||
self._name = f"QR {split_entity_id(camera_entity)[1]}"
|
||||
self._state = None
|
||||
self._attr_name = f"QR {split_entity_id(camera_entity)[1]}"
|
||||
self._attr_state = None
|
||||
|
||||
@property
|
||||
def camera_entity(self):
|
||||
"""Return camera entity id from process pictures."""
|
||||
return self._camera
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the entity."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the entity."""
|
||||
return self._name
|
||||
|
||||
def process_image(self, image):
|
||||
def process_image(self, image: bytes) -> None:
|
||||
"""Process image."""
|
||||
stream = io.BytesIO(image)
|
||||
img = Image.open(stream)
|
||||
|
||||
barcodes = pyzbar.decode(img)
|
||||
if barcodes:
|
||||
self._state = barcodes[0].data.decode("utf-8")
|
||||
self._attr_state = barcodes[0].data.decode("utf-8")
|
||||
else:
|
||||
self._state = None
|
||||
self._attr_state = None
|
||||
|
Loading…
x
Reference in New Issue
Block a user