Skip to content

Commit

Permalink
add option to configure update interval
Browse files Browse the repository at this point in the history
  • Loading branch information
mampfes committed Sep 24, 2023
1 parent 5270bcf commit 903eabc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
20 changes: 12 additions & 8 deletions custom_components/bayernluefter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST
from homeassistant.const import CONF_HOST, CONF_SCAN_INTERVAL
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import DeviceInfo, format_mac
from homeassistant.helpers.entity import Entity, EntityDescription
Expand All @@ -17,7 +17,7 @@

from pyernluefter import Bayernluefter

from .const import DOMAIN
from .const import DOMAIN, DEFAULT_SCAN_INTERVAL

_LOGGER = logging.getLogger(__name__)

Expand All @@ -33,7 +33,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
session = async_get_clientsession(hass)
device = Bayernluefter(entry.data[CONF_HOST], session)

coordinator = BayernluefterDataUpdateCoordinator(hass, device=device)
update_interval = timedelta(
seconds=entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
)
coordinator = BayernluefterDataUpdateCoordinator(
hass, device=device, update_interval=update_interval
)
await coordinator.async_config_entry_first_refresh()

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
Expand All @@ -56,10 +61,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return unload_ok


async def on_update_options_listener(hass, entry):
async def on_update_options_listener(hass: HomeAssistant, entry: ConfigEntry):
"""Handle options update."""
_LOGGER.warning("on_update_options_list")
pass # TODO
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator.update_interval = timedelta(seconds=entry.options[CONF_SCAN_INTERVAL])


class BayernluefterDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
Expand All @@ -71,12 +76,11 @@ def __init__(
self,
hass: HomeAssistant,
device: Bayernluefter,
update_interval,
) -> None:
"""Initialize."""
self._device = device

update_interval = timedelta(seconds=20)

super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=update_interval)

async def _async_update_data(self) -> dict[str, Any]:
Expand Down
49 changes: 41 additions & 8 deletions custom_components/bayernluefter/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@

import logging
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import (
CONF_HOST,
CONF_MAC,
CONF_SCAN_INTERVAL,
)
from homeassistant.helpers import aiohttp_client
from homeassistant.core import callback
from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.helpers import aiohttp_client, selector
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.schema_config_entry_flow import (
SchemaFlowFormStep,
SchemaOptionsFlowHandler,
)

from pyernluefter import Bayernluefter

from .const import DOMAIN
from .const import DOMAIN, DEFAULT_SCAN_INTERVAL

_LOGGER = logging.getLogger(__name__)

Expand All @@ -25,14 +31,39 @@
}
)

SIMPLE_OPTIONS_SCHEMA = vol.Schema(
{
vol.Optional(
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
): selector.NumberSelector(
selector.NumberSelectorConfig(
mode=selector.NumberSelectorMode.BOX,
unit_of_measurement="seconds",
min=1,
max=600,
),
),
}
)

class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
OPTIONS_FLOW = {
"init": SchemaFlowFormStep(next_step="simple_options"),
"simple_options": SchemaFlowFormStep(SIMPLE_OPTIONS_SCHEMA),
}


class ConfigFlow(ConfigFlow, domain=DOMAIN):
"""Component config flow."""

VERSION = 1

def __init__(self):
self._source = None
@staticmethod
@callback
def async_get_options_flow(
config_entry: ConfigEntry,
) -> SchemaOptionsFlowHandler:
"""Get options flow for this handler."""
return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW)

async def async_step_user(self, user_input=None):
"""Handle the start of the config flow.
Expand All @@ -59,10 +90,12 @@ async def async_step_user(self, user_input=None):
errors["base"] = "cannot_connect"
else:
user_input[CONF_MAC] = format_mac(device.raw()["MAC"])
# user_input[CONF_MODEL] = avr.protocol.model
await self.async_set_unique_id(user_input[CONF_MAC])
self._abort_if_unique_id_configured()
return self.async_create_entry(title=f"{device.raw_converted()['DeviceName']} @ {user_input[CONF_HOST]}", data=user_input)
return self.async_create_entry(
title=f"{device.raw_converted()['DeviceName']} @ {user_input[CONF_HOST]}", # noqa: E501
data=user_input,
)

return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
Expand Down
2 changes: 2 additions & 0 deletions custom_components/bayernluefter/const.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Constants for the component."""

DOMAIN = "bayernluefter"

DEFAULT_SCAN_INTERVAL = 10 # seconds

0 comments on commit 903eabc

Please sign in to comment.