Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Smappee integration."""
2 
3 from pysmappee import Smappee, helper, mqtt
4 import voluptuous as vol
5 
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import (
8  CONF_CLIENT_ID,
9  CONF_CLIENT_SECRET,
10  CONF_IP_ADDRESS,
11  CONF_PLATFORM,
12 )
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv
15 from homeassistant.helpers.typing import ConfigType
16 from homeassistant.util import Throttle
17 
18 from . import api, config_flow
19 from .const import (
20  AUTHORIZE_URL,
21  CONF_SERIALNUMBER,
22  DOMAIN,
23  MIN_TIME_BETWEEN_UPDATES,
24  PLATFORMS,
25  TOKEN_URL,
26 )
27 
28 type SmappeeConfigEntry = ConfigEntry[SmappeeBase]
29 
30 CONFIG_SCHEMA = vol.Schema(
31  {
32  DOMAIN: vol.Schema(
33  {
34  vol.Required(CONF_CLIENT_ID): cv.string,
35  vol.Required(CONF_CLIENT_SECRET): cv.string,
36  }
37  )
38  },
39  extra=vol.ALLOW_EXTRA,
40 )
41 
42 
43 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
44  """Set up the Smappee component."""
45  hass.data[DOMAIN] = {}
46 
47  if DOMAIN not in config:
48  return True
49 
50  client_id = config[DOMAIN][CONF_CLIENT_ID]
51  hass.data[DOMAIN][client_id] = {}
52 
53  # decide platform
54  platform = "PRODUCTION"
55  if client_id == "homeassistant_f2":
56  platform = "ACCEPTANCE"
57  elif client_id == "homeassistant_f3":
58  platform = "DEVELOPMENT"
59 
60  hass.data[DOMAIN][CONF_PLATFORM] = platform
61 
62  config_flow.SmappeeFlowHandler.async_register_implementation(
63  hass,
64  config_entry_oauth2_flow.LocalOAuth2Implementation(
65  hass,
66  DOMAIN,
67  config[DOMAIN][CONF_CLIENT_ID],
68  config[DOMAIN][CONF_CLIENT_SECRET],
69  AUTHORIZE_URL[platform],
70  TOKEN_URL[platform],
71  ),
72  )
73 
74  return True
75 
76 
77 async def async_setup_entry(hass: HomeAssistant, entry: SmappeeConfigEntry) -> bool:
78  """Set up Smappee from a zeroconf or config entry."""
79  if CONF_IP_ADDRESS in entry.data:
80  if helper.is_smappee_genius(entry.data[CONF_SERIALNUMBER]):
81  # next generation: local mqtt broker
82  smappee_mqtt = mqtt.SmappeeLocalMqtt(
83  serial_number=entry.data[CONF_SERIALNUMBER]
84  )
85  await hass.async_add_executor_job(smappee_mqtt.start_and_wait_for_config)
86  smappee = Smappee(
87  api=smappee_mqtt, serialnumber=entry.data[CONF_SERIALNUMBER]
88  )
89  else:
90  # legacy devices through local api
91  smappee_api = api.api.SmappeeLocalApi(ip=entry.data[CONF_IP_ADDRESS])
92  smappee = Smappee(
93  api=smappee_api, serialnumber=entry.data[CONF_SERIALNUMBER]
94  )
95  await hass.async_add_executor_job(smappee.load_local_service_location)
96  else:
97  implementation = (
98  await config_entry_oauth2_flow.async_get_config_entry_implementation(
99  hass, entry
100  )
101  )
102 
103  smappee_api = api.ConfigEntrySmappeeApi(hass, entry, implementation)
104 
105  smappee = Smappee(api=smappee_api)
106  await hass.async_add_executor_job(smappee.load_service_locations)
107 
108  entry.runtime_data = SmappeeBase(hass, smappee)
109 
110  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
111 
112  return True
113 
114 
115 async def async_unload_entry(hass: HomeAssistant, entry: SmappeeConfigEntry) -> bool:
116  """Unload a config entry."""
117  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
118 
119 
121  """An object to hold the PySmappee instance."""
122 
123  def __init__(self, hass: HomeAssistant, smappee: Smappee) -> None:
124  """Initialize the Smappee API wrapper class."""
125  self.hasshass = hass
126  self.smappeesmappee = smappee
127 
128  @Throttle(MIN_TIME_BETWEEN_UPDATES)
129  async def async_update(self) -> None:
130  """Update all Smappee trends and appliance states."""
131  await self.hasshass.async_add_executor_job(
132  self.smappeesmappee.update_trends_and_appliance_states
133  )
None __init__(self, HomeAssistant hass, Smappee smappee)
Definition: __init__.py:123
bool async_setup_entry(HomeAssistant hass, SmappeeConfigEntry entry)
Definition: __init__.py:77
bool async_unload_entry(HomeAssistant hass, SmappeeConfigEntry entry)
Definition: __init__.py:115
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:43