Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Streamlabs Water Monitor devices."""
2 
3 from streamlabswater.streamlabswater import StreamlabsClient
4 import voluptuous as vol
5 
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import CONF_API_KEY, Platform
8 from homeassistant.core import HomeAssistant, ServiceCall
10 
11 from .const import DOMAIN
12 from .coordinator import StreamlabsCoordinator
13 
14 ATTR_AWAY_MODE = "away_mode"
15 SERVICE_SET_AWAY_MODE = "set_away_mode"
16 AWAY_MODE_AWAY = "away"
17 AWAY_MODE_HOME = "home"
18 
19 CONF_LOCATION_ID = "location_id"
20 
21 ISSUE_PLACEHOLDER = {"url": "/config/integrations/dashboard/add?domain=streamlabswater"}
22 
23 SET_AWAY_MODE_SCHEMA = vol.Schema(
24  {
25  vol.Required(ATTR_AWAY_MODE): vol.In([AWAY_MODE_AWAY, AWAY_MODE_HOME]),
26  vol.Optional(CONF_LOCATION_ID): cv.string,
27  }
28 )
29 
30 PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
31 
32 
33 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
34  """Set up StreamLabs from a config entry."""
35 
36  api_key = entry.data[CONF_API_KEY]
37  client = StreamlabsClient(api_key)
38  coordinator = StreamlabsCoordinator(hass, client)
39 
40  await coordinator.async_config_entry_first_refresh()
41 
42  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
43  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
44 
45  def set_away_mode(service: ServiceCall) -> None:
46  """Set the StreamLabsWater Away Mode."""
47  away_mode = service.data.get(ATTR_AWAY_MODE)
48  location_id = service.data.get(CONF_LOCATION_ID) or list(coordinator.data)[0]
49  client.update_location(location_id, away_mode)
50 
51  hass.services.async_register(
52  DOMAIN, SERVICE_SET_AWAY_MODE, set_away_mode, schema=SET_AWAY_MODE_SCHEMA
53  )
54 
55  return True
56 
57 
58 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
59  """Unload a config entry."""
60  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
61  hass.data[DOMAIN].pop(entry.entry_id)
62 
63  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:33
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:58