Skip to content

Commit b5d7bba

Browse files
committed
Configure login pause #2
1 parent 91add87 commit b5d7bba

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

custom_components/speedport/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""The Speedport integration."""
2-
32
from homeassistant.config_entries import ConfigEntry
43
from homeassistant.const import Platform
54
from homeassistant.core import HomeAssistant
65
from homeassistant.helpers import aiohttp_client
76
from speedport import Speedport
87

8+
from .config_flow import OptionsFlowHandler
99
from .const import DOMAIN
1010

1111
PLATFORMS: list[Platform] = [
@@ -19,22 +19,32 @@
1919

2020
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2121
"""Set up Speedport from a config entry."""
22-
session = aiohttp_client.async_get_clientsession(hass)
2322

2423
hass.data.setdefault(DOMAIN, {})
2524
speedport = await Speedport(
26-
host=entry.data["host"], password=entry.data["password"], session=session
25+
host=entry.data["host"],
26+
password=entry.data["password"],
27+
session=aiohttp_client.async_get_clientsession(hass),
28+
pause_time=entry.options.get("pause_time", 5),
2729
).create()
2830
hass.data[DOMAIN][entry.entry_id] = speedport
2931
hass.data[DOMAIN]["coordinators"] = {}
3032

33+
entry.async_on_unload(entry.add_update_listener(update_listener))
34+
3135
for platform in PLATFORMS:
3236
hass.async_create_task(
3337
hass.config_entries.async_forward_entry_setup(entry, platform)
3438
)
3539
return True
3640

3741

42+
async def update_listener(hass, entry):
43+
"""Handle options update."""
44+
speedport: Speedport = hass.data[DOMAIN][entry.entry_id]
45+
speedport.set_pause_time(entry.options.get("pause_time", 5))
46+
47+
3848
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3949
"""Unload a config entry."""
4050
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):

custom_components/speedport/config_flow.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import voluptuous as vol
99

1010
from homeassistant import config_entries
11-
from homeassistant.core import HomeAssistant
11+
from homeassistant.core import HomeAssistant, callback
1212
from homeassistant.data_entry_flow import FlowResult
1313
from homeassistant.exceptions import HomeAssistantError
1414

@@ -76,10 +76,43 @@ async def async_step_user(
7676
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
7777
)
7878

79+
@staticmethod
80+
@callback
81+
def async_get_options_flow(
82+
config_entry: config_entries.ConfigEntry,
83+
) -> config_entries.OptionsFlow:
84+
"""Create the options flow."""
85+
return OptionsFlowHandler(config_entry)
86+
7987

8088
class CannotConnect(HomeAssistantError):
8189
"""Error to indicate we cannot connect."""
8290

8391

8492
class InvalidAuth(HomeAssistantError):
8593
"""Error to indicate there is invalid auth."""
94+
95+
96+
class OptionsFlowHandler(config_entries.OptionsFlow):
97+
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
98+
"""Initialize options flow."""
99+
self.config_entry = config_entry
100+
101+
async def async_step_init(
102+
self, user_input: dict[str, Any] | None = None
103+
) -> FlowResult:
104+
"""Manage the options."""
105+
if user_input is not None:
106+
return self.async_create_entry(title="", data=user_input)
107+
108+
return self.async_show_form(
109+
step_id="init",
110+
data_schema=vol.Schema(
111+
{
112+
vol.Required(
113+
"pause_time",
114+
default=self.config_entry.options.get("pause_time", 5),
115+
): int
116+
}
117+
),
118+
)

custom_components/speedport/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"iot_class": "local_polling",
1313
"issue_tracker": "https://github.com/Andre0512/speedport/issues",
1414
"requirements": [
15-
"speedport-api==0.5.4"
15+
"speedport-api==0.5.5"
1616
],
17-
"version": "0.3.1"
17+
"version": "0.3.2"
1818
}

custom_components/speedport/translations/en.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,16 @@
1717
}
1818
}
1919
}
20+
},
21+
"options": {
22+
"step": {
23+
"init": {
24+
"data": {
25+
"pause_time": "Pause for minutes"
26+
},
27+
"title": "Settings",
28+
"description": "Time to stop updating sensors that require authorization when the user logs in to the web interface. (User gets logged out after this time)"
29+
}
30+
}
2031
}
2132
}

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
speedport-api==0.5.4
1+
speedport-api==0.5.5
22
homeassistant~=2023.10
33
pytz~=2023.3

0 commit comments

Comments
 (0)