diff --git a/custom_components/saas/__init__.py b/custom_components/saas/__init__.py index 28671be..9ebcca0 100644 --- a/custom_components/saas/__init__.py +++ b/custom_components/saas/__init__.py @@ -2,10 +2,23 @@ import asyncio import logging from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady from .const import DOMAIN from homeassistant.helpers.dispatcher import async_dispatcher_connect from .services import async_setup_services # Import the service setup function +try: + import homeassistant + import logging + logging.getLogger(__name__).info("SAAS init loaded successfully") +except Exception as e: + import traceback + logging.getLogger(__name__).error(f"SAAS init failed: {e}\n{traceback.format_exc()}") + raise + + + + _logger = logging.getLogger(__name__) async def async_setup(hass: HomeAssistant, config: dict): @@ -16,6 +29,11 @@ async def async_setup(hass: HomeAssistant, config: dict): async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up a config entry.""" _logger.info(f"Starting setup of config entry with ID: {entry.entry_id}") + + if "mqtt" not in hass.config.components: + _logger.warning("MQTT not yet available. Retrying later...") + raise ConfigEntryNotReady("MQTT integration is not ready") + hass.data.setdefault(DOMAIN, {}) if entry.entry_id not in hass.data[DOMAIN] and entry.data: @@ -23,13 +41,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): _logger.info(f"hass.data[DOMAIN] after adding entry data: {hass.data[DOMAIN]}") - # Forward the setup to the sensor and button platforms using the new method. - # Note: async_forward_entry_setups must always be awaited. await hass.config_entries.async_forward_entry_setups(entry, ["sensor", "button"]) _logger.info(f"hass.data[DOMAIN] before async_setup_services: {hass.data[DOMAIN]}") - # Setup the services _logger.info("Starting setup of services") await async_setup_services(hass) _logger.info("Finished setup of services") @@ -49,11 +64,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" _logger.info(f"Starting unload of config entry with ID: {entry.entry_id}") - # Remove the sensor platform - _logger.info("Removing sensor platform") await hass.config_entries.async_forward_entry_unload(entry, "sensor") - # Ensure hass.data[DOMAIN] is a dictionary before popping if isinstance(hass.data.get(DOMAIN, {}), dict): hass.data[DOMAIN].pop(entry.entry_id, None) diff --git a/custom_components/saas/button.py b/custom_components/saas/button.py index 945dcb2..96a82ca 100644 --- a/custom_components/saas/button.py +++ b/custom_components/saas/button.py @@ -572,4 +572,4 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # Add the entities async_add_entities(entities) - # _LOGGER.debug("SAAS Sleep Tracking buttons set up successfully") \ No newline at end of file + return True \ No newline at end of file diff --git a/custom_components/saas/manifest.json b/custom_components/saas/manifest.json index 7de1702..4f2ad3a 100644 --- a/custom_components/saas/manifest.json +++ b/custom_components/saas/manifest.json @@ -3,6 +3,7 @@ "name": "SAAS - Sleep As Android Status", "codeowners": ["@sudoxnym"], "config_flow": true, + "platforms": ["sensor", "button"], "dependencies": ["mqtt", "mobile_app"], "documentation": "https://www.github.com/sudoxnym/saas", "issue_tracker": "https://www.github.com/sudoxnym/saas/issues", diff --git a/custom_components/saas/sensor.py b/custom_components/saas/sensor.py index 00cc7e4..3c16274 100644 --- a/custom_components/saas/sensor.py +++ b/custom_components/saas/sensor.py @@ -833,3 +833,4 @@ async def async_setup_entry(hass, entry, async_add_entities): await entity.async_setup() async_add_entities(entities) + return True \ No newline at end of file diff --git a/custom_components/saas/services.py b/custom_components/saas/services.py index d61ad18..5a3040a 100644 --- a/custom_components/saas/services.py +++ b/custom_components/saas/services.py @@ -50,7 +50,7 @@ class SAASService: except Exception as e: _LOGGER.error(f"Error occurred while calling service: {e}") -async def async_setup_services(hass: HomeAssistant) -> None: +async def async_setup_services(hass: HomeAssistant) -> bool: """Set up services for the SAAS component.""" _LOGGER.info(f"Setting up services for {DOMAIN}") # Register the service for each entry @@ -70,4 +70,6 @@ async def async_setup_services(hass: HomeAssistant) -> None: _LOGGER.warning(f"No notify_target found for name: {name}. Skipping service registration.") else: _LOGGER.warning(f"No entry data found for entry_id: {entry_id}") - _LOGGER.info(f"Finished setting up services for {DOMAIN}") \ No newline at end of file + _LOGGER.info(f"Finished setting up services for {DOMAIN}") + + return True \ No newline at end of file diff --git a/custom_components/saas/services.yaml b/custom_components/saas/services.yaml new file mode 100644 index 0000000..e69de29