Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Prosegur Alarm integration."""
2 
3 import logging
4 
5 from pyprosegur.auth import Auth
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_COUNTRY, CONF_PASSWORD, CONF_USERNAME, Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
11 from homeassistant.helpers import aiohttp_client
12 
13 from .const import DOMAIN
14 
15 PLATFORMS = [Platform.ALARM_CONTROL_PANEL, Platform.CAMERA]
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 
20 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
21  """Set up Prosegur Alarm from a config entry."""
22  try:
23  session = aiohttp_client.async_get_clientsession(hass)
24  hass.data.setdefault(DOMAIN, {})
25  hass.data[DOMAIN][entry.entry_id] = Auth(
26  session,
27  entry.data[CONF_USERNAME],
28  entry.data[CONF_PASSWORD],
29  entry.data[CONF_COUNTRY],
30  )
31  await hass.data[DOMAIN][entry.entry_id].login()
32 
33  except ConnectionRefusedError as error:
34  _LOGGER.error("Configured credential are invalid, %s", error)
35 
36  raise ConfigEntryAuthFailed from error
37 
38  except ConnectionError as error:
39  _LOGGER.error("Could not connect with Prosegur backend: %s", error)
40  raise ConfigEntryNotReady from error
41 
42  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
43 
44  return True
45 
46 
47 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
48  """Unload a config entry."""
49  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
50  if unload_ok:
51  hass.data[DOMAIN].pop(entry.entry_id)
52 
53  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:20
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:47