Home Assistant Unofficial Reference 2024.12.1
__init__.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 import logging
7 
8 from aiohttp import ClientResponseError
9 from doorbirdpy import DoorBird
10 
11 from homeassistant.const import (
12  CONF_HOST,
13  CONF_NAME,
14  CONF_PASSWORD,
15  CONF_TOKEN,
16  CONF_USERNAME,
17 )
18 from homeassistant.core import HomeAssistant
19 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
20 from homeassistant.helpers import issue_registry as ir
21 from homeassistant.helpers.aiohttp_client import async_get_clientsession
23 from homeassistant.helpers.typing import ConfigType
24 
25 from .const import CONF_EVENTS, DOMAIN, PLATFORMS
26 from .device import ConfiguredDoorBird
27 from .models import DoorBirdConfigEntry, DoorBirdData
28 from .view import DoorBirdRequestView
29 
30 CONF_CUSTOM_URL = "hass_url_override"
31 
32 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
33 
34 _LOGGER = logging.getLogger(__name__)
35 
36 
37 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
38  """Set up the DoorBird component."""
39  # Provide an endpoint for the door stations to call to trigger events
40  hass.http.register_view(DoorBirdRequestView)
41  return True
42 
43 
44 async def async_setup_entry(hass: HomeAssistant, entry: DoorBirdConfigEntry) -> bool:
45  """Set up DoorBird from a config entry."""
46  door_station_config = entry.data
47  config_entry_id = entry.entry_id
48  device_ip = door_station_config[CONF_HOST]
49  username = door_station_config[CONF_USERNAME]
50  password = door_station_config[CONF_PASSWORD]
51  session = async_get_clientsession(hass)
52 
53  device = DoorBird(device_ip, username, password, http_session=session)
54  try:
55  info = await device.info()
56  except ClientResponseError as err:
57  if err.status == HTTPStatus.UNAUTHORIZED:
58  raise ConfigEntryAuthFailed from err
59  raise ConfigEntryNotReady from err
60  except OSError as oserr:
61  raise ConfigEntryNotReady from oserr
62 
63  token: str = door_station_config.get(CONF_TOKEN, config_entry_id)
64  custom_url: str | None = door_station_config.get(CONF_CUSTOM_URL)
65  name: str | None = door_station_config.get(CONF_NAME)
66  events = entry.options.get(CONF_EVENTS, [])
67  event_entity_ids: dict[str, str] = {}
68  door_station = ConfiguredDoorBird(
69  hass, device, name, custom_url, token, event_entity_ids
70  )
71  door_bird_data = DoorBirdData(door_station, info, event_entity_ids)
72  door_station.update_events(events)
73  # Subscribe to doorbell or motion events
74  if not await _async_register_events(hass, door_station, entry):
75  raise ConfigEntryNotReady
76 
77  entry.async_on_unload(entry.add_update_listener(_update_listener))
78  entry.runtime_data = door_bird_data
79  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
80 
81  return True
82 
83 
84 async def async_unload_entry(hass: HomeAssistant, entry: DoorBirdConfigEntry) -> bool:
85  """Unload a config entry."""
86  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
87 
88 
90  hass: HomeAssistant, door_station: ConfiguredDoorBird, entry: DoorBirdConfigEntry
91 ) -> bool:
92  """Register events on device."""
93  issue_id = f"doorbird_schedule_error_{entry.entry_id}"
94  try:
95  await door_station.async_register_events()
96  except ClientResponseError as ex:
97  ir.async_create_issue(
98  hass,
99  DOMAIN,
100  issue_id,
101  severity=ir.IssueSeverity.ERROR,
102  translation_key="error_registering_events",
103  data={"entry_id": entry.entry_id},
104  is_fixable=True,
105  translation_placeholders={
106  "error": str(ex),
107  "name": door_station.name or entry.data[CONF_NAME],
108  },
109  )
110  _LOGGER.debug("Error registering DoorBird events", exc_info=True)
111  return False
112  else:
113  ir.async_delete_issue(hass, DOMAIN, issue_id)
114 
115  return True
116 
117 
118 async def _update_listener(hass: HomeAssistant, entry: DoorBirdConfigEntry) -> None:
119  """Handle options update."""
120  door_station = entry.runtime_data.door_station
121  door_station.update_events(entry.options[CONF_EVENTS])
122  # Subscribe to doorbell or motion events
123  await _async_register_events(hass, door_station, entry)
bool async_unload_entry(HomeAssistant hass, DoorBirdConfigEntry entry)
Definition: __init__.py:84
None _update_listener(HomeAssistant hass, DoorBirdConfigEntry entry)
Definition: __init__.py:118
bool _async_register_events(HomeAssistant hass, ConfiguredDoorBird door_station, DoorBirdConfigEntry entry)
Definition: __init__.py:91
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:37
bool async_setup_entry(HomeAssistant hass, DoorBirdConfigEntry entry)
Definition: __init__.py:44
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)