Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The JuiceNet integration."""
2 
3 from datetime import timedelta
4 import logging
5 
6 import aiohttp
7 from pyjuicenet import Api, TokenError
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
11 from homeassistant.const import CONF_ACCESS_TOKEN, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryNotReady
14 from homeassistant.helpers import config_validation as cv
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 from homeassistant.helpers.typing import ConfigType
17 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
18 
19 from .const import DOMAIN, JUICENET_API, JUICENET_COORDINATOR
20 from .device import JuiceNetApi
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 PLATFORMS = [Platform.NUMBER, Platform.SENSOR, Platform.SWITCH]
25 
26 CONFIG_SCHEMA = vol.Schema(
27  vol.All(
28  cv.deprecated(DOMAIN),
29  {DOMAIN: vol.Schema({vol.Required(CONF_ACCESS_TOKEN): cv.string})},
30  ),
31  extra=vol.ALLOW_EXTRA,
32 )
33 
34 
35 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
36  """Set up the JuiceNet component."""
37  conf = config.get(DOMAIN)
38  hass.data.setdefault(DOMAIN, {})
39 
40  if not conf:
41  return True
42 
43  hass.async_create_task(
44  hass.config_entries.flow.async_init(
45  DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
46  )
47  )
48  return True
49 
50 
51 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
52  """Set up JuiceNet from a config entry."""
53 
54  config = entry.data
55 
56  session = async_get_clientsession(hass)
57 
58  access_token = config[CONF_ACCESS_TOKEN]
59  api = Api(access_token, session)
60 
61  juicenet = JuiceNetApi(api)
62 
63  try:
64  await juicenet.setup()
65  except TokenError as error:
66  _LOGGER.error("JuiceNet Error %s", error)
67  return False
68  except aiohttp.ClientError as error:
69  _LOGGER.error("Could not reach the JuiceNet API %s", error)
70  raise ConfigEntryNotReady from error
71 
72  if not juicenet.devices:
73  _LOGGER.error("No JuiceNet devices found for this account")
74  return False
75  _LOGGER.debug("%d JuiceNet device(s) found", len(juicenet.devices))
76 
77  async def async_update_data():
78  """Update all device states from the JuiceNet API."""
79  for device in juicenet.devices:
80  await device.update_state(True)
81  return True
82 
83  coordinator = DataUpdateCoordinator(
84  hass,
85  _LOGGER,
86  config_entry=entry,
87  name="JuiceNet",
88  update_method=async_update_data,
89  update_interval=timedelta(seconds=30),
90  )
91 
92  await coordinator.async_config_entry_first_refresh()
93 
94  hass.data[DOMAIN][entry.entry_id] = {
95  JUICENET_API: juicenet,
96  JUICENET_COORDINATOR: coordinator,
97  }
98 
99  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
100 
101  return True
102 
103 
104 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
105  """Unload a config entry."""
106  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
107  if unload_ok:
108  hass.data[DOMAIN].pop(entry.entry_id)
109  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:104
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:51
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:35
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)