Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The mütesync integration."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 import logging
7 
8 import mutesync
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers import update_coordinator
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 
16 from .const import DOMAIN, UPDATE_INTERVAL_IN_MEETING, UPDATE_INTERVAL_NOT_IN_MEETING
17 
18 PLATFORMS = [Platform.BINARY_SENSOR]
19 
20 
21 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
22  """Set up mütesync from a config entry."""
23  client = mutesync.PyMutesync(
24  entry.data["token"],
25  entry.data["host"],
27  )
28 
29  async def update_data():
30  """Update the data."""
31  async with asyncio.timeout(2.5):
32  state = await client.get_state()
33 
34  if state["muted"] is None or state["in_meeting"] is None:
35  raise update_coordinator.UpdateFailed("Got invalid response")
36 
37  if state["in_meeting"]:
38  coordinator.update_interval = UPDATE_INTERVAL_IN_MEETING
39  else:
40  coordinator.update_interval = UPDATE_INTERVAL_NOT_IN_MEETING
41 
42  return state
43 
44  coordinator = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = (
45  update_coordinator.DataUpdateCoordinator(
46  hass,
47  logging.getLogger(__name__),
48  config_entry=entry,
49  name=DOMAIN,
50  update_interval=UPDATE_INTERVAL_NOT_IN_MEETING,
51  update_method=update_data,
52  )
53  )
54  await coordinator.async_config_entry_first_refresh()
55 
56  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
57 
58  return True
59 
60 
61 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
62  """Unload a config entry."""
63  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
64  if unload_ok:
65  hass.data[DOMAIN].pop(entry.entry_id)
66 
67  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:21
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:61
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)