Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for controlling Sisyphus Kinetic Art Tables."""
2 
3 import asyncio
4 import logging
5 
6 from sisyphus_control import Table
7 import voluptuous as vol
8 
9 from homeassistant.const import CONF_HOST, CONF_NAME, EVENT_HOMEASSISTANT_STOP, Platform
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.aiohttp_client import async_get_clientsession
13 from homeassistant.helpers.discovery import async_load_platform
14 from homeassistant.helpers.typing import ConfigType
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 DATA_SISYPHUS = "sisyphus"
19 DOMAIN = "sisyphus"
20 
21 AUTODETECT_SCHEMA = vol.Schema({})
22 
23 TABLE_SCHEMA = vol.Schema(
24  {vol.Required(CONF_NAME): cv.string, vol.Required(CONF_HOST): cv.string}
25 )
26 
27 TABLES_SCHEMA = vol.Schema([TABLE_SCHEMA])
28 
29 CONFIG_SCHEMA = vol.Schema(
30  {DOMAIN: vol.Any(AUTODETECT_SCHEMA, TABLES_SCHEMA)}, extra=vol.ALLOW_EXTRA
31 )
32 
33 # Silence these loggers by default. Their INFO level is super chatty and we
34 # only need error-level logging from the integration itself by default.
35 logging.getLogger("socketio.client").setLevel(logging.CRITICAL + 1)
36 logging.getLogger("engineio.client").setLevel(logging.CRITICAL + 1)
37 
38 
39 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
40  """Set up the sisyphus component."""
41 
42  tables = hass.data.setdefault(DATA_SISYPHUS, {})
43  table_configs = config[DOMAIN]
44  session = async_get_clientsession(hass)
45 
46  async def add_table(host, name=None):
47  """Add platforms for a single table with the given hostname."""
48  tables[host] = TableHolder(hass, session, host, name)
49 
50  hass.async_create_task(
51  async_load_platform(hass, Platform.LIGHT, DOMAIN, {CONF_HOST: host}, config)
52  )
53  hass.async_create_task(
55  hass, Platform.MEDIA_PLAYER, DOMAIN, {CONF_HOST: host}, config
56  )
57  )
58 
59  if isinstance(table_configs, dict): # AUTODETECT_SCHEMA
60  for ip_address in await Table.find_table_ips(session):
61  await add_table(ip_address)
62  else: # TABLES_SCHEMA
63  for conf in table_configs:
64  await add_table(conf[CONF_HOST], conf[CONF_NAME])
65 
66  async def close_tables(*args):
67  """Close all table objects."""
68  tasks = [table.close() for table in tables.values()]
69  if tasks:
70  await asyncio.wait(tasks)
71 
72  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, close_tables)
73 
74  return True
75 
76 
78  """Holds table objects and makes them available to platforms."""
79 
80  def __init__(self, hass, session, host, name):
81  """Initialize the table holder."""
82  self._hass_hass = hass
83  self._session_session = session
84  self._host_host = host
85  self._name_name = name
86  self._table_table = None
87  self._table_task_table_task = None
88 
89  @property
90  def available(self):
91  """Return true if the table is responding to heartbeats."""
92  if self._table_task_table_task and self._table_task_table_task.done():
93  return self._table_task_table_task.result().is_connected
94  return False
95 
96  @property
97  def name(self):
98  """Return the name of the table."""
99  return self._name_name
100 
101  async def get_table(self):
102  """Return the Table held by this holder, connecting to it if needed."""
103  if self._table_table:
104  return self._table_table
105 
106  if not self._table_task_table_task:
107  self._table_task_table_task = self._hass_hass.async_create_task(self._connect_table_connect_table())
108 
109  return await self._table_task_table_task
110 
111  async def _connect_table(self):
112  try:
113  self._table_table = await Table.connect(self._host_host, self._session_session)
114  if self._name_name is None:
115  self._name_name = self._table_table.name
116  _LOGGER.debug("Connected to %s at %s", self._name_name, self._host_host)
117  return self._table_table
118  finally:
119  self._table_task_table_task = None
120 
121  async def close(self):
122  """Close the table held by this holder, if any."""
123  if self._table_table:
124  await self._table_table.close()
125  self._table_table = None
126  self._table_task_table_task = None
def __init__(self, hass, session, host, name)
Definition: __init__.py:80
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:39
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)
None async_load_platform(core.HomeAssistant hass, Platform|str component, str platform, DiscoveryInfoType|None discovered, ConfigType hass_config)
Definition: discovery.py:152