11"""The Easywave integration."""
2+
23from __future__ import annotations
34
5+ from dataclasses import dataclass
46import logging
57
68from homeassistant .config_entries import ConfigEntry
79from homeassistant .const import Platform
810from homeassistant .core import HomeAssistant
9- from homeassistant .exceptions import HomeAssistantError
11+ from homeassistant .exceptions import ConfigEntryNotReady , HomeAssistantError
1012from homeassistant .helpers import device_registry as dr , issue_registry as ir
1113
1214from .const import (
1517 get_frequency_for_pid ,
1618 is_country_allowed_for_frequency ,
1719)
20+ from .coordinator import EasywaveCoordinator
21+ from .transceiver import RX11Transceiver
1822
1923_LOGGER = logging .getLogger (__name__ )
2024
21- type EasywaveConfigEntry = ConfigEntry [None ]
25+
26+ @dataclass
27+ class EasywaveRuntimeData :
28+ """Runtime data for the Easywave integration."""
29+
30+ coordinator : EasywaveCoordinator
31+ frequency : str
32+ country : str
33+
34+
35+ type EasywaveConfigEntry = ConfigEntry [EasywaveRuntimeData ]
2236
2337PLATFORMS : list [Platform ] = [Platform .SENSOR ]
2438
2539
2640async def async_setup_entry (hass : HomeAssistant , entry : EasywaveConfigEntry ) -> bool :
2741 """Set up Easywave from a config entry."""
28- hass .data .setdefault (DOMAIN , {})
29-
42+
3043 # ── Regulatory compliance check (868 MHz) ──────────────────────────
3144 # The operating frequency is derived from the USB device's PID.
3245 # If the configured HA country is outside the allowed region for that
@@ -37,8 +50,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: EasywaveConfigEntry) ->
3750
3851 if frequency and not is_country_allowed_for_frequency (frequency , country_code ):
3952 _LOGGER .warning (
40- "This hardware operates on %s, which is not permitted in your "
41- "configured region (%s). Integration disabled for regulatory compliance." ,
53+ "This hardware operates on %s, which is not permitted in "
54+ "your configured region (%s). Integration disabled for "
55+ "regulatory compliance" ,
4256 frequency ,
4357 country_code or "unknown" ,
4458 )
@@ -55,26 +69,52 @@ async def async_setup_entry(hass: HomeAssistant, entry: EasywaveConfigEntry) ->
5569 "country" : country_code or "unknown" ,
5670 },
5771 )
72+ # Return False for regulatory compliance violation (not a setup error)
5873 return False
5974
6075 # If the check passed, make sure any stale repair issue is removed
6176 # (e.g. user changed their country setting).
6277 ir .async_delete_issue (hass , DOMAIN , f"frequency_not_permitted_{ entry .entry_id } " )
63-
78+
79+ # ── Initialize transceiver and coordinator ──────────────────────────
80+ # Create transceiver instance (will search for USB RX11 device)
81+ transceiver = RX11Transceiver (hass )
82+
83+ # Create coordinator for managing connection lifecycle & offline mode
84+ coordinator = EasywaveCoordinator (hass , transceiver , entry )
85+
86+ # Attempt initial setup (may succeed in offline mode)
87+ if not await coordinator .async_setup ():
88+ raise ConfigEntryNotReady ("Failed to initialize coordinator" )
89+
90+ # Set runtime data for the integration
91+ entry .runtime_data = EasywaveRuntimeData (
92+ coordinator = coordinator ,
93+ frequency = frequency or "unknown" ,
94+ country = country_code or "unknown" ,
95+ )
96+
6497 await hass .config_entries .async_forward_entry_setups (entry , PLATFORMS )
6598 return True
6699
67100
68101async def async_unload_entry (hass : HomeAssistant , entry : EasywaveConfigEntry ) -> bool :
69102 """Unload a config entry."""
103+ # Shutdown coordinator
104+ if hasattr (entry , "runtime_data" ) and entry .runtime_data :
105+ await entry .runtime_data .coordinator .async_shutdown ()
106+
70107 return await hass .config_entries .async_unload_platforms (entry , PLATFORMS )
71108
72109
73110async def async_remove_config_entry_device (
74111 hass : HomeAssistant , config_entry : ConfigEntry , device_entry : dr .DeviceEntry
75112) -> bool :
76- """Prevent removing the RX11 gateway device via the UI device menu."""
77- raise HomeAssistantError (
78- translation_domain = DOMAIN ,
79- translation_key = "cannot_delete_rx11" ,
80- )
113+ """Allow removing devices except the RX11 gateway."""
114+ gateway_identifier = (DOMAIN , f"{ config_entry .entry_id } _gateway" )
115+ if gateway_identifier in device_entry .identifiers :
116+ raise HomeAssistantError (
117+ translation_domain = DOMAIN ,
118+ translation_key = "cannot_delete_gateway" ,
119+ )
120+ return True
0 commit comments