Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The sms component."""
2 
3 import logging
4 
5 import voluptuous as vol
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_DEVICE, CONF_NAME, Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryNotReady
11 from homeassistant.helpers import config_validation as cv, discovery
12 from homeassistant.helpers.typing import ConfigType
13 
14 from .const import (
15  CONF_BAUD_SPEED,
16  DEFAULT_BAUD_SPEED,
17  DOMAIN,
18  GATEWAY,
19  HASS_CONFIG,
20  NETWORK_COORDINATOR,
21  SIGNAL_COORDINATOR,
22  SMS_GATEWAY,
23 )
24 from .coordinator import NetworkCoordinator, SignalCoordinator
25 from .gateway import create_sms_gateway
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 PLATFORMS = [Platform.SENSOR]
30 
31 SMS_CONFIG_SCHEMA = {vol.Required(CONF_DEVICE): cv.isdevice}
32 
33 CONFIG_SCHEMA = vol.Schema(
34  {
35  DOMAIN: vol.Schema(
36  vol.All(
37  cv.deprecated(CONF_DEVICE),
38  SMS_CONFIG_SCHEMA,
39  ),
40  )
41  },
42  extra=vol.ALLOW_EXTRA,
43 )
44 
45 
46 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
47  """Configure Gammu state machine."""
48  hass.data.setdefault(DOMAIN, {})
49  hass.data[HASS_CONFIG] = config
50  return True
51 
52 
53 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
54  """Configure Gammu state machine."""
55 
56  device = entry.data[CONF_DEVICE]
57  connection_mode = "at"
58  baud_speed = entry.data.get(CONF_BAUD_SPEED, DEFAULT_BAUD_SPEED)
59  if baud_speed != DEFAULT_BAUD_SPEED:
60  connection_mode += baud_speed
61  config = {"Device": device, "Connection": connection_mode}
62  _LOGGER.debug("Connecting mode:%s", connection_mode)
63  gateway = await create_sms_gateway(config, hass)
64  if not gateway:
65  raise ConfigEntryNotReady(f"Cannot find device {device}")
66 
67  signal_coordinator = SignalCoordinator(hass, gateway)
68  network_coordinator = NetworkCoordinator(hass, gateway)
69 
70  # Fetch initial data so we have data when entities subscribe
71  await signal_coordinator.async_config_entry_first_refresh()
72  await network_coordinator.async_config_entry_first_refresh()
73 
74  hass.data.setdefault(DOMAIN, {})
75  hass.data[DOMAIN][SMS_GATEWAY] = {
76  SIGNAL_COORDINATOR: signal_coordinator,
77  NETWORK_COORDINATOR: network_coordinator,
78  GATEWAY: gateway,
79  }
80 
81  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
82 
83  # set up notify platform, no entry support for notify component yet,
84  # have to use discovery to load platform.
85  hass.async_create_task(
86  discovery.async_load_platform(
87  hass,
88  Platform.NOTIFY,
89  DOMAIN,
90  {CONF_NAME: DOMAIN},
91  hass.data[HASS_CONFIG],
92  )
93  )
94  return True
95 
96 
97 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
98  """Unload a config entry."""
99  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
100  if unload_ok:
101  gateway = hass.data[DOMAIN].pop(SMS_GATEWAY)[GATEWAY]
102  await gateway.terminate_async()
103 
104  return unload_ok
def create_sms_gateway(config, hass)
Definition: gateway.py:198
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:46
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:97
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:53