Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Vanderbilt (formerly Siemens) SPC alarm systems."""
2 
3 import logging
4 
5 from pyspcwebgw import SpcWebGateway
6 from pyspcwebgw.area import Area
7 from pyspcwebgw.zone import Zone
8 import voluptuous as vol
9 
10 from homeassistant.const import Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import aiohttp_client, discovery
14 from homeassistant.helpers.dispatcher import async_dispatcher_send
15 from homeassistant.helpers.typing import ConfigType
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 CONF_WS_URL = "ws_url"
20 CONF_API_URL = "api_url"
21 
22 DOMAIN = "spc"
23 DATA_API = "spc_api"
24 
25 SIGNAL_UPDATE_ALARM = "spc_update_alarm_{}"
26 SIGNAL_UPDATE_SENSOR = "spc_update_sensor_{}"
27 
28 CONFIG_SCHEMA = vol.Schema(
29  {
30  DOMAIN: vol.Schema(
31  {
32  vol.Required(CONF_WS_URL): cv.string,
33  vol.Required(CONF_API_URL): cv.string,
34  }
35  )
36  },
37  extra=vol.ALLOW_EXTRA,
38 )
39 
40 
41 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
42  """Set up the SPC component."""
43 
44  async def async_update_callback(spc_object):
45  if isinstance(spc_object, Area):
46  async_dispatcher_send(hass, SIGNAL_UPDATE_ALARM.format(spc_object.id))
47  elif isinstance(spc_object, Zone):
48  async_dispatcher_send(hass, SIGNAL_UPDATE_SENSOR.format(spc_object.id))
49 
50  session = aiohttp_client.async_get_clientsession(hass)
51 
52  spc = SpcWebGateway(
53  loop=hass.loop,
54  session=session,
55  api_url=config[DOMAIN].get(CONF_API_URL),
56  ws_url=config[DOMAIN].get(CONF_WS_URL),
57  async_callback=async_update_callback,
58  )
59 
60  hass.data[DATA_API] = spc
61 
62  if not await spc.async_load_parameters():
63  _LOGGER.error("Failed to load area/zone information from SPC")
64  return False
65 
66  # add sensor devices for each zone (typically motion/fire/door sensors)
67  hass.async_create_task(
68  discovery.async_load_platform(hass, Platform.BINARY_SENSOR, DOMAIN, {}, config)
69  )
70 
71  # create a separate alarm panel for each area
72  hass.async_create_task(
73  discovery.async_load_platform(
74  hass, Platform.ALARM_CONTROL_PANEL, DOMAIN, {}, config
75  )
76  )
77 
78  # start listening for incoming events over websocket
79  spc.start()
80 
81  return True
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:41
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193