Skip to content

Commit

Permalink
Configurable poll interval (#189)
Browse files Browse the repository at this point in the history
* Add configurable poll interval

* Add translations

* Add docs
  • Loading branch information
WebSpider authored Nov 8, 2024
1 parent 6b8f2d4 commit 92d4b59
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 14 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,23 @@ Pick any of the subjects from the example. If you want to enable full debugging,
- **custom_components.myskoda:** Set debug level for the custom component. The communication between hass and library.
- **custom_components.myskoda.XYZ** Sets debug level for individual entity types in the custom component.
## Customize polling interval
This integration does not poll at a set interval, instead when the last update has been a while, we request new information from MySkoda.
The reason for this is that cars emit a lot of events when they are operating, and we use these events to partially update the car information.
When the car is quiet for a period we call the **POLLING INTERVAL**, we will request a full update of the car.
By default, this POLLING INTERVAL is set to 30 minutes. You can tune this to anything between 1 and 1440 minutes (1 day) by filling in the desired value in
Integrations > MySkoda > Hubs > Select your account > Configure
## Disabling polling
You can disable polling completely and use automations to update the data from MySkoda. In order to do this, disable polling in the integration, and call the following action:
```yaml
action: homeassistant.update_entity
target:
entity_id: device_tracker.skoda_4ever
```
8 changes: 8 additions & 0 deletions custom_components/myskoda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
trace_configs = []
if config.options.get("tracing"):
trace_configs.append(TRACE_CONFIG)

session = async_create_clientsession(hass, trace_configs=trace_configs)
myskoda = MySkoda(session, get_default_context())

Expand All @@ -54,6 +55,7 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
hass.data[DOMAIN][config.entry_id] = {COORDINATORS: coordinators}

await hass.config_entries.async_forward_entry_setups(config, PLATFORMS)
config.async_on_unload(config.add_update_listener(_async_update_listener))

return True

Expand All @@ -65,3 +67,9 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok


async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry):
"""Handle options update."""
# Do a lazy reload of integration when configuration changed
await hass.config_entries.async_reload(entry.entry_id)
44 changes: 35 additions & 9 deletions custom_components/myskoda/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,45 @@
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.schema_config_entry_flow import (
SchemaCommonFlowHandler,
SchemaFlowError,
SchemaFlowFormStep,
SchemaOptionsFlowHandler,
)
from homeassistant.util.ssl import get_default_context
from myskoda import MySkoda

from .const import DOMAIN
from .const import (
DOMAIN,
CONF_POLL_INTERVAL,
CONF_POLL_INTERVAL_MIN,
CONF_POLL_INTERVAL_MAX,
)

_LOGGER = logging.getLogger(__name__)


async def validate_options_input(
handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
) -> dict[str, Any]:
"""Validate options are valid."""

if CONF_POLL_INTERVAL in user_input:
polling_interval: int = user_input[CONF_POLL_INTERVAL]
if CONF_POLL_INTERVAL_MIN <= polling_interval <= CONF_POLL_INTERVAL_MAX:
return user_input
raise SchemaFlowError("invalid_polling_interval")

return user_input


async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> None:
"""Check that the inputs are valid."""
hub = MySkoda(async_get_clientsession(hass), get_default_context())

await hub.connect(data["email"], data["password"])


STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required("email"): str,
Expand All @@ -37,20 +66,17 @@
OPTIONS_SCHEMA = vol.Schema(
{
vol.Required("tracing", default=False): bool,
vol.Optional(CONF_POLL_INTERVAL): int,
}
)
OPTIONS_FLOW = {
"init": SchemaFlowFormStep(OPTIONS_SCHEMA),
"init": SchemaFlowFormStep(
OPTIONS_SCHEMA,
validate_user_input=validate_options_input,
)
}


async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> None:
"""Check that the inputs are valid."""
hub = MySkoda(async_get_clientsession(hass), get_default_context())

await hub.connect(data["email"], data["password"])


class ConfigFlow(BaseConfigFlow, domain=DOMAIN):
"""Handle a config flow for MySkoda."""

Expand Down
6 changes: 5 additions & 1 deletion custom_components/myskoda/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
DOMAIN = "myskoda"
COORDINATORS = "coordinators"

FETCH_INTERVAL_IN_MINUTES = 30
DEFAULT_FETCH_INTERVAL_IN_MINUTES = 30
API_COOLDOWN_IN_SECONDS = 30.0

CONF_POLL_INTERVAL = "poll_interval_in_minutes"
CONF_POLL_INTERVAL_MIN = 1
CONF_POLL_INTERVAL_MAX = 1440
13 changes: 11 additions & 2 deletions custom_components/myskoda/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
from myskoda.models.user import User
from myskoda.mqtt import EventCharging, EventType

from .const import API_COOLDOWN_IN_SECONDS, DOMAIN, FETCH_INTERVAL_IN_MINUTES
from .const import (
API_COOLDOWN_IN_SECONDS,
CONF_POLL_INTERVAL,
DOMAIN,
DEFAULT_FETCH_INTERVAL_IN_MINUTES,
)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -65,7 +70,11 @@ def __init__(
hass,
_LOGGER,
name=DOMAIN,
update_interval=timedelta(minutes=FETCH_INTERVAL_IN_MINUTES),
update_interval=timedelta(
seconds=config.options.get(
CONF_POLL_INTERVAL, DEFAULT_FETCH_INTERVAL_IN_MINUTES
)
),
always_update=False,
)
self.hass = hass
Expand Down
11 changes: 9 additions & 2 deletions custom_components/myskoda/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,18 @@
}
},
"options": {
"error": {
"invalid_polling_interval": "Invalid polling interval specified. Please choose between 1 and 1440 minutes"
},
"step": {
"init": {
"data": {
"tracing": "API response tracing. Requires debug logging enabled in configuration.yaml."
}
"tracing": "API response tracing. Requires debug logging enabled in configuration.yaml.",
"poll_interval_in_minutes": "Polling interval in minutes when car is idle."
},
"data_description": {
"poll_interval_in_minutes": "Specify a polling interval between 1 and 1440 minutes. (default 30)"
}
}
}
}
Expand Down

0 comments on commit 92d4b59

Please sign in to comment.