Skip to content

Commit

Permalink
Configure login pause #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre0512 committed Oct 17, 2023
1 parent 91add87 commit b5d7bba
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
16 changes: 13 additions & 3 deletions custom_components/speedport/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""The Speedport integration."""

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client
from speedport import Speedport

from .config_flow import OptionsFlowHandler
from .const import DOMAIN

PLATFORMS: list[Platform] = [
Expand All @@ -19,22 +19,32 @@

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

hass.data.setdefault(DOMAIN, {})
speedport = await Speedport(
host=entry.data["host"], password=entry.data["password"], session=session
host=entry.data["host"],
password=entry.data["password"],
session=aiohttp_client.async_get_clientsession(hass),
pause_time=entry.options.get("pause_time", 5),
).create()
hass.data[DOMAIN][entry.entry_id] = speedport
hass.data[DOMAIN]["coordinators"] = {}

entry.async_on_unload(entry.add_update_listener(update_listener))

for platform in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
return True


async def update_listener(hass, entry):
"""Handle options update."""
speedport: Speedport = hass.data[DOMAIN][entry.entry_id]
speedport.set_pause_time(entry.options.get("pause_time", 5))


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
Expand Down
35 changes: 34 additions & 1 deletion custom_components/speedport/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError

Expand Down Expand Up @@ -76,10 +76,43 @@ async def async_step_user(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)

@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> config_entries.OptionsFlow:
"""Create the options flow."""
return OptionsFlowHandler(config_entry)


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


class InvalidAuth(HomeAssistantError):
"""Error to indicate there is invalid auth."""


class OptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Manage the options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Required(
"pause_time",
default=self.config_entry.options.get("pause_time", 5),
): int
}
),
)
4 changes: 2 additions & 2 deletions custom_components/speedport/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/Andre0512/speedport/issues",
"requirements": [
"speedport-api==0.5.4"
"speedport-api==0.5.5"
],
"version": "0.3.1"
"version": "0.3.2"
}
11 changes: 11 additions & 0 deletions custom_components/speedport/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,16 @@
}
}
}
},
"options": {
"step": {
"init": {
"data": {
"pause_time": "Pause for minutes"
},
"title": "Settings",
"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)"
}
}
}
}
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
speedport-api==0.5.4
speedport-api==0.5.5
homeassistant~=2023.10
pytz~=2023.3

0 comments on commit b5d7bba

Please sign in to comment.