diff --git a/custom_components/audiobookshelf/__init__.py b/custom_components/audiobookshelf/__init__.py index 1a76984..a60d330 100644 --- a/custom_components/audiobookshelf/__init__.py +++ b/custom_components/audiobookshelf/__init__.py @@ -1,32 +1,44 @@ """Custom component for Audiobookshelf.""" import logging -from homeassistant.helpers import discovery -# from .sensor import async_refresh_libraries +import voluptuous as vol +from homeassistant.helpers import config_validation as cv, discovery DOMAIN = "audiobookshelf" -_LOGGER = logging.getLogger(__name__) - -# async def async_setup(hass, config): -# """Set up the Audiobookshelf component.""" -# # Schedule the setup of sensor platform -# hass.async_create_task(discovery.async_load_platform(hass, "sensor", DOMAIN, {}, config)) -# hass.async_create_task(async_refresh_libraries(hass)) +CONFIG_SCHEMA = vol.Schema( + { + DOMAIN: vol.Schema( + { + vol.Required("api_key"): cv.string, + vol.Required("api_url"): cv.string, + vol.Optional("scan_interval", default=300): cv.positive_int + } + ) + }, + extra=vol.ALLOW_EXTRA, +) -# return True +_LOGGER = logging.getLogger(__name__) async def async_setup(hass, config): """Set up the Audiobookshelf component.""" - # Schedule the setup of sensor platform + conf = config.get(DOMAIN) + if conf is None: + _LOGGER.error(f"No config found for {DOMAIN}!") + return True + api_key = conf["api_key"] + api_url = conf["api_url"] + scan_interval = conf["scan_interval"] + + _LOGGER.info("API URL: %s", api_url) + _LOGGER.info("Scan Interval: %s", scan_interval) + + hass.data[DOMAIN] = { + "api_key": api_key, + "api_url": api_url, + "scan_interval": scan_interval + } + # Schedule the setup of sensor platform if needed hass.async_create_task(discovery.async_load_platform(hass, "sensor", DOMAIN, {}, config)) - # Use a helper to get the async_add_entities function from the sensor platform setup - # async def platform_setup(): - # """Wait for platform to be set up and then start refreshing libraries.""" - # platform = hass.data.get('sensor_platform') - # if platform: - # await async_refresh_libraries(hass, platform.async_add_entities) - - # hass.async_create_task(platform_setup()) - return True \ No newline at end of file diff --git a/custom_components/audiobookshelf/sensor.py b/custom_components/audiobookshelf/sensor.py index 33a0883..e6260e7 100644 --- a/custom_components/audiobookshelf/sensor.py +++ b/custom_components/audiobookshelf/sensor.py @@ -195,6 +195,18 @@ async def do_nothing(data): async def async_setup_platform(hass: HomeAssistant, config, async_add_entities, discovery_info=None): """Set up the sensor platform.""" + conf = hass.data.get(DOMAIN) + if conf is None: + _LOGGER.error("Configuration not found in hass.data") + return + + global API_URL + API_URL = conf["api_url"] + global API_KEY + API_KEY = conf["api_key"] + global SCAN_INTERVAL + SCAN_INTERVAL = timedelta(seconds=conf["scan_interval"]) + coordinator = AudiobookshelfDataUpdateCoordinator(hass) await coordinator.async_config_entry_first_refresh()