1 """Support for DoorBird devices."""
3 from __future__
import annotations
5 from http
import HTTPStatus
8 from aiohttp
import ClientResponseError
9 from doorbirdpy
import DoorBird
25 from .const
import CONF_EVENTS, DOMAIN, PLATFORMS
26 from .device
import ConfiguredDoorBird
27 from .models
import DoorBirdConfigEntry, DoorBirdData
28 from .view
import DoorBirdRequestView
30 CONF_CUSTOM_URL =
"hass_url_override"
32 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
34 _LOGGER = logging.getLogger(__name__)
37 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
38 """Set up the DoorBird component."""
40 hass.http.register_view(DoorBirdRequestView)
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]
53 device = DoorBird(device_ip, username, password, http_session=session)
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
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] = {}
69 hass, device, name, custom_url, token, event_entity_ids
71 door_bird_data =
DoorBirdData(door_station, info, event_entity_ids)
72 door_station.update_events(events)
75 raise ConfigEntryNotReady
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)
85 """Unload a config entry."""
86 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
90 hass: HomeAssistant, door_station: ConfiguredDoorBird, entry: DoorBirdConfigEntry
92 """Register events on device."""
93 issue_id = f
"doorbird_schedule_error_{entry.entry_id}"
95 await door_station.async_register_events()
96 except ClientResponseError
as ex:
97 ir.async_create_issue(
101 severity=ir.IssueSeverity.ERROR,
102 translation_key=
"error_registering_events",
103 data={
"entry_id": entry.entry_id},
105 translation_placeholders={
107 "name": door_station.name
or entry.data[CONF_NAME],
110 _LOGGER.debug(
"Error registering DoorBird events", exc_info=
True)
113 ir.async_delete_issue(hass, DOMAIN, issue_id)
119 """Handle options update."""
120 door_station = entry.runtime_data.door_station
121 door_station.update_events(entry.options[CONF_EVENTS])
bool async_unload_entry(HomeAssistant hass, DoorBirdConfigEntry entry)
None _update_listener(HomeAssistant hass, DoorBirdConfigEntry entry)
bool _async_register_events(HomeAssistant hass, ConfiguredDoorBird door_station, DoorBirdConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_setup_entry(HomeAssistant hass, DoorBirdConfigEntry entry)
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)