diff --git a/__init__.py b/__init__.py deleted file mode 100644 index 31095f8..0000000 --- a/__init__.py +++ /dev/null @@ -1,155 +0,0 @@ -import logging -from datetime import timedelta - -import aiohttp -from homeassistant.config_entries import ConfigEntry -from homeassistant.core import HomeAssistant - -from .api import ProxmoxClient -from .const import ( - DOMAIN, - PLATFORMS, - CONF_SCAN_INTERVAL, - CONF_IP_MODE, - CONF_IP_PREFIX, - DEFAULT_SCAN_INTERVAL, - DEFAULT_IP_MODE, - DEFAULT_IP_PREFIX, -) -from .coordinator import ProxmoxResourcesCoordinator, ProxmoxNodesCoordinator - -_LOGGER = logging.getLogger(__name__) - - -def _opt(entry: ConfigEntry, key: str, default): - return entry.options.get(key, default) - - -async def _apply_options_now(hass: HomeAssistant, entry: ConfigEntry) -> None: - """Apply updated options to all coordinators without reload/restart.""" - data = hass.data[DOMAIN][entry.entry_id] - - new_scan_interval = int(_opt(entry, CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)) - new_ip_mode = str(_opt(entry, CONF_IP_MODE, DEFAULT_IP_MODE)) - new_ip_prefix = str(_opt(entry, CONF_IP_PREFIX, DEFAULT_IP_PREFIX)) - - # store new values for future coordinator creations - data["scan_interval"] = new_scan_interval - data["ip_mode"] = new_ip_mode - data["ip_prefix"] = new_ip_prefix - - td = timedelta(seconds=new_scan_interval) - - # cluster coordinators - resources = data.get("resources") - if resources: - resources.update_interval = td - - nodes = data.get("nodes") - if nodes: - nodes.update_interval = td - - # node coordinators - for node_coord in (data.get("node_coordinators") or {}).values(): - node_coord.update_interval = td - - # guest coordinators (also update ip preference config) - for guest_coord in (data.get("guest_coordinators") or {}).values(): - guest_coord.update_interval = td - guest_coord.ip_mode = new_ip_mode - guest_coord.ip_prefix = new_ip_prefix - - # trigger refresh so UI updates quickly - tasks = [] - if resources: - tasks.append(resources.async_request_refresh()) - if nodes: - tasks.append(nodes.async_request_refresh()) - - for node_coord in (data.get("node_coordinators") or {}).values(): - tasks.append(node_coord.async_request_refresh()) - - for guest_coord in (data.get("guest_coordinators") or {}).values(): - tasks.append(guest_coord.async_request_refresh()) - - if tasks: - # don't fail all if one refresh fails - for t in tasks: - hass.async_create_task(t) - - _LOGGER.debug( - "Applied options live for %s: scan_interval=%s ip_mode=%s ip_prefix=%s", - entry.entry_id, - new_scan_interval, - new_ip_mode, - new_ip_prefix, - ) - - -async def _update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: - """Called by HA when options are changed.""" - await _apply_options_now(hass, entry) - - -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: - session = aiohttp.ClientSession() - - client = ProxmoxClient( - host=entry.data["host"], - port=entry.data["port"], - token_name=entry.data["token_name"], - token_value=entry.data["token_value"], - verify_ssl=entry.data["verify_ssl"], - session=session, - ) - - scan_interval = int(_opt(entry, CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)) - ip_mode = str(_opt(entry, CONF_IP_MODE, DEFAULT_IP_MODE)) - ip_prefix = str(_opt(entry, CONF_IP_PREFIX, DEFAULT_IP_PREFIX)) - - resources = ProxmoxResourcesCoordinator(hass, client, scan_interval=scan_interval) - nodes = ProxmoxNodesCoordinator(hass, client, scan_interval=scan_interval) - - await resources.async_config_entry_first_refresh() - await nodes.async_config_entry_first_refresh() - - hass.data.setdefault(DOMAIN, {}) - hass.data[DOMAIN][entry.entry_id] = { - "session": session, - "client": client, - "resources": resources, - "nodes": nodes, - "scan_interval": scan_interval, - "ip_mode": ip_mode, - "ip_prefix": ip_prefix, - "guest_coordinators": {}, # (node, vmtype, vmid) -> ProxmoxGuestCoordinator - "node_coordinators": {}, # node -> ProxmoxNodeCoordinator - "platform_cache": {}, # per-platform caches + unsub handles - } - - # Apply option updates live (gear icon -> save) - entry.async_on_unload(entry.add_update_listener(_update_listener)) - - await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) - _LOGGER.debug("Set up proxmox_pve entry %s", entry.entry_id) - return True - - -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: - unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) - - if unload_ok: - data = hass.data.get(DOMAIN, {}).pop(entry.entry_id, None) - if data: - pc = data.get("platform_cache") or {} - for key in ("sensor_unsub", "switch_unsub", "button_unsub"): - for unsub in pc.get(key, []): - try: - unsub() - except Exception: - pass - - if data.get("session"): - await data["session"].close() - - return unload_ok