Skip to content

Commit

Permalink
feat: rewrite library sensors and config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
wolffshots committed Nov 20, 2024
1 parent 6fbb866 commit a06da30
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 172 deletions.
2 changes: 1 addition & 1 deletion .devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ludeeus/integration_blueprint",
"name": "wolffshots/hass-audiobookshelf",
"image": "mcr.microsoft.com/devcontainers/python:3.12",
"postCreateCommand": "scripts/setup",
"forwardPorts": [
Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,8 @@ cython_debug/

test

test_creds.py
test_creds.py

# Home Assistant configuration
config/*
!config/configuration.yaml
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml

This file was deleted.

1 change: 1 addition & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ignore = [
"D212", # multi-line-summary-first-line (incompatible with formatter)
"COM812", # incompatible with formatter
"ISC001", # incompatible with formatter
"ERA001", commented code isn't always bad
]

[lint.flake8-pytest-style]
Expand Down
58 changes: 32 additions & 26 deletions custom_components/audiobookshelf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
"""Custom component for Audiobookshelf."""

import logging

import voluptuous as vol
from homeassistant.helpers import config_validation as cv, discovery
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_SCAN_INTERVAL, CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv

DOMAIN = "audiobookshelf"
from .const import DOMAIN, PLATFORMS

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,
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_URL): cv.string,
vol.Optional(CONF_SCAN_INTERVAL, default=300): cv.positive_int,
}
)
},
Expand All @@ -21,27 +27,27 @@
_LOGGER = logging.getLogger(__name__)


async def async_setup(hass, config):
"""Set up the Audiobookshelf component."""
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)
def clean_config(data: dict) -> dict:
"""Remove the api key from config."""
if bool(data[CONF_API_KEY]):
data[CONF_API_KEY] = "<redacted>"
return data


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Audiobookshelf from a config entry."""
if entry.data is None:
error_message = "Configuration not found"
raise ConfigEntryNotReady(error_message)

_LOGGER.debug(
"Setting up Audiobookshelf with config: %s", clean_config(entry.data.copy())
)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
46 changes: 46 additions & 0 deletions custom_components/audiobookshelf/config_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Config flow for Audiobookshelf integration."""

from __future__ import annotations

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_API_KEY, CONF_SCAN_INTERVAL, CONF_URL

from .const import DOMAIN


class AudiobookshelfConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Audiobookshelf."""

VERSION = 1

async def async_step_user(
self, user_input: dict | None = None
) -> config_entries.ConfigFlowResult:
"""Handle the user step."""
errors = {}

if user_input is not None:
# Process user input
return self.async_create_entry(
title="Audiobookshelf",
data={
CONF_URL: user_input[CONF_URL],
CONF_API_KEY: user_input[CONF_API_KEY],
CONF_SCAN_INTERVAL: user_input.get(CONF_SCAN_INTERVAL, 300),
},
)

# Show the form to the user
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_URL): str,
vol.Required(CONF_API_KEY): str,
vol.Optional(CONF_SCAN_INTERVAL, default=300): cv.positive_int,
}
),
errors=errors,
)
6 changes: 6 additions & 0 deletions custom_components/audiobookshelf/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Constants for the Audiobookshelf integration."""

from homeassistant.const import Platform

DOMAIN = "audiobookshelf"
PLATFORMS: list[Platform] = [Platform.SENSOR]
11 changes: 8 additions & 3 deletions custom_components/audiobookshelf/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/wolffshots/hass-audiobookshelf/issues",
"dependencies": [],
"requirements": ["aiohttp"],
"codeowners": ["@wolffshots"],
"integration_type": "device"
"requirements": [
"aiohttp"
],
"codeowners": [
"@wolffshots"
],
"integration_type": "device",
"config_flow": true
}
Loading

0 comments on commit a06da30

Please sign in to comment.