Skip to content

Commit 68295e2

Browse files
committed
Added options flow to reconfigure Sonarr integration to match Radarr Integration
1 parent fd32a34 commit 68295e2

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

custom_components/sonarr_upcoming_media/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
3333
setup_client,
3434
hass,
3535
config_entry.data[CONF_API_KEY],
36-
config_entry.data[CONF_DAYS],
36+
config_entry.options.get(CONF_DAYS, config_entry.data[CONF_DAYS]),
3737
config_entry.data[CONF_HOST],
3838
config_entry.data[CONF_PORT],
3939
config_entry.data[CONF_SSL],
4040
config_entry.data[CONF_URLBASE],
41-
config_entry.data[CONF_MAX],
41+
config_entry.options.get(CONF_MAX, config_entry.data[CONF_MAX]),
4242
)
4343
except FailedToLogin as err:
4444
raise ConfigEntryNotReady("Failed to Log-in") from err
@@ -52,6 +52,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
5252
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = coordinator
5353

5454
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
55+
config_entry.async_on_unload(config_entry.add_update_listener(update_listener))
5556

5657
return True
5758

@@ -64,3 +65,7 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
6465
if not hass.data[DOMAIN]:
6566
del hass.data[DOMAIN]
6667
return unload_ok
68+
69+
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
70+
"""Handle options update."""
71+
await hass.config_entries.async_reload(config_entry.entry_id)

custom_components/sonarr_upcoming_media/config_flow.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
SonarrCannotBeReached
2424
)
2525

26+
from homeassistant.config_entries import ConfigEntry
27+
from homeassistant.core import callback
28+
from .options_flow import SonarrOptionFlow
29+
2630
def valid_max():
2731
return lambda x: type(x) == int and x > 0
2832

@@ -40,6 +44,11 @@ def valid_max():
4044

4145
class SonarrConfigFlow(ConfigFlow, domain=DOMAIN):
4246
"""Config flow for the Sonarr integration."""
47+
@staticmethod
48+
@callback
49+
def async_get_options_flow(config_entry: ConfigEntry) -> SonarrOptionFlow:
50+
return SonarrOptionFlow(config_entry)
51+
4352
async def async_step_user(
4453
self, user_input: dict[str, Any] | None = None
4554
):
@@ -67,4 +76,4 @@ async def async_step_user(
6776
return self.async_create_entry(title=user_input[CONF_NAME], data=user_input)
6877

6978
schema = self.add_suggested_values_to_schema(SONARR_SCHEMA, user_input)
70-
return self.async_show_form(step_id="user", data_schema=schema, errors=errors)
79+
return self.async_show_form(step_id="user", data_schema=schema, errors=errors)

custom_components/sonarr_upcoming_media/manifest.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@
88
"iot_class": "local_polling",
99
"issue_tracker": "https://github.com/custom-components/sensor.sonarr_upcoming_media/issues",
1010
"requirements": [],
11-
"version": "0.4.3"
11+
"version": "0.4.5"
1212
}
13-
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import Any
2+
from homeassistant.config_entries import OptionsFlow, ConfigEntry, ConfigFlowResult
3+
import voluptuous as vol
4+
5+
from .const import (
6+
CONF_DAYS,
7+
CONF_MAX,
8+
DOMAIN,
9+
)
10+
11+
import logging
12+
_LOGGER = logging.getLogger(__name__)
13+
class SonarrOptionFlow(OptionsFlow):
14+
def __init__(self, config_entry: ConfigEntry) -> None:
15+
self._config_entry = config_entry
16+
17+
18+
async def async_step_init(self, user_input: dict[str, Any] | None = None) -> ConfigFlowResult:
19+
errors = {}
20+
21+
client = self.hass.data[DOMAIN][self._config_entry.entry_id]._client
22+
23+
if user_input is not None:
24+
# Validate and process user input here
25+
return self.async_create_entry(title="", data=user_input)
26+
27+
SONARR_SCHEMA = vol.Schema({
28+
vol.Required(CONF_DAYS, default=self._config_entry.options.get(CONF_DAYS, self._config_entry.data[CONF_DAYS])): vol.All(vol.Coerce(int), vol.Range(min=1)),
29+
vol.Required(CONF_MAX, default=self._config_entry.options.get(CONF_MAX, self._config_entry.data[CONF_MAX])): vol.All(vol.Coerce(int), vol.Range(min=0)),
30+
})
31+
32+
# Display a form to gather user input
33+
return self.async_show_form(step_id="init", data_schema=SONARR_SCHEMA, errors=errors)
34+
35+
def keys(d) -> list:
36+
return [i for i in d.keys()]

custom_components/sonarr_upcoming_media/strings.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,15 @@
2323
"failed_to_login": "Failed to Log-in",
2424
"cannot_be_reached": "Sonarr cannot be reached"
2525
}
26+
},
27+
"options": {
28+
"step": {
29+
"init": {
30+
"data": {
31+
"days": "How many days look ahead for the upcoming sensor",
32+
"max": "Max number of items in sensor"
33+
}
34+
}
35+
}
2636
}
27-
}
37+
}

0 commit comments

Comments
 (0)