Skip to content

Commit

Permalink
Merge pull request #15 from etmelvin/main
Browse files Browse the repository at this point in the history
Added the ability to specify scan intervals in seconds when configuring integration
  • Loading branch information
gdgib committed Aug 23, 2023
2 parents 257a43b + 69c80cb commit ecf82f2
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 50 deletions.
25 changes: 22 additions & 3 deletions custom_components/span_panel/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
"""The Span Panel integration."""
from __future__ import annotations
from datetime import timedelta

import logging

import async_timeout
import httpx

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, Platform
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_HOST,
CONF_SCAN_INTERVAL,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.httpx_client import get_async_client
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import COORDINATOR, DOMAIN, NAME, SCAN_INTERVAL
from .const import COORDINATOR, DOMAIN, NAME, DEFAULT_SCAN_INTERVAL
from .span_panel import SpanPanel

PLATFORMS: list[Platform] = [
Expand Down Expand Up @@ -57,16 +63,22 @@ async def async_update_data():

name = "SN-TODO"

scan_interval: int = entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL.seconds
)

coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
name=f"span panel {name}",
update_method=async_update_data,
update_interval=SCAN_INTERVAL,
update_interval=timedelta(seconds=scan_interval),
)

await coordinator.async_config_entry_first_refresh()

entry.async_on_unload(entry.add_update_listener(update_listener))

hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = {
COORDINATOR: coordinator,
Expand All @@ -87,3 +99,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok


async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""
Update listener.
"""
await hass.config_entries.async_reload(entry.entry_id)
40 changes: 37 additions & 3 deletions custom_components/span_panel/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components import zeroconf
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, CONF_SCAN_INTERVAL
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.httpx_client import get_async_client
from homeassistant.util.network import is_ipv4_address

from .const import DOMAIN
from .const import DOMAIN, DEFAULT_SCAN_INTERVAL
from .span_panel_api import SpanPanelApi

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -276,3 +276,37 @@ def update_existing_entry(
self.hass.config_entries.async_update_entry(entry, data=entry_data)
self.hass.async_create_task(self.hass.config_entries.async_reload(entry_id))
return self.async_abort(reason="reauth_successful")

@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> OptionsFlowHandler:
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)


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

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

curr_scan_interval = self.config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL.seconds
)

return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Optional(
CONF_SCAN_INTERVAL, default=curr_scan_interval
): vol.All(int, vol.Range(min=5)),
}
),
)
2 changes: 1 addition & 1 deletion custom_components/span_panel/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

PANEL_MAIN_RELAY_STATE_UNKNOWN_VALUE = "UNKNOWN"

SCAN_INTERVAL = timedelta(seconds=15)
DEFAULT_SCAN_INTERVAL = timedelta(seconds=15)
API_TIMEOUT = 30


Expand Down
95 changes: 52 additions & 43 deletions custom_components/span_panel/strings.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
{
"config": {
"abort": {
"no_devices_found": "No devices found on the network",
"already_configured": "Span Panel already configured. Only a single configuration is possible.",
"reauth_successful": "Authentication successful."
},
"error": {
"cannot_connect": "Failed to connect to Span Panel",
"invalid_auth": "Invalid authentication",
"unknown": "Unexpected error"
},
"flow_title": "Span Panel ({host})",
"step": {
"confirm_discovery": {
"description": "Do you want to setup Span Panel at {host}?",
"title": "Connect to the Span Panel"
},
"user": {
"data": {
"host": "Host"
},
"title": "Connect to the Span Panel"
},
"auth_menu": {
"title": "Choose Authentication Options",
"menu_options": {
"auth_proximity": "Authenticate through your physical Span Panel",
"auth_token": "Authenticate using an access token"
}
},
"auth_proximity": {
"title": "Proximity Authentication",
"description": "Please open and close the Span Panel door {remaining} times."
},
"auth_token": {
"title": "Manual Token Authentication",
"description": "Please enter your access token:",
"data": {
"access_token": "Access Token"
}
}
}
}
"config": {
"abort": {
"no_devices_found": "No devices found on the network",
"already_configured": "Span Panel already configured. Only a single configuration is possible.",
"reauth_successful": "Authentication successful."
},
"error": {
"cannot_connect": "Failed to connect to Span Panel",
"invalid_auth": "Invalid authentication",
"unknown": "Unexpected error"
},
"flow_title": "Span Panel ({host})",
"step": {
"confirm_discovery": {
"description": "Do you want to setup Span Panel at {host}?",
"title": "Connect to the Span Panel"
},
"user": {
"data": {
"host": "Host"
},
"title": "Connect to the Span Panel"
},
"auth_menu": {
"title": "Choose Authentication Options",
"menu_options": {
"auth_proximity": "Authenticate through your physical Span Panel",
"auth_token": "Authenticate using an access token"
}
},
"auth_proximity": {
"title": "Proximity Authentication",
"description": "Please open and close the Span Panel door {remaining} times."
},
"auth_token": {
"title": "Manual Token Authentication",
"description": "Please enter your access token:",
"data": {
"access_token": "Access Token"
}
}
}
},
"options": {
"step": {
"init": {
"data": {
"scan_interval": "Scan interval in seconds"
}
}
}
}
}
9 changes: 9 additions & 0 deletions custom_components/span_panel/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,14 @@
}
}
}
},
"options": {
"step": {
"init": {
"data": {
"scan_interval": "Scan interval in seconds"
}
}
}
}
}

0 comments on commit ecf82f2

Please sign in to comment.