From 25466ced9a918be549b325e2b066badacde9bb24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 26 Oct 2021 04:45:53 +0300 Subject: [PATCH 1/4] refactor: use http.HTTPStatus instead of const.HTTP_* (#87) Addressing future HA change: https://github.com/home-assistant/core/pull/58380 --- custom_components/tesla_custom/__init__.py | 4 ++-- custom_components/tesla_custom/config_flow.py | 4 ++-- tests/test_config_flow.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/custom_components/tesla_custom/__init__.py b/custom_components/tesla_custom/__init__.py index b07a8498..ecc70d87 100644 --- a/custom_components/tesla_custom/__init__.py +++ b/custom_components/tesla_custom/__init__.py @@ -2,6 +2,7 @@ import asyncio from collections import defaultdict from datetime import timedelta +from http import HTTPStatus import logging import async_timeout @@ -12,7 +13,6 @@ CONF_TOKEN, CONF_USERNAME, EVENT_HOMEASSISTANT_CLOSE, - HTTP_UNAUTHORIZED, ) from homeassistant.core import callback from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady @@ -169,7 +169,7 @@ async def async_setup_entry(hass, config_entry): raise ConfigEntryNotReady from ex except TeslaException as ex: await async_client.aclose() - if ex.code == HTTP_UNAUTHORIZED: + if ex.code == HTTPStatus.UNAUTHORIZED: raise ConfigEntryAuthFailed from ex if ex.message in [ "VEHICLE_UNAVAILABLE", diff --git a/custom_components/tesla_custom/config_flow.py b/custom_components/tesla_custom/config_flow.py index 12d78a02..e0eb211f 100644 --- a/custom_components/tesla_custom/config_flow.py +++ b/custom_components/tesla_custom/config_flow.py @@ -1,4 +1,5 @@ """Tesla Config Flow.""" +from http import HTTPStatus import logging from homeassistant import config_entries, core, exceptions @@ -7,7 +8,6 @@ CONF_SCAN_INTERVAL, CONF_TOKEN, CONF_USERNAME, - HTTP_UNAUTHORIZED, ) from homeassistant.core import callback from homeassistant.helpers import config_validation as cv @@ -167,7 +167,7 @@ async def validate_input(hass: core.HomeAssistant, data): _LOGGER.error("Authentication error: %s %s", ex.message, ex) raise InvalidAuth() from ex except TeslaException as ex: - if ex.code == HTTP_UNAUTHORIZED or isinstance(ex, IncompleteCredentials): + if ex.code == HTTPStatus.UNAUTHORIZED or isinstance(ex, IncompleteCredentials): _LOGGER.error("Invalid credentials: %s", ex.message) raise InvalidAuth() from ex _LOGGER.error("Unable to communicate with Tesla API: %s", ex.message) diff --git a/tests/test_config_flow.py b/tests/test_config_flow.py index 25ee9759..67cbc08e 100644 --- a/tests/test_config_flow.py +++ b/tests/test_config_flow.py @@ -1,5 +1,6 @@ """Test the Tesla config flow.""" import datetime +from http import HTTPStatus from unittest.mock import patch from homeassistant import config_entries, data_entry_flow, setup @@ -8,7 +9,6 @@ CONF_SCAN_INTERVAL, CONF_TOKEN, CONF_USERNAME, - HTTP_NOT_FOUND, ) from pytest_homeassistant_custom_component.common import MockConfigEntry from teslajsonpy.exceptions import IncompleteCredentials, TeslaException @@ -114,7 +114,7 @@ async def test_form_cannot_connect(hass): with patch( "custom_components.tesla_custom.config_flow.TeslaAPI.connect", - side_effect=TeslaException(code=HTTP_NOT_FOUND), + side_effect=TeslaException(code=HTTPStatus.NOT_FOUND), ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], From f289f401a9b5f3a7f0d7e3776b4e3e6ba52b0dd8 Mon Sep 17 00:00:00 2001 From: Andy Allsopp Date: Sat, 30 Oct 2021 00:28:41 +0100 Subject: [PATCH 2/4] Improve documentation (#94) * Update README.md Initial insert of 'usage' section to provide basic documentation for functionality. (Relates #65) * Update README.md Updated readme to reflect polling rather than scan, and updated some verbiage. --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index d0d7328f..e1f5a1b8 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,35 @@ To use the component, you will need an application to generate a Tesla refresh t 8. In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Tesla Custom Integration". +## Usage +The `Tesla` integration offers integration with the [Tesla](https://auth.tesla.com/login) cloud service and provides presence detection as well as sensors such as charger state and temperature. + + +This integration provides the following platforms: + +- Binary sensors - such as update available, parking, and charger connection. +- Sensors - such as Battery level, Inside/Outside temperature, odometer, estimated range, and charging rate. +- Device tracker - to track location of your car +- Locks - Door lock, rear trunk lock, front trunk (frunk) lock and charger door lock. Enables you to control Tesla's door, trunks and charger door lock. +- Climate - HVAC control. Allow you to control (turn on/off, set target temperature) your Tesla's HVAC system. Also enables preset modes to enable or disable max defrost mode `defrost` or `normal` operation mode. +- Switches - Charger and max range switch allow you to start/stop charging and set max range charging. Polling switch allows you to disable polling of vehicles to conserve battery. Sentry mode switch enables or disable Sentry mode. + +## Options + +Tesla options are set via **Configuration** -> **Integrations** -> **Tesla** -> **Options**. + +* Seconds between polling - referred to below as the `polling_interval`. + +* Wake cars on start - Whether to wake sleeping cars on Home Assistant startup. This allows a user to choose whether cars should continue to sleep (and not update information) or to wake up the cars potentially interrupting long term hibernation and increasing vampire drain. + +## Potential Battery impacts + +Here are some things to consider and understand when implementing the Tesla component and its potential effect on your car's battery. + +- The `polling_interval` determines when to check if the car is awake and new information is available, but the Tesla integration will not wake up a sleeping car during this polling. By default, the polling will occur every 660 seconds. Polling a car too frequently can keep the car awake and drain the battery. Different firmware versions and measurements of Tesla cars can take from 11 to 15 minutes for sleep mode to occur. There is no official information on sleep mode timings so your mileage may vary and you should experiment with different polling times for an optimal experience. +- The car will, however, be woken up when a command is actively sent to the car, such as door unlock or turning on the HVAC. It will then also fetch updated information while the car is awake based on the `polling_interval`. +- The car can intentionally be woken up to fetch recent information by sending a harmless command, for example, a lock command. This can be used in an automation to, for example, ensure that updated information is available every morning. (Note that the command must be valid for that specific car model. So locking the frunk of a Model 3 will not wake up that car). +- You can also toggle the `polling switch` on/off to disable polling of the vehicle completely via automations or the Lovelace UI. ## Contributions are welcome! From 4ae822755e13b8e358b84c43d10ddacc04ed2f16 Mon Sep 17 00:00:00 2001 From: Alan Tse Date: Wed, 3 Nov 2021 17:16:38 -0700 Subject: [PATCH 3/4] docs: add add-integration badge --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e1f5a1b8..d7e27907 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ To use the component, you will need an application to generate a Tesla refresh t 5. Download _all_ the files from the `custom_components/tesla_custom/` directory (folder) in this repository. 6. Place the files you downloaded in the new directory (folder) you created. 7. Restart Home Assistant. -8. In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Tesla Custom Integration". +8. [![Add Integration][add-integration-badge]][add-integration] or in the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Tesla Custom Integration". ## Usage @@ -90,3 +90,5 @@ _Component built with [integration_blueprint][integration_blueprint]._ [releases]: https://github.com/alandtse/tesla/releases [download-all]: https://img.shields.io/github/downloads/alandtse/tesla/total?style=for-the-badge [download-latest]: https://img.shields.io/github/downloads/alandtse/tesla/latest/total?style=for-the-badge +[add-integration]: https://my.home-assistant.io/redirect/config_flow_start?domain=tesla_custom +[add-integration-badge]: https://my.home-assistant.io/badges/config_flow_start.svg From 5dd66eca42fc03b08b30f8e52d877952fa37e9a3 Mon Sep 17 00:00:00 2001 From: Alan Tse Date: Wed, 3 Nov 2021 17:19:28 -0700 Subject: [PATCH 4/4] docs: add add integration link to info.md --- info.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/info.md b/info.md index 224760f1..2f606290 100644 --- a/info.md +++ b/info.md @@ -28,7 +28,7 @@ To use the component, you will need an application to generate a Tesla refresh t 1. Click install. 2. Reboot Home Assistant. 3. Hard refresh browser cache. -4. In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Tesla Custom Integration". If you are replacing core, remove the core integration before installing. +4. [![Add Integration][add-integration-badge]][add-integration] or in the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Tesla Custom Integration". {% endif %} @@ -57,3 +57,5 @@ _Component built with [integration_blueprint][integration_blueprint]._ [user_profile]: https://github.com/alandtse [download-all]: https://img.shields.io/github/downloads/alandtse/tesla/total?style=for-the-badge [download-latest]: https://img.shields.io/github/downloads/alandtse/tesla/latest/total?style=for-the-badge +[add-integration]: https://my.home-assistant.io/redirect/config_flow_start?domain=tesla_custom +[add-integration-badge]: https://my.home-assistant.io/badges/config_flow_start.svg