Improve type hints in webhook implementations (#121588)

This commit is contained in:
epenet 2024-07-09 12:05:23 +02:00 committed by GitHub
parent 7aada39b77
commit 9ca398ef82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 39 additions and 18 deletions

View File

@ -89,7 +89,9 @@ async def async_setup(hass: HomeAssistant, hass_config: ConfigType) -> bool:
return True return True
async def handle_webhook(hass, webhook_id, request): async def handle_webhook(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> web.Response:
"""Handle incoming webhook from Geofency.""" """Handle incoming webhook from Geofency."""
try: try:
data = WEBHOOK_SCHEMA(dict(await request.post())) data = WEBHOOK_SCHEMA(dict(await request.post()))

View File

@ -55,7 +55,9 @@ WEBHOOK_SCHEMA = vol.Schema(
) )
async def handle_webhook(hass, webhook_id, request): async def handle_webhook(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> web.Response:
"""Handle incoming webhook with GPSLogger request.""" """Handle incoming webhook with GPSLogger request."""
try: try:
data = WEBHOOK_SCHEMA(dict(await request.post())) data = WEBHOOK_SCHEMA(dict(await request.post()))

View File

@ -61,7 +61,9 @@ WEBHOOK_SCHEMA = vol.All(
) )
async def handle_webhook(hass, webhook_id, request): async def handle_webhook(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> web.Response:
"""Handle incoming webhook from Locative.""" """Handle incoming webhook from Locative."""
try: try:
data = WEBHOOK_SCHEMA(dict(await request.post())) data = WEBHOOK_SCHEMA(dict(await request.post()))

View File

@ -5,6 +5,7 @@ import hmac
import json import json
import logging import logging
from aiohttp import web
import voluptuous as vol import voluptuous as vol
from homeassistant.components import webhook from homeassistant.components import webhook
@ -48,13 +49,15 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True return True
async def handle_webhook(hass, webhook_id, request): async def handle_webhook(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> None:
"""Handle incoming webhook with Mailgun inbound messages.""" """Handle incoming webhook with Mailgun inbound messages."""
body = await request.text() body = await request.text()
try: try:
data = json.loads(body) if body else {} data = json.loads(body) if body else {}
except ValueError: except ValueError:
return None return
if ( if (
isinstance(data, dict) isinstance(data, dict)

View File

@ -38,7 +38,7 @@ async def async_handle_webhook(
data = await request.json() data = await request.json()
except ValueError as err: except ValueError as err:
_LOGGER.error("Error in data: %s", err) _LOGGER.error("Error in data: %s", err)
return None return
_LOGGER.debug("Got webhook data: %s", data) _LOGGER.debug("Got webhook data: %s", data)

View File

@ -5,7 +5,7 @@ import json
import logging import logging
import re import re
from aiohttp.web import json_response from aiohttp import web
import voluptuous as vol import voluptuous as vol
from homeassistant.components import cloud, mqtt, webhook from homeassistant.components import cloud, mqtt, webhook
@ -153,7 +153,9 @@ async def async_connect_mqtt(hass, component):
return True return True
async def handle_webhook(hass, webhook_id, request): async def handle_webhook(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> web.Response:
"""Handle webhook callback. """Handle webhook callback.
iOS sets the "topic" as part of the payload. iOS sets the "topic" as part of the payload.
@ -166,7 +168,7 @@ async def handle_webhook(hass, webhook_id, request):
message = await request.json() message = await request.json()
except ValueError: except ValueError:
_LOGGER.warning("Received invalid JSON from OwnTracks") _LOGGER.warning("Received invalid JSON from OwnTracks")
return json_response([]) return web.json_response([])
# Android doesn't populate topic # Android doesn't populate topic
if "topic" not in message: if "topic" not in message:
@ -183,7 +185,7 @@ async def handle_webhook(hass, webhook_id, request):
" set a username in Connection -> Identification" " set a username in Connection -> Identification"
) )
# Keep it as a 200 response so the incorrect packet is discarded # Keep it as a 200 response so the incorrect packet is discarded
return json_response([]) return web.json_response([])
async_dispatcher_send(hass, DOMAIN, hass, context, message) async_dispatcher_send(hass, DOMAIN, hass, context, message)
@ -200,7 +202,7 @@ async def handle_webhook(hass, webhook_id, request):
] ]
if message["_type"] == "encrypted" and context.secret: if message["_type"] == "encrypted" and context.secret:
return json_response( return web.json_response(
{ {
"_type": "encrypted", "_type": "encrypted",
"data": encrypt_message( "data": encrypt_message(
@ -209,7 +211,7 @@ async def handle_webhook(hass, webhook_id, request):
} }
) )
return json_response(response) return web.json_response(response)
class OwnTracksContext: class OwnTracksContext:

View File

@ -184,7 +184,9 @@ async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> Non
await hass.config_entries.async_reload(entry.entry_id) await hass.config_entries.async_reload(entry.entry_id)
async def handle_webhook(hass, webhook_id, request): async def handle_webhook(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> web.Response | None:
"""Handle incoming webhook from Plaato.""" """Handle incoming webhook from Plaato."""
try: try:
data = WEBHOOK_SCHEMA(await request.json()) data = WEBHOOK_SCHEMA(await request.json())

View File

@ -3,6 +3,7 @@
import asyncio import asyncio
import logging import logging
from aiohttp import web
from httpx import ConnectTimeout from httpx import ConnectTimeout
from pypoint import PointSession from pypoint import PointSession
import voluptuous as vol import voluptuous as vol
@ -158,13 +159,15 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return unload_ok return unload_ok
async def handle_webhook(hass, webhook_id, request): async def handle_webhook(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> None:
"""Handle webhook callback.""" """Handle webhook callback."""
try: try:
data = await request.json() data = await request.json()
_LOGGER.debug("Webhook %s: %s", webhook_id, data) _LOGGER.debug("Webhook %s: %s", webhook_id, data)
except ValueError: except ValueError:
return None return
if isinstance(data, dict): if isinstance(data, dict):
data["webhook_id"] = webhook_id data["webhook_id"] = webhook_id

View File

@ -5,6 +5,7 @@ from __future__ import annotations
import logging import logging
import secrets import secrets
from aiohttp import web
from toonapi import Status, Toon, ToonError from toonapi import Status, Toon, ToonError
from homeassistant.components import cloud, webhook from homeassistant.components import cloud, webhook
@ -98,7 +99,7 @@ class ToonDataUpdateCoordinator(DataUpdateCoordinator[Status]):
) )
async def handle_webhook( async def handle_webhook(
self, hass: HomeAssistant, webhook_id: str, request self, hass: HomeAssistant, webhook_id: str, request: web.Request
) -> None: ) -> None:
"""Handle webhook callback.""" """Handle webhook callback."""
try: try:

View File

@ -56,7 +56,9 @@ WEBHOOK_SCHEMA = vol.Schema(
) )
async def handle_webhook(hass, webhook_id, request): async def handle_webhook(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> web.Response:
"""Handle incoming webhook with Traccar Client request.""" """Handle incoming webhook with Traccar Client request."""
try: try:
data = WEBHOOK_SCHEMA(dict(request.query)) data = WEBHOOK_SCHEMA(dict(request.query))

View File

@ -46,7 +46,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True return True
async def handle_webhook(hass, webhook_id, request): async def handle_webhook(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> web.Response:
"""Handle incoming webhook from Twilio for inbound messages and calls.""" """Handle incoming webhook from Twilio for inbound messages and calls."""
data = dict(await request.post()) data = dict(await request.post())
data["webhook_id"] = webhook_id data["webhook_id"] = webhook_id