1- """The Sagemcom integration."""
2- import asyncio
1+ """The Sagemcom F@st integration."""
2+ from __future__ import annotations
3+
4+ from dataclasses import dataclass
35from datetime import timedelta
4- import logging
56
67from aiohttp .client_exceptions import ClientError
7- from homeassistant .config_entries import SOURCE_REAUTH , ConfigEntry
8+ from homeassistant .config_entries import ConfigEntry
89from homeassistant .const import (
910 CONF_HOST ,
1011 CONF_PASSWORD ,
1112 CONF_SCAN_INTERVAL ,
12- CONF_SOURCE ,
1313 CONF_SSL ,
1414 CONF_USERNAME ,
1515 CONF_VERIFY_SSL ,
1616)
1717from homeassistant .core import HomeAssistant
18- from homeassistant .exceptions import ConfigEntryNotReady
19- from homeassistant .helpers import aiohttp_client , service
18+ from homeassistant .exceptions import ConfigEntryAuthFailed , ConfigEntryNotReady
19+ from homeassistant .helpers import aiohttp_client
2020from homeassistant .helpers .device_registry import CONNECTION_NETWORK_MAC
2121from sagemcom_api .client import SagemcomClient
2222from sagemcom_api .enums import EncryptionMethod
2323from sagemcom_api .exceptions import (
2424 AccessRestrictionException ,
2525 AuthenticationException ,
26- LoginTimeoutException ,
2726 MaximumSessionCountException ,
2827 UnauthorizedException ,
2928)
29+ from sagemcom_api .models import DeviceInfo as GatewayDeviceInfo
30+
31+ from .const import (
32+ CONF_ENCRYPTION_METHOD ,
33+ DEFAULT_SCAN_INTERVAL ,
34+ DOMAIN ,
35+ LOGGER ,
36+ PLATFORMS ,
37+ )
38+ from .coordinator import SagemcomDataUpdateCoordinator
3039
31- from .const import CONF_ENCRYPTION_METHOD , DEFAULT_SCAN_INTERVAL , DOMAIN
32- from .device_tracker import SagemcomDataUpdateCoordinator
33-
34- _LOGGER = logging .getLogger (__name__ )
35-
36- PLATFORMS = ["device_tracker" ]
37-
38- SERVICE_REBOOT = "reboot"
39-
40-
41- async def async_setup (hass : HomeAssistant , config : dict ):
42- """Set up the Sagemcom component."""
4340
44- hass .data .setdefault (DOMAIN , {})
41+ @dataclass
42+ class HomeAssistantSagemcomFastData :
43+ """SagemcomFast data stored in the Home Assistant data object."""
4544
46- return True
45+ coordinator : SagemcomDataUpdateCoordinator
46+ gateway : GatewayDeviceInfo
4747
4848
4949async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ):
50- """Set up Sagemcom from a config entry."""
51-
50+ """Set up Sagemcom F@st from a config entry."""
5251 host = entry .data [CONF_HOST ]
5352 username = entry .data [CONF_USERNAME ]
5453 password = entry .data [CONF_PASSWORD ]
@@ -68,37 +67,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
6867
6968 try :
7069 await client .login ()
71- except AccessRestrictionException :
72- _LOGGER .error ("access_restricted" )
73- hass .async_create_task (
74- hass .config_entries .flow .async_init (
75- DOMAIN ,
76- context = {CONF_SOURCE : SOURCE_REAUTH },
77- data = entry .data ,
78- )
79- )
80- return False
81- except (AuthenticationException , UnauthorizedException ):
82- _LOGGER .error ("invalid_auth" )
83- hass .async_create_task (
84- hass .config_entries .flow .async_init (
85- DOMAIN ,
86- context = {CONF_SOURCE : SOURCE_REAUTH },
87- data = entry .data ,
88- )
89- )
90- return False
70+ except AccessRestrictionException as exception :
71+ LOGGER .error ("Access restricted" )
72+ raise ConfigEntryAuthFailed ("Access restricted" ) from exception
73+ except (AuthenticationException , UnauthorizedException ) as exception :
74+ LOGGER .error ("Invalid_auth" )
75+ raise ConfigEntryAuthFailed ("Invalid credentials" ) from exception
9176 except (TimeoutError , ClientError ) as exception :
92- _LOGGER .error ("Failed to connect" )
77+ LOGGER .error ("Failed to connect" )
9378 raise ConfigEntryNotReady ("Failed to connect" ) from exception
9479 except MaximumSessionCountException as exception :
95- _LOGGER .error ("Maximum session count reached" )
80+ LOGGER .error ("Maximum session count reached" )
9681 raise ConfigEntryNotReady ("Maximum session count reached" ) from exception
97- except LoginTimeoutException :
98- _LOGGER .error ("Request timed-out" )
99- return False
10082 except Exception as exception : # pylint: disable=broad-except
101- _LOGGER .exception (exception )
83+ LOGGER .exception (exception )
10284 return False
10385
10486 try :
@@ -110,18 +92,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
11092
11193 coordinator = SagemcomDataUpdateCoordinator (
11294 hass ,
113- _LOGGER ,
95+ LOGGER ,
11496 name = "sagemcom_hosts" ,
11597 client = client ,
11698 update_interval = timedelta (seconds = update_interval ),
11799 )
118100
119- await coordinator .async_refresh ()
101+ await coordinator .async_config_entry_first_refresh ()
120102
121- hass .data [DOMAIN ][entry .entry_id ] = {
122- "coordinator" : coordinator ,
123- "update_listener" : entry .add_update_listener (update_listener ),
124- }
103+ hass .data .setdefault (DOMAIN , {})[entry .entry_id ] = HomeAssistantSagemcomFastData (
104+ coordinator = coordinator , gateway = gateway
105+ )
125106
126107 # Create gateway device in Home Assistant
127108 device_registry = hass .helpers .device_registry .async_get (hass )
@@ -134,36 +115,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
134115 name = f"{ gateway .manufacturer } { gateway .model_number } " ,
135116 model = gateway .model_name ,
136117 sw_version = gateway .software_version ,
118+ configuration_url = f"{ 'https' if ssl else 'http' } ://{ host } " ,
137119 )
138120
139- # Register components
140121 await hass .config_entries .async_forward_entry_setups (entry , PLATFORMS )
141-
142- # Handle gateway device services
143- async def async_command_reboot (call ):
144- """Handle reboot service call."""
145- await client .reboot ()
146-
147- service .async_register_admin_service (
148- hass , DOMAIN , SERVICE_REBOOT , async_command_reboot
149- )
122+ entry .async_on_unload (entry .add_update_listener (update_listener ))
150123
151124 return True
152125
153126
154- async def async_unload_entry (hass : HomeAssistant , entry : ConfigEntry ):
127+ async def async_unload_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
155128 """Unload a config entry."""
156-
157- unload_ok = all (
158- await asyncio .gather (
159- * [
160- hass .config_entries .async_forward_entry_unload (entry , component )
161- for component in PLATFORMS
162- ]
163- )
164- )
165- if unload_ok :
166- hass .data [DOMAIN ][entry .entry_id ]["update_listener" ]()
129+ if unload_ok := await hass .config_entries .async_unload_platforms (entry , PLATFORMS ):
167130 hass .data [DOMAIN ].pop (entry .entry_id )
168131
169132 return unload_ok
@@ -172,9 +135,9 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
172135async def update_listener (hass : HomeAssistant , entry : ConfigEntry ):
173136 """Update when entry options update."""
174137 if entry .options [CONF_SCAN_INTERVAL ]:
175- coordinator = hass .data [DOMAIN ][entry .entry_id ][ "coordinator" ]
176- coordinator .update_interval = timedelta (
138+ data : HomeAssistantSagemcomFastData = hass .data [DOMAIN ][entry .entry_id ]
139+ data . coordinator .update_interval = timedelta (
177140 seconds = entry .options [CONF_SCAN_INTERVAL ]
178141 )
179142
180- await coordinator .async_refresh ()
143+ await data . coordinator .async_refresh ()
0 commit comments