mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Add some type hints for webhook component (#52895)
* Add some type hints * Fix type hint * Address comment * Make pylint happy
This commit is contained in:
parent
2b6a3716e8
commit
40549d9d2f
@ -1,6 +1,10 @@
|
|||||||
"""Webhooks for Home Assistant."""
|
"""Webhooks for Home Assistant."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Awaitable
|
||||||
import logging
|
import logging
|
||||||
import secrets
|
import secrets
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
from aiohttp.web import Request, Response
|
from aiohttp.web import Request, Response
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -8,7 +12,7 @@ import voluptuous as vol
|
|||||||
from homeassistant.components import websocket_api
|
from homeassistant.components import websocket_api
|
||||||
from homeassistant.components.http.view import HomeAssistantView
|
from homeassistant.components.http.view import HomeAssistantView
|
||||||
from homeassistant.const import HTTP_OK
|
from homeassistant.const import HTTP_OK
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.network import get_url
|
from homeassistant.helpers.network import get_url
|
||||||
from homeassistant.loader import bind_hass
|
from homeassistant.loader import bind_hass
|
||||||
from homeassistant.util.aiohttp import MockRequest
|
from homeassistant.util.aiohttp import MockRequest
|
||||||
@ -28,7 +32,13 @@ SCHEMA_WS_LIST = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
|
|||||||
|
|
||||||
@callback
|
@callback
|
||||||
@bind_hass
|
@bind_hass
|
||||||
def async_register(hass, domain, name, webhook_id, handler):
|
def async_register(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
domain: str,
|
||||||
|
name: str,
|
||||||
|
webhook_id: str,
|
||||||
|
handler: Callable[[HomeAssistant, str, Request], Awaitable[Response | None]],
|
||||||
|
) -> None:
|
||||||
"""Register a webhook."""
|
"""Register a webhook."""
|
||||||
handlers = hass.data.setdefault(DOMAIN, {})
|
handlers = hass.data.setdefault(DOMAIN, {})
|
||||||
|
|
||||||
@ -40,21 +50,21 @@ def async_register(hass, domain, name, webhook_id, handler):
|
|||||||
|
|
||||||
@callback
|
@callback
|
||||||
@bind_hass
|
@bind_hass
|
||||||
def async_unregister(hass, webhook_id):
|
def async_unregister(hass: HomeAssistant, webhook_id: str) -> None:
|
||||||
"""Remove a webhook."""
|
"""Remove a webhook."""
|
||||||
handlers = hass.data.setdefault(DOMAIN, {})
|
handlers = hass.data.setdefault(DOMAIN, {})
|
||||||
handlers.pop(webhook_id, None)
|
handlers.pop(webhook_id, None)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_generate_id():
|
def async_generate_id() -> str:
|
||||||
"""Generate a webhook_id."""
|
"""Generate a webhook_id."""
|
||||||
return secrets.token_hex(32)
|
return secrets.token_hex(32)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@bind_hass
|
@bind_hass
|
||||||
def async_generate_url(hass, webhook_id):
|
def async_generate_url(hass: HomeAssistant, webhook_id: str) -> str:
|
||||||
"""Generate the full URL for a webhook_id."""
|
"""Generate the full URL for a webhook_id."""
|
||||||
return "{}{}".format(
|
return "{}{}".format(
|
||||||
get_url(hass, prefer_external=True, allow_cloud=False),
|
get_url(hass, prefer_external=True, allow_cloud=False),
|
||||||
@ -63,7 +73,7 @@ def async_generate_url(hass, webhook_id):
|
|||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_generate_path(webhook_id):
|
def async_generate_path(webhook_id: str) -> str:
|
||||||
"""Generate the path component for a webhook_id."""
|
"""Generate the path component for a webhook_id."""
|
||||||
return URL_WEBHOOK_PATH.format(webhook_id=webhook_id)
|
return URL_WEBHOOK_PATH.format(webhook_id=webhook_id)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user