Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Schluter DITRA-HEAT integration."""
2 
3 import logging
4 
5 from requests import RequestException, Session
6 from schluter.api import Api
7 from schluter.authenticator import AuthenticationState, Authenticator
8 import voluptuous as vol
9 
10 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import discovery
14 from homeassistant.helpers.typing import ConfigType
15 
16 from .const import DOMAIN
17 
18 _LOGGER = logging.getLogger(__name__)
19 DATA_SCHLUTER_SESSION = "schluter_session"
20 DATA_SCHLUTER_API = "schluter_api"
21 SCHLUTER_CONFIG_FILE = ".schluter.conf"
22 API_TIMEOUT = 10
23 
24 CONFIG_SCHEMA = vol.Schema(
25  {
26  vol.Required(DOMAIN): vol.Schema(
27  {
28  vol.Required(CONF_USERNAME): cv.string,
29  vol.Required(CONF_PASSWORD): cv.string,
30  }
31  )
32  },
33  extra=vol.ALLOW_EXTRA,
34 )
35 
36 
37 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
38  """Set up the Schluter component."""
39  _LOGGER.debug("Starting setup of schluter")
40 
41  conf = config[DOMAIN]
42  api_http_session = Session()
43  api = Api(timeout=API_TIMEOUT, http_session=api_http_session)
44 
45  authenticator = Authenticator(
46  api,
47  conf.get(CONF_USERNAME),
48  conf.get(CONF_PASSWORD),
49  session_id_cache_file=hass.config.path(SCHLUTER_CONFIG_FILE),
50  )
51 
52  authentication = None
53  try:
54  authentication = authenticator.authenticate()
55  except RequestException as ex:
56  _LOGGER.error("Unable to connect to Schluter service: %s", ex)
57  return False
58 
59  state = authentication.state
60 
61  if state == AuthenticationState.AUTHENTICATED:
62  hass.data[DOMAIN] = {
63  DATA_SCHLUTER_API: api,
64  DATA_SCHLUTER_SESSION: authentication.session_id,
65  }
66  discovery.load_platform(hass, Platform.CLIMATE, DOMAIN, {}, config)
67  return True
68  if state == AuthenticationState.BAD_PASSWORD:
69  _LOGGER.error("Invalid password provided")
70  return False
71  if state == AuthenticationState.BAD_EMAIL:
72  _LOGGER.error("Invalid email provided")
73  return False
74 
75  _LOGGER.error("Unknown set up error: %s", state)
76  return False
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:37