Add enable_wake_on_start option to Tesla (#33035)

This commit is contained in:
Alan Tse 2020-03-22 20:33:55 -07:00 committed by GitHub
parent d3f9408650
commit 087b672449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 35 deletions

View File

@ -9,8 +9,8 @@
"step": { "step": {
"user": { "user": {
"data": { "data": {
"password": "Password", "username": "Email Address",
"username": "Email Address" "password": "Password"
}, },
"description": "Please enter your information.", "description": "Please enter your information.",
"title": "Tesla - Configuration" "title": "Tesla - Configuration"
@ -22,7 +22,8 @@
"step": { "step": {
"init": { "init": {
"data": { "data": {
"scan_interval": "Seconds between scans" "scan_interval": "Seconds between scans",
"enable_wake_on_start": "Force cars awake on startup"
} }
} }
} }

View File

@ -28,8 +28,10 @@ from .config_flow import (
validate_input, validate_input,
) )
from .const import ( from .const import (
CONF_WAKE_ON_START,
DATA_LISTENER, DATA_LISTENER,
DEFAULT_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL,
DEFAULT_WAKE_ON_START,
DOMAIN, DOMAIN,
ICONS, ICONS,
MIN_SCAN_INTERVAL, MIN_SCAN_INTERVAL,
@ -71,7 +73,10 @@ async def async_setup(hass, base_config):
def _update_entry(email, data=None, options=None): def _update_entry(email, data=None, options=None):
data = data or {} data = data or {}
options = options or {CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL} options = options or {
CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL,
CONF_WAKE_ON_START: DEFAULT_WAKE_ON_START,
}
for entry in hass.config_entries.async_entries(DOMAIN): for entry in hass.config_entries.async_entries(DOMAIN):
if email != entry.title: if email != entry.title:
continue continue
@ -131,7 +136,11 @@ async def async_setup_entry(hass, config_entry):
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
), ),
) )
(refresh_token, access_token) = await controller.connect() (refresh_token, access_token) = await controller.connect(
wake_if_asleep=config_entry.options.get(
CONF_WAKE_ON_START, DEFAULT_WAKE_ON_START
)
)
except TeslaException as ex: except TeslaException as ex:
_LOGGER.error("Unable to communicate with Tesla API: %s", ex.message) _LOGGER.error("Unable to communicate with Tesla API: %s", ex.message)
return False return False

View File

@ -15,7 +15,13 @@ from homeassistant.const import (
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers import aiohttp_client, config_validation as cv from homeassistant.helpers import aiohttp_client, config_validation as cv
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, MIN_SCAN_INTERVAL from .const import (
CONF_WAKE_ON_START,
DEFAULT_SCAN_INTERVAL,
DEFAULT_WAKE_ON_START,
DOMAIN,
MIN_SCAN_INTERVAL,
)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -103,7 +109,13 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
default=self.config_entry.options.get( default=self.config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
), ),
): vol.All(cv.positive_int, vol.Clamp(min=MIN_SCAN_INTERVAL)) ): vol.All(cv.positive_int, vol.Clamp(min=MIN_SCAN_INTERVAL)),
vol.Optional(
CONF_WAKE_ON_START,
default=self.config_entry.options.get(
CONF_WAKE_ON_START, DEFAULT_WAKE_ON_START
),
): bool,
} }
) )
return self.async_show_form(step_id="init", data_schema=data_schema) return self.async_show_form(step_id="init", data_schema=data_schema)

View File

@ -1,7 +1,9 @@
"""Const file for Tesla cars.""" """Const file for Tesla cars."""
CONF_WAKE_ON_START = "enable_wake_on_start"
DOMAIN = "tesla" DOMAIN = "tesla"
DATA_LISTENER = "listener" DATA_LISTENER = "listener"
DEFAULT_SCAN_INTERVAL = 660 DEFAULT_SCAN_INTERVAL = 660
DEFAULT_WAKE_ON_START = False
MIN_SCAN_INTERVAL = 60 MIN_SCAN_INTERVAL = 60
TESLA_COMPONENTS = [ TESLA_COMPONENTS = [
"sensor", "sensor",

View File

@ -22,7 +22,8 @@
"step": { "step": {
"init": { "init": {
"data": { "data": {
"scan_interval": "Seconds between scans" "scan_interval": "Seconds between scans",
"enable_wake_on_start": "Force cars awake on startup"
} }
} }
} }

View File

@ -4,7 +4,13 @@ from unittest.mock import patch
from teslajsonpy import TeslaException from teslajsonpy import TeslaException
from homeassistant import config_entries, data_entry_flow, setup from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.components.tesla.const import DOMAIN, MIN_SCAN_INTERVAL from homeassistant.components.tesla.const import (
CONF_WAKE_ON_START,
DEFAULT_SCAN_INTERVAL,
DEFAULT_WAKE_ON_START,
DOMAIN,
MIN_SCAN_INTERVAL,
)
from homeassistant.const import ( from homeassistant.const import (
CONF_ACCESS_TOKEN, CONF_ACCESS_TOKEN,
CONF_PASSWORD, CONF_PASSWORD,
@ -137,10 +143,34 @@ async def test_option_flow(hass):
assert result["step_id"] == "init" assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure( result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={CONF_SCAN_INTERVAL: 350} result["flow_id"],
user_input={CONF_SCAN_INTERVAL: 350, CONF_WAKE_ON_START: True},
) )
assert result["type"] == "create_entry" assert result["type"] == "create_entry"
assert result["data"] == {CONF_SCAN_INTERVAL: 350} assert result["data"] == {
CONF_SCAN_INTERVAL: 350,
CONF_WAKE_ON_START: True,
}
async def test_option_flow_defaults(hass):
"""Test config flow options."""
entry = MockConfigEntry(domain=DOMAIN, data={}, options=None)
entry.add_to_hass(hass)
result = await hass.config_entries.options.async_init(entry.entry_id)
assert result["type"] == "form"
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={}
)
assert result["type"] == "create_entry"
assert result["data"] == {
CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL,
CONF_WAKE_ON_START: DEFAULT_WAKE_ON_START,
}
async def test_option_flow_input_floor(hass): async def test_option_flow_input_floor(hass):
@ -157,4 +187,7 @@ async def test_option_flow_input_floor(hass):
result["flow_id"], user_input={CONF_SCAN_INTERVAL: 1} result["flow_id"], user_input={CONF_SCAN_INTERVAL: 1}
) )
assert result["type"] == "create_entry" assert result["type"] == "create_entry"
assert result["data"] == {CONF_SCAN_INTERVAL: MIN_SCAN_INTERVAL} assert result["data"] == {
CONF_SCAN_INTERVAL: MIN_SCAN_INTERVAL,
CONF_WAKE_ON_START: DEFAULT_WAKE_ON_START,
}