1+ """MeshCore SMS Gateway Integration."""
2+
3+ import logging
4+ from typing import Any , Dict
5+
6+ from homeassistant .config_entries import ConfigEntry
7+ from homeassistant .const import Platform
8+ from homeassistant .core import HomeAssistant
9+ from homeassistant .exceptions import ConfigEntryNotReady
10+
11+ from .const import DOMAIN
12+ from .gateway import MeshCoreSMSGateway
13+
14+ _LOGGER = logging .getLogger (__name__ )
15+
16+ PLATFORMS : list [Platform ] = []
17+
18+
19+ async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
20+ """Set up MeshCore SMS Gateway from a config entry."""
21+
22+ # Check if MeshCore integration is loaded
23+ if "meshcore" not in hass .config .integrations :
24+ _LOGGER .error ("MeshCore integration is not loaded. Please install and configure it first." )
25+ raise ConfigEntryNotReady ("MeshCore integration is required" )
26+
27+ # Initialize the gateway
28+ gateway = MeshCoreSMSGateway (hass , entry )
29+
30+ # Set up the gateway
31+ if not await gateway .async_setup ():
32+ return False
33+
34+ # Store gateway instance
35+ hass .data .setdefault (DOMAIN , {})
36+ hass .data [DOMAIN ][entry .entry_id ] = gateway
37+
38+ # Set up platforms
39+ await hass .config_entries .async_forward_entry_setups (entry , PLATFORMS )
40+
41+ # Register update listener
42+ entry .async_on_unload (entry .add_update_listener (async_reload_entry ))
43+
44+ _LOGGER .info (
45+ "MeshCore SMS Gateway initialized for %s" ,
46+ entry .data .get ("from_number" )
47+ )
48+
49+ return True
50+
51+
52+ async def async_unload_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
53+ """Unload a config entry."""
54+ # Unload platforms
55+ if unload_ok := await hass .config_entries .async_unload_platforms (
56+ entry , PLATFORMS
57+ ):
58+ # Get gateway instance
59+ gateway = hass .data [DOMAIN ].pop (entry .entry_id )
60+
61+ # Unload gateway
62+ await gateway .async_unload ()
63+
64+ return unload_ok
65+
66+
67+ async def async_reload_entry (hass : HomeAssistant , entry : ConfigEntry ) -> None :
68+ """Reload config entry."""
69+ await async_unload_entry (hass , entry )
70+ await async_setup_entry (hass , entry )
71+
72+
73+ async def async_migrate_entry (hass : HomeAssistant , config_entry : ConfigEntry ) -> bool :
74+ """Migrate old entry."""
75+ _LOGGER .debug ("Migrating from version %s" , config_entry .version )
76+
77+ if config_entry .version == 1 :
78+ # Future migrations go here
79+ pass
80+
81+ _LOGGER .info ("Migration to version %s successful" , config_entry .version )
82+
83+ return True
0 commit comments