Home Assistant Unofficial Reference 2024.12.1
view.py
Go to the documentation of this file.
1 """Support for DoorBird devices."""
2 
3 from __future__ import annotations
4 
5 from http import HTTPStatus
6 
7 from aiohttp import web
8 
9 from homeassistant.components.http import KEY_HASS, HomeAssistantView
10 from homeassistant.helpers.dispatcher import async_dispatcher_send
11 
12 from .const import API_URL, DOMAIN
13 from .util import get_door_station_by_token
14 
15 
16 class DoorBirdRequestView(HomeAssistantView):
17  """Provide a page for the device to call."""
18 
19  requires_auth = False
20  url = API_URL
21  name = API_URL[1:].replace("/", ":")
22  extra_urls = [API_URL + "/{event}"]
23 
24  async def get(self, request: web.Request, event: str) -> web.Response:
25  """Respond to requests from the device."""
26  hass = request.app[KEY_HASS]
27  token: str | None = request.query.get("token")
28  if not token or not (door_station := get_door_station_by_token(hass, token)):
29  return web.Response(
30  status=HTTPStatus.UNAUTHORIZED, text="Invalid token provided."
31  )
32 
33  event_data = door_station.get_event_data(event)
34  #
35  # This integration uses a multiple different events.
36  # It would be a major breaking change to change this to
37  # a single event at this point.
38  #
39  # Do not copy this pattern in the future
40  # for any new integrations.
41  #
42  event_type = f"{DOMAIN}_{event}"
43  hass.bus.async_fire(event_type, event_data)
44  async_dispatcher_send(hass, event_type)
45  return web.Response(text="OK")
web.Response get(self, web.Request request, str event)
Definition: view.py:24
ConfiguredDoorBird|None get_door_station_by_token(HomeAssistant hass, str token)
Definition: util.py:19
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193