Skip to content

Commit

Permalink
feat: add polling interval service (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
Olen authored Jan 15, 2022
1 parent 33663fb commit 3b2d8bc
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions custom_components/tesla_custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ async def _async_update_data(self):
# Note: asyncio.TimeoutError and aiohttp.ClientError are already
# handled by the data update coordinator.
async with async_timeout.timeout(30):
_LOGGER.debug("Running controller.update()")
return await self.controller.update()
except IncompleteCredentials:
await self.hass.config_entries.async_reload(self.config_entry.entry_id)
Expand Down
1 change: 1 addition & 0 deletions custom_components/tesla_custom/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@
ATTR_PARAMETERS = "parameters"
ATTR_PATH_VARS = "path_vars"
SERVICE_API = "api"
SERVICE_SCAN_INTERVAL = "polling_interval"
68 changes: 67 additions & 1 deletion custom_components/tesla_custom/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
from homeassistant.helpers import config_validation as cv
import voluptuous as vol

from homeassistant.const import ATTR_COMMAND
from homeassistant.const import (
ATTR_COMMAND,
CONF_SCAN_INTERVAL,
)
from .const import (
ATTR_PARAMETERS,
ATTR_PATH_VARS,
DOMAIN,
SERVICE_API,
SERVICE_SCAN_INTERVAL,
DEFAULT_SCAN_INTERVAL,
)

_LOGGER = logging.getLogger(__name__)
Expand All @@ -30,6 +35,12 @@
}
)

SCAN_INTERVAL_SCHEMA = vol.Schema(
{
vol.Optional(CONF_EMAIL): vol.All(cv.string, vol.Length(min=1)),
vol.Required(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL): vol.All(vol.Coerce(int), vol.Range(min=0, max=3600)),
}
)

@callback
def async_setup_services(hass) -> None:
Expand All @@ -42,13 +53,23 @@ async def async_call_tesla_service(service_call) -> None:
if service == SERVICE_API:
await api(service_call)

if service == SERVICE_SCAN_INTERVAL:
await set_update_interval(service_call)

hass.services.async_register(
DOMAIN,
SERVICE_API,
async_call_tesla_service,
schema=API_SCHEMA,
)

hass.services.async_register(
DOMAIN,
SERVICE_SCAN_INTERVAL,
async_call_tesla_service,
schema=SCAN_INTERVAL_SCHEMA,
)

async def api(call):
"""Handle api service request.
Expand Down Expand Up @@ -88,8 +109,53 @@ async def api(call):
path_vars = parameters.pop(ATTR_PATH_VARS)
return await controller.api(name=command, path_vars=path_vars, **parameters)

async def set_update_interval(call):
"""Handle api service request.
Arguments:
call.CONF_EMAIL {str: ""} -- email, optional
call.CONF_SCAN_INTERVAL {int: 660} -- New scan interval
Returns:
bool -- True if new interval is set
"""
_LOGGER.debug("call %s", call)
service_data = call.data
email = service_data.get(CONF_EMAIL, "")

if len(hass.config_entries.async_entries(DOMAIN)) > 1 and not email:
raise ValueError("Email address missing")
controller: Controller = None
for entry in hass.config_entries.async_entries(DOMAIN):
if (
len(hass.config_entries.async_entries(DOMAIN)) > 1
and entry.title != email
):
continue
controller = hass.data[DOMAIN].get(entry.entry_id)["coordinator"].controller
if controller is None:
raise ValueError(f"No Tesla controllers found for email {email}")

update_interval = service_data.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
_LOGGER.debug(
"Service %s called with email: %s interval %s",
SERVICE_SCAN_INTERVAL,
email,
update_interval,
)
old_update_interval = controller.update_interval
controller.update_interval = update_interval
if old_update_interval != controller.update_interval:
_LOGGER.debug(
"Changing update_interval from %s to %s",
old_update_interval,
controller.update_interval,
)
return True

@callback
def async_unload_services(hass) -> None:
"""Unload Tesla services."""
hass.services.async_remove(DOMAIN, SERVICE_API)
hass.services.async_remove(DOMAIN, SERVICE_SCAN_INTERVAL)
15 changes: 15 additions & 0 deletions custom_components/tesla_custom/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,18 @@ api:
required: true
selector:
object:

polling_interval:
description: Set polling interval for updating fresh data from an awake car
fields:
scan_interval:
description: Number of seconds between each poll. See https://github.com/alandtse/tesla/wiki/Polling-policy more information.
example: 660
required: true
default: 660
selector:
number:
min: 0
max: 3600
step: 30
unit_of_measurement: "s"

0 comments on commit 3b2d8bc

Please sign in to comment.