Home Assistant Unofficial Reference 2024.12.1
services.py
Go to the documentation of this file.
1 """Services for the Blink integration."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
7 from homeassistant.config_entries import ConfigEntryState
8 from homeassistant.const import ATTR_DEVICE_ID, CONF_PIN
9 from homeassistant.core import HomeAssistant, ServiceCall
10 from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
11 from homeassistant.helpers import config_validation as cv
12 
13 from .const import ATTR_CONFIG_ENTRY_ID, DOMAIN, SERVICE_SEND_PIN
14 from .coordinator import BlinkConfigEntry
15 
16 SERVICE_UPDATE_SCHEMA = vol.Schema(
17  {
18  vol.Required(ATTR_DEVICE_ID): vol.All(cv.ensure_list, [cv.string]),
19  }
20 )
21 SERVICE_SEND_PIN_SCHEMA = vol.Schema(
22  {
23  vol.Required(ATTR_CONFIG_ENTRY_ID): vol.All(cv.ensure_list, [cv.string]),
24  vol.Optional(CONF_PIN): cv.string,
25  }
26 )
27 
28 
29 def setup_services(hass: HomeAssistant) -> None:
30  """Set up the services for the Blink integration."""
31 
32  async def send_pin(call: ServiceCall):
33  """Call blink to send new pin."""
34  config_entry: BlinkConfigEntry | None
35  for entry_id in call.data[ATTR_CONFIG_ENTRY_ID]:
36  if not (config_entry := hass.config_entries.async_get_entry(entry_id)):
38  translation_domain=DOMAIN,
39  translation_key="integration_not_found",
40  translation_placeholders={"target": DOMAIN},
41  )
42  if config_entry.state != ConfigEntryState.LOADED:
43  raise HomeAssistantError(
44  translation_domain=DOMAIN,
45  translation_key="not_loaded",
46  translation_placeholders={"target": config_entry.title},
47  )
48  coordinator = config_entry.runtime_data
49  await coordinator.api.auth.send_auth_key(
50  coordinator.api,
51  call.data[CONF_PIN],
52  )
53 
54  hass.services.async_register(
55  DOMAIN,
56  SERVICE_SEND_PIN,
57  send_pin,
58  schema=SERVICE_SEND_PIN_SCHEMA,
59  )