1 """Access point for the HomematicIP Cloud component."""
3 from __future__
import annotations
6 from collections.abc
import Callable
10 from homematicip.aio.auth
import AsyncAuth
11 from homematicip.aio.home
import AsyncHome
12 from homematicip.base.base_connection
import HmipConnectionError
13 from homematicip.base.enums
import EventType
20 from .const
import HMIPC_AUTHTOKEN, HMIPC_HAPID, HMIPC_NAME, HMIPC_PIN, PLATFORMS
21 from .errors
import HmipcConnectionError
23 _LOGGER = logging.getLogger(__name__)
27 """Manages HomematicIP client registration."""
31 def __init__(self, hass: HomeAssistant, config: dict[str, str]) ->
None:
32 """Initialize HomematicIP Cloud client registration."""
37 """Connect to HomematicIP for registration."""
42 except HmipcConnectionError:
44 return self.
authauth
is not None
47 """Check blue butten has been pressed."""
49 return await self.
authauth.isRequestAcknowledged()
50 except HmipConnectionError:
54 """Register client at HomematicIP."""
56 authtoken = await self.
authauth.requestAuthToken()
57 await self.
authauth.confirmAuthToken(authtoken)
58 except HmipConnectionError:
62 async
def get_auth(self, hass: HomeAssistant, hapid, pin):
63 """Create a HomematicIP access point object."""
66 await auth.init(hapid)
69 await auth.connectionRequest(
"HomeAssistant")
70 except HmipConnectionError:
76 """Manages HomematicIP HTTP and WebSocket connection."""
80 def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) ->
None:
81 """Initialize HomematicIP Cloud connection."""
86 self.
_retry_task_retry_task: asyncio.Task |
None =
None
90 self.reset_connection_listener: Callable |
None =
None
93 """Initialize connection."""
101 except HmipcConnectionError
as err:
102 raise ConfigEntryNotReady
from err
103 except Exception
as err:
104 _LOGGER.error(
"Error connecting with HomematicIP Cloud: %s", err)
108 "Connected to HomematicIP with HAP %s", self.
config_entryconfig_entry.unique_id
111 await self.
hasshass.config_entries.async_forward_entry_setups(
119 """Async update the home device.
121 Triggered when the HMIP HOME_CHANGED event has fired.
122 There are several occasions for this event to happen.
123 1. We are interested to check whether the access point
124 is still connected. If not, entity state changes cannot
125 be forwarded to hass. So if access point is disconnected all devices
126 are set to unavailable.
127 2. We need to update home including devices and groups after a reconnect.
128 3. We need to update home without devices and groups in all other cases.
131 if not self.
homehome.connected:
132 _LOGGER.error(
"HMIP access point has lost connection with the cloud")
141 job = self.
hasshass.async_create_task(self.
get_stateget_state())
147 """Create an entity or a group."""
148 is_device = EventType(kwargs[
"event_type"]) == EventType.DEVICE_ADDED
152 """Delay entity creation to allow the user to enter a device name."""
154 await asyncio.sleep(30)
155 await self.
hasshass.config_entries.async_reload(self.
config_entryconfig_entry.entry_id)
158 """Update HMIP state and tell Home Assistant."""
159 await self.
homehome.get_current_state()
163 """Execute when get_state coroutine has finished."""
166 except HmipConnectionError:
169 _LOGGER.error(
"Updating state after HMIP access point reconnect failed")
170 self.
hasshass.async_create_task(self.
homehome.disable_events())
173 """Set all devices to unavailable and tell Home Assistant."""
174 for device
in self.
homehome.devices:
175 device.unreach =
True
179 """Signal all devices to update their state."""
180 for device
in self.
homehome.devices:
181 device.fire_update_event()
184 """Start WebSocket connection."""
187 retry_delay = 2 **
min(tries, 8)
190 await self.
homehome.get_current_state()
191 hmip_events = await self.
homehome.enable_events()
194 except HmipConnectionError:
197 "Error connecting to HomematicIP with HAP %s. "
198 "Retrying in %d seconds"
211 asyncio.sleep(retry_delay)
214 except asyncio.CancelledError:
218 """Close the websocket connection."""
222 await self.
homehome.disable_events()
223 _LOGGER.debug(
"Closed connection to HomematicIP cloud server")
224 await self.
hasshass.config_entries.async_unload_platforms(
232 """Wrap the call to async_reset.
234 Used as an argument to EventBus.async_listen_once.
238 "Reset connection to access point id %s", self.
config_entryconfig_entry.unique_id
245 authtoken: str |
None,
248 """Create a HomematicIP access point object."""
254 home.modelType =
"HomematicIP Cloud Home"
256 home.set_auth_token(authtoken)
258 await home.init(hapid)
259 await home.get_current_state()
260 except HmipConnectionError
as err:
261 raise HmipcConnectionError
from err
bool async_checkbutton(self)
def get_auth(self, HomeAssistant hass, hapid, pin)
None __init__(self, HomeAssistant hass, dict[str, str] config)
AsyncHome get_hap(self, HomeAssistant hass, str|None hapid, str|None authtoken, str|None name)
None __init__(self, HomeAssistant hass, ConfigEntry config_entry)
None shutdown(self, event)
None async_create_entity_lazy(self, is_device=True)
None set_all_to_unavailable(self)
None async_update(self, *args, **kwargs)
None async_create_entity(self, *args, **kwargs)
None get_state_finished(self, future)
bool async_setup(self, int tries=0)
web.Response get(self, web.Request request, str config_key)
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)