Improve type hints for webhook implementation in push (#121589)

Improve type hints in push webhook implementation
This commit is contained in:
epenet 2024-07-09 12:16:23 +02:00 committed by GitHub
parent ba0cd595ff
commit d4cc44d352
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,8 +6,9 @@ import asyncio
from collections import deque from collections import deque
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import cast
import aiohttp from aiohttp import web
import voluptuous as vol import voluptuous as vol
from homeassistant.components import webhook from homeassistant.components import webhook
@ -76,12 +77,14 @@ async def async_setup_platform(
async_add_entities(cameras) async_add_entities(cameras)
async def handle_webhook(hass, webhook_id, request): async def handle_webhook(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> None:
"""Handle incoming webhook POST with image files.""" """Handle incoming webhook POST with image files."""
try: try:
async with asyncio.timeout(5): async with asyncio.timeout(5):
data = dict(await request.post()) data = dict(await request.post())
except (TimeoutError, aiohttp.web.HTTPException) as error: except (TimeoutError, web.HTTPException) as error:
_LOGGER.error("Could not get information from POST <%s>", error) _LOGGER.error("Could not get information from POST <%s>", error)
return return
@ -91,9 +94,8 @@ async def handle_webhook(hass, webhook_id, request):
_LOGGER.warning("Webhook call without POST parameter <%s>", camera.image_field) _LOGGER.warning("Webhook call without POST parameter <%s>", camera.image_field)
return return
await camera.update_image( image_data = cast(web.FileField, data[camera.image_field])
data[camera.image_field].file.read(), data[camera.image_field].filename await camera.update_image(image_data.file.read(), image_data.filename)
)
class PushCamera(Camera): class PushCamera(Camera):