Skip to content

Commit

Permalink
feat: add set polling policy in configuration (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
Olen authored Jan 15, 2022
1 parent 3b2d8bc commit 2814149
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 183 deletions.
16 changes: 8 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ repos:
rev: 5.7.0
hooks:
- id: isort
- repo: local
hooks:
- id: pytest-check
name: pytest-check
entry: pytest
language: system
pass_filenames: false
always_run: true
# - repo: local
# hooks:
# - id: pytest-check
# name: pytest-check
# entry: pytest
# language: system
# pass_filenames: false
# always_run: true
6 changes: 6 additions & 0 deletions custom_components/tesla_custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
from .config_flow import CannotConnect, InvalidAuth, validate_input
from .const import (
CONF_EXPIRATION,
CONF_POLLING_POLICY,
CONF_WAKE_ON_START,
DATA_LISTENER,
DEFAULT_POLLING_POLICY,
DEFAULT_SCAN_INTERVAL,
DEFAULT_WAKE_ON_START,
DOMAIN,
Expand Down Expand Up @@ -71,6 +73,7 @@ def _update_entry(email, data=None, options=None):
options = options or {
CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL,
CONF_WAKE_ON_START: DEFAULT_WAKE_ON_START,
CONF_POLLING_POLICY: DEFAULT_POLLING_POLICY,
}
for entry in hass.config_entries.async_entries(DOMAIN):
if email != entry.title:
Expand Down Expand Up @@ -138,6 +141,9 @@ async def async_setup_entry(hass, config_entry):
update_interval=config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
),
polling_policy=config_entry.options.get(
CONF_POLLING_POLICY, DEFAULT_POLLING_POLICY
),
)
result = await controller.connect(
wake_if_asleep=config_entry.options.get(
Expand Down
18 changes: 18 additions & 0 deletions custom_components/tesla_custom/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
import voluptuous as vol

from .const import (
ATTR_POLLING_POLICY_ALWAYS,
ATTR_POLLING_POLICY_CONNECTED,
ATTR_POLLING_POLICY_NORMAL,
CONF_EXPIRATION,
CONF_POLLING_POLICY,
CONF_WAKE_ON_START,
DEFAULT_POLLING_POLICY,
DEFAULT_SCAN_INTERVAL,
DEFAULT_WAKE_ON_START,
MIN_SCAN_INTERVAL,
Expand Down Expand Up @@ -138,6 +143,18 @@ async def async_step_init(self, user_input=None):
CONF_WAKE_ON_START, DEFAULT_WAKE_ON_START
),
): bool,
vol.Required(
CONF_POLLING_POLICY,
default=self.config_entry.options.get(
CONF_POLLING_POLICY, DEFAULT_POLLING_POLICY
),
): vol.In(
[
ATTR_POLLING_POLICY_NORMAL,
ATTR_POLLING_POLICY_CONNECTED,
ATTR_POLLING_POLICY_ALWAYS,
]
),
}
)
return self.async_show_form(step_id="init", data_schema=data_schema)
Expand All @@ -160,6 +177,7 @@ async def validate_input(hass: core.HomeAssistant, data):
update_interval=DEFAULT_SCAN_INTERVAL,
expiration=data.get(CONF_EXPIRATION, 0),
auth_domain=data.get(CONF_DOMAIN, AUTH_DOMAIN),
polling_policy=data.get(CONF_POLLING_POLICY, DEFAULT_POLLING_POLICY),
)
result = await controller.connect(test_login=True)
config[CONF_TOKEN] = result["refresh_token"]
Expand Down
5 changes: 5 additions & 0 deletions custom_components/tesla_custom/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
VERSION = "1.4.0"
CONF_WAKE_ON_START = "enable_wake_on_start"
CONF_EXPIRATION = "expiration"
CONF_POLLING_POLICY = "polling_policy"
DOMAIN = "tesla_custom"
DATA_LISTENER = "listener"
DEFAULT_SCAN_INTERVAL = 660
Expand Down Expand Up @@ -43,5 +44,9 @@

ATTR_PARAMETERS = "parameters"
ATTR_PATH_VARS = "path_vars"
ATTR_POLLING_POLICY_NORMAL = "normal"
ATTR_POLLING_POLICY_CONNECTED = "connected"
ATTR_POLLING_POLICY_ALWAYS = "always"
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.4.1"],
"requirements": ["teslajsonpy==1.5.0"],
"codeowners": ["@alandtse"],
"dependencies": ["http"],
"dhcp": [
Expand Down
Loading

0 comments on commit 2814149

Please sign in to comment.