Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The pushbullet component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from pushbullet import InvalidKeyError, PushBullet, PushbulletError
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import (
11  CONF_API_KEY,
12  CONF_NAME,
13  EVENT_HOMEASSISTANT_START,
14  Platform,
15 )
16 from homeassistant.core import Event, HomeAssistant
17 from homeassistant.exceptions import ConfigEntryNotReady
18 from homeassistant.helpers import config_validation as cv, discovery
19 from homeassistant.helpers.typing import ConfigType
20 
21 from .api import PushBulletNotificationProvider
22 from .const import DATA_HASS_CONFIG, DOMAIN
23 
24 PLATFORMS = [Platform.SENSOR]
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
29 
30 
31 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
32  """Set up the pushbullet component."""
33 
34  hass.data[DATA_HASS_CONFIG] = config
35  return True
36 
37 
38 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
39  """Set up pushbullet from a config entry."""
40 
41  try:
42  pushbullet = await hass.async_add_executor_job(
43  PushBullet, entry.data[CONF_API_KEY]
44  )
45  except InvalidKeyError:
46  _LOGGER.error("Invalid API key for Pushbullet")
47  return False
48  except PushbulletError as err:
49  raise ConfigEntryNotReady from err
50 
51  pb_provider = PushBulletNotificationProvider(hass, pushbullet)
52  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = pb_provider
53 
54  def start_listener(event: Event) -> None:
55  """Start the listener thread."""
56  _LOGGER.debug("Starting listener for pushbullet")
57  pb_provider.start()
58 
59  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, start_listener)
60 
61  hass.async_create_task(
62  discovery.async_load_platform(
63  hass,
64  Platform.NOTIFY,
65  DOMAIN,
66  {CONF_NAME: entry.data[CONF_NAME], "entry_id": entry.entry_id},
67  hass.data[DATA_HASS_CONFIG],
68  )
69  )
70  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
71 
72  return True
73 
74 
75 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
76  """Unload a config entry."""
77  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
78  pb_provider: PushBulletNotificationProvider = hass.data[DOMAIN].pop(
79  entry.entry_id
80  )
81  await hass.async_add_executor_job(pb_provider.close)
82  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:38
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:75
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:31