Skip to content

Commit

Permalink
feat: add set by vin to update polling interval service (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehendrix23 authored Feb 23, 2022
1 parent 0d87757 commit f278680
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions custom_components/tesla_custom/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
ATTR_POLLING_POLICY_NORMAL = "normal"
ATTR_POLLING_POLICY_CONNECTED = "connected"
ATTR_POLLING_POLICY_ALWAYS = "always"
ATTR_VIN = "vin"
DEFAULT_POLLING_POLICY = ATTR_POLLING_POLICY_NORMAL
SERVICE_API = "api"
SERVICE_SCAN_INTERVAL = "polling_interval"
2 changes: 1 addition & 1 deletion custom_components/tesla_custom/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://github.com/alandtse/tesla",
"issue_tracker": "https://github.com/alandtse/tesla/issues",
"requirements": ["teslajsonpy==1.5.0"],
"requirements": ["teslajsonpy==1.8.0"],
"codeowners": ["@alandtse"],
"dependencies": ["http"],
"dhcp": [
Expand Down
20 changes: 13 additions & 7 deletions custom_components/tesla_custom/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .const import (
ATTR_PARAMETERS,
ATTR_PATH_VARS,
ATTR_VIN,
DOMAIN,
SERVICE_API,
SERVICE_SCAN_INTERVAL,
Expand All @@ -38,7 +39,8 @@
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)),
vol.Optional(ATTR_VIN): 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=-1, max=3600)),
}
)

Expand Down Expand Up @@ -114,6 +116,7 @@ async def set_update_interval(call):
Arguments:
call.CONF_EMAIL {str: ""} -- email, optional
call.ATTR_VIN {str: ""} -- vehicle VIN, optional
call.CONF_SCAN_INTERVAL {int: 660} -- New scan interval
Returns:
Expand All @@ -137,21 +140,24 @@ async def set_update_interval(call):
if controller is None:
raise ValueError(f"No Tesla controllers found for email {email}")

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

@callback
Expand Down
18 changes: 17 additions & 1 deletion custom_components/tesla_custom/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,30 @@ api:
polling_interval:
description: Set polling interval for updating fresh data from an awake car
fields:
email:
# Description of the field
description: Email address (optional if only one account)
# Example value that can be passed for this field
example: "elon@tesla.com"
required: false
selector:
text:
vin:
# Description of the field
description: Vehicle VIN (if not provided then default polling interval will be updated)
# Example value that can be passed for this field
example: "5YJSA11111111111"
required: false
selector:
text:
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
min: -1
max: 3600
step: 30
unit_of_measurement: "s"

0 comments on commit f278680

Please sign in to comment.