11"""The multimatic integration."""
22import asyncio
3- from datetime import timedelta
3+ from datetime import datetime , timedelta
44import logging
55
6+ from pymultimatic .api import ApiError , defaults
7+
68from homeassistant .config_entries import ConfigEntry
79from homeassistant .const import CONF_SCAN_INTERVAL , EVENT_HOMEASSISTANT_STOP
810from homeassistant .core import HomeAssistant
11+ from homeassistant .helpers .event import async_track_time_interval
912from homeassistant .helpers .typing import ConfigType
10- from pymultimatic .api import defaults
1113
1214from .const import (
15+ CONF_APPLICATION ,
1316 CONF_SERIAL_NUMBER ,
1417 COORDINATOR_LIST ,
1518 COORDINATORS ,
1619 DEFAULT_SCAN_INTERVAL ,
1720 DOMAIN ,
21+ FORCE_RELOGIN_TIMEDELTA ,
1822 PLATFORMS ,
19- SERVICES_HANDLER , CONF_APPLICATION ,
23+ RELOGIN_TASK_CLEAN ,
24+ SERVICES_HANDLER ,
2025)
2126from .coordinator import MultimaticApi , MultimaticCoordinator
2227from .service import SERVICES , MultimaticServiceHandler
@@ -71,14 +76,26 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
7176 async def logout (event ):
7277 await api .logout ()
7378
79+ async def force_relogin (time : datetime ):
80+ try :
81+ _LOGGER .debug ("Periodic relogin" )
82+ await api .login (True )
83+ except ApiError :
84+ _LOGGER .debug ("Error during periodic login" , exc_info = True )
85+
86+ hass .data [DOMAIN ][entry .entry_id ][RELOGIN_TASK_CLEAN ] = async_track_time_interval (
87+ hass , force_relogin , FORCE_RELOGIN_TIMEDELTA
88+ )
7489 hass .bus .async_listen_once (EVENT_HOMEASSISTANT_STOP , logout )
7590
7691 await async_setup_service (hass , api , entry )
7792
7893 return True
7994
8095
81- async def async_setup_service (hass , api : MultimaticApi , entry : ConfigEntry ):
96+ async def async_setup_service (
97+ hass : HomeAssistant , api : MultimaticApi , entry : ConfigEntry
98+ ):
8299 """Set up services."""
83100 serial = api .serial if api .fixed_serial else None
84101
@@ -96,7 +113,7 @@ async def async_setup_service(hass, api: MultimaticApi, entry: ConfigEntry):
96113 hass .data [DOMAIN ][entry .entry_id ][SERVICES_HANDLER ] = service_handler
97114
98115
99- async def async_unload_services (hass , entry : ConfigEntry ):
116+ async def async_unload_services (hass : HomeAssistant , entry : ConfigEntry ):
100117 """Remove services when integration is removed."""
101118 service_handler = hass .data [DOMAIN ][entry .entry_id ].get (SERVICES_HANDLER , None )
102119 if service_handler :
@@ -121,6 +138,11 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
121138 )
122139 )
123140 )
141+
142+ relogin_task_clean = hass .data [DOMAIN ][entry .entry_id ][RELOGIN_TASK_CLEAN ]
143+ if relogin_task_clean :
144+ relogin_task_clean ()
145+
124146 if unload_ok :
125147 await async_unload_services (hass , entry )
126148 hass .data [DOMAIN ].pop (entry .entry_id )
@@ -130,7 +152,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
130152 return unload_ok
131153
132154
133- async def async_migrate_entry (hass , config_entry : ConfigEntry ):
155+ async def async_migrate_entry (hass : HomeAssistant , config_entry : ConfigEntry ) -> bool :
134156 """Migrate old entry."""
135157 _LOGGER .debug ("Migrating from version %s" , config_entry .version )
136158 if config_entry .version == 1 :
0 commit comments