1 """The JuiceNet integration."""
3 from datetime
import timedelta
7 from pyjuicenet
import Api, TokenError
8 import voluptuous
as vol
19 from .const
import DOMAIN, JUICENET_API, JUICENET_COORDINATOR
20 from .device
import JuiceNetApi
22 _LOGGER = logging.getLogger(__name__)
24 PLATFORMS = [Platform.NUMBER, Platform.SENSOR, Platform.SWITCH]
26 CONFIG_SCHEMA = vol.Schema(
28 cv.deprecated(DOMAIN),
29 {DOMAIN: vol.Schema({vol.Required(CONF_ACCESS_TOKEN): cv.string})},
31 extra=vol.ALLOW_EXTRA,
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, {})
43 hass.async_create_task(
44 hass.config_entries.flow.async_init(
45 DOMAIN, context={
"source": SOURCE_IMPORT}, data=conf
52 """Set up JuiceNet from a config entry."""
58 access_token = config[CONF_ACCESS_TOKEN]
59 api = Api(access_token, session)
64 await juicenet.setup()
65 except TokenError
as error:
66 _LOGGER.error(
"JuiceNet Error %s", error)
68 except aiohttp.ClientError
as error:
69 _LOGGER.error(
"Could not reach the JuiceNet API %s", error)
70 raise ConfigEntryNotReady
from error
72 if not juicenet.devices:
73 _LOGGER.error(
"No JuiceNet devices found for this account")
75 _LOGGER.debug(
"%d JuiceNet device(s) found", len(juicenet.devices))
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)
88 update_method=async_update_data,
92 await coordinator.async_config_entry_first_refresh()
94 hass.data[DOMAIN][entry.entry_id] = {
95 JUICENET_API: juicenet,
96 JUICENET_COORDINATOR: coordinator,
99 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
105 """Unload a config entry."""
106 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
108 hass.data[DOMAIN].pop(entry.entry_id)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)
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)