Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Blink Home Camera System."""
2 
3 from copy import deepcopy
4 import logging
5 from typing import Any
6 
7 from aiohttp import ClientError
8 from blinkpy.auth import Auth
9 from blinkpy.blinkpy import Blink
10 import voluptuous as vol
11 
12 from homeassistant.components import persistent_notification
13 from homeassistant.const import (
14  CONF_FILE_PATH,
15  CONF_FILENAME,
16  CONF_NAME,
17  CONF_PIN,
18  CONF_SCAN_INTERVAL,
19 )
20 from homeassistant.core import HomeAssistant, callback
21 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
22 from homeassistant.helpers import config_validation as cv
23 from homeassistant.helpers.aiohttp_client import async_get_clientsession
24 from homeassistant.helpers.typing import ConfigType
25 
26 from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, PLATFORMS
27 from .coordinator import BlinkConfigEntry, BlinkUpdateCoordinator
28 from .services import setup_services
29 
30 _LOGGER = logging.getLogger(__name__)
31 
32 SERVICE_SAVE_VIDEO_SCHEMA = vol.Schema(
33  {vol.Required(CONF_NAME): cv.string, vol.Required(CONF_FILENAME): cv.string}
34 )
35 SERVICE_SEND_PIN_SCHEMA = vol.Schema({vol.Optional(CONF_PIN): cv.string})
36 SERVICE_SAVE_RECENT_CLIPS_SCHEMA = vol.Schema(
37  {vol.Required(CONF_NAME): cv.string, vol.Required(CONF_FILE_PATH): cv.string}
38 )
39 
40 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
41 
42 
44  hass: HomeAssistant, entry: BlinkConfigEntry, data: dict[str, Any]
45 ) -> None:
46  """Reauth flow wrapper."""
47  entry.async_start_reauth(hass, data=data)
48  persistent_notification.async_create(
49  hass,
50  (
51  "Blink configuration migrated to a new version. Please go to the"
52  " integrations page to re-configure (such as sending a new 2FA key)."
53  ),
54  "Blink Migration",
55  )
56 
57 
58 async def async_migrate_entry(hass: HomeAssistant, entry: BlinkConfigEntry) -> bool:
59  """Handle migration of a previous version config entry."""
60  _LOGGER.debug("Migrating from version %s", entry.version)
61  data = {**entry.data}
62  if entry.version == 1:
63  data.pop("login_response", None)
64  await _reauth_flow_wrapper(hass, entry, data)
65  return False
66  if entry.version == 2:
67  await _reauth_flow_wrapper(hass, entry, data)
68  return False
69  return True
70 
71 
72 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
73  """Set up Blink."""
74 
75  setup_services(hass)
76 
77  return True
78 
79 
80 async def async_setup_entry(hass: HomeAssistant, entry: BlinkConfigEntry) -> bool:
81  """Set up Blink via config entry."""
83  session = async_get_clientsession(hass)
84  blink = Blink(session=session)
85  auth_data = deepcopy(dict(entry.data))
86  blink.auth = Auth(auth_data, no_prompt=True, session=session)
87  blink.refresh_rate = entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
88  coordinator = BlinkUpdateCoordinator(hass, blink)
89 
90  try:
91  await blink.start()
92  except (ClientError, TimeoutError) as ex:
93  raise ConfigEntryNotReady("Can not connect to host") from ex
94 
95  if blink.auth.check_key_required():
96  _LOGGER.debug("Attempting a reauth flow")
97  raise ConfigEntryAuthFailed("Need 2FA for Blink")
98 
99  if not blink.available:
100  raise ConfigEntryNotReady
101 
102  await coordinator.async_config_entry_first_refresh()
103 
104  entry.runtime_data = coordinator
105 
106  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
107 
108  return True
109 
110 
111 @callback
113  hass: HomeAssistant, entry: BlinkConfigEntry
114 ) -> None:
115  options = dict(entry.options)
116  if CONF_SCAN_INTERVAL not in entry.options:
117  options[CONF_SCAN_INTERVAL] = entry.data.get(
118  CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
119  )
120  hass.config_entries.async_update_entry(entry, options=options)
121 
122 
123 async def async_unload_entry(hass: HomeAssistant, entry: BlinkConfigEntry) -> bool:
124  """Unload Blink entry."""
125  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
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)