Skip to content

Commit

Permalink
Merge pull request #116 from alandtse/dev
Browse files Browse the repository at this point in the history
2021-12-11
  • Loading branch information
alandtse authored Dec 11, 2021
2 parents 3a3c0fb + 3458fa9 commit 59c6d2c
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 195 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ This integration provides the following platforms:
- 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.

- Buttons - Horn and Flash lights
## Options

Tesla options are set via **Configuration** -> **Integrations** -> **Tesla** -> **Options**.
Expand Down
20 changes: 3 additions & 17 deletions custom_components/tesla_custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_DOMAIN,
CONF_SCAN_INTERVAL,
CONF_TOKEN,
CONF_USERNAME,
EVENT_HOMEASSISTANT_CLOSE,
)
from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.httpx_client import SERVER_SOFTWARE, USER_AGENT
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
import httpx
from teslajsonpy import Controller as TeslaAPI
from teslajsonpy.const import AUTH_DOMAIN
from teslajsonpy.exceptions import IncompleteCredentials, TeslaException
import voluptuous as vol

from .config_flow import CannotConnect, InvalidAuth, validate_input
from .const import (
Expand All @@ -39,21 +39,6 @@

_LOGGER = logging.getLogger(__name__)

CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_TOKEN): cv.string,
vol.Optional(
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
): vol.All(cv.positive_int, vol.Clamp(min=MIN_SCAN_INTERVAL)),
}
)
},
extra=vol.ALLOW_EXTRA,
)


@callback
def _async_save_tokens(hass, config_entry, access_token, refresh_token, expiration):
Expand Down Expand Up @@ -149,6 +134,7 @@ async def async_setup_entry(hass, config_entry):
refresh_token=config[CONF_TOKEN],
access_token=config[CONF_ACCESS_TOKEN],
expiration=config.get(CONF_EXPIRATION, 0),
auth_domain=config.get(CONF_DOMAIN, AUTH_DOMAIN),
update_interval=config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
),
Expand Down
52 changes: 52 additions & 0 deletions custom_components/tesla_custom/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Support for Tesla charger buttons."""
from custom_components.tesla_custom.const import ICONS
import logging

_LOGGER = logging.getLogger(__name__)

from homeassistant.components.button import ButtonEntity

from . import DOMAIN as TESLA_DOMAIN
from .tesla_device import TeslaDevice


async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Tesla button by config_entry."""
coordinator = hass.data[TESLA_DOMAIN][config_entry.entry_id]["coordinator"]
entities = []
for device in hass.data[TESLA_DOMAIN][config_entry.entry_id]["devices"]["button"]:
if device.type == "horn":
entities.append(Horn(device, coordinator))
elif device.type == "flash lights":
entities.append(FlashLights(device, coordinator))
async_add_entities(entities, True)


class Horn(TeslaDevice, ButtonEntity):
"""Representation of a Tesla horn button."""

def __init__(self, tesla_device, coordinator):
"""Initialise the button."""
super().__init__(tesla_device, coordinator)
self.controller = coordinator.controller

@TeslaDevice.Decorators.check_for_reauth
async def async_press(self, **kwargs):
"""Send the command."""
_LOGGER.debug("Honk horn: %s", self.name)
await self.tesla_device.honk_horn()


class FlashLights(TeslaDevice, ButtonEntity):
"""Representation of a Tesla flash lights button."""

def __init__(self, tesla_device, coordinator):
"""Initialise the button."""
super().__init__(tesla_device, coordinator)
self.controller = coordinator.controller

@TeslaDevice.Decorators.check_for_reauth
async def async_press(self, **kwargs):
"""Send the command."""
_LOGGER.debug("Flash lights: %s", self.name)
await self.tesla_device.flash_lights()
3 changes: 3 additions & 0 deletions custom_components/tesla_custom/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ async def async_set_temperature(self, **kwargs):
if temperature:
_LOGGER.debug("%s: Setting temperature to %s", self.name, temperature)
await self.tesla_device.set_temperature(temperature)
self.async_write_ha_state()

@TeslaDevice.Decorators.check_for_reauth
async def async_set_hvac_mode(self, hvac_mode):
Expand All @@ -96,13 +97,15 @@ async def async_set_hvac_mode(self, hvac_mode):
await self.tesla_device.set_status(False)
elif hvac_mode == HVAC_MODE_HEAT_COOL:
await self.tesla_device.set_status(True)
self.async_write_ha_state()

@TeslaDevice.Decorators.check_for_reauth
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
_LOGGER.debug("%s: Setting preset_mode to: %s", self.name, preset_mode)
try:
await self.tesla_device.set_preset_mode(preset_mode)
self.async_write_ha_state()
except UnknownPresetMode as ex:
_LOGGER.error("%s", ex.message)

Expand Down
9 changes: 7 additions & 2 deletions custom_components/tesla_custom/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from homeassistant import config_entries, core, exceptions
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_DOMAIN,
CONF_SCAN_INTERVAL,
CONF_TOKEN,
CONF_USERNAME,
Expand All @@ -14,6 +15,7 @@
from homeassistant.helpers.httpx_client import SERVER_SOFTWARE, USER_AGENT
import httpx
from teslajsonpy import Controller as TeslaAPI, TeslaException
from teslajsonpy.const import AUTH_DOMAIN
from teslajsonpy.exceptions import IncompleteCredentials
import voluptuous as vol

Expand Down Expand Up @@ -97,6 +99,7 @@ def _async_schema(self):
{
vol.Required(CONF_USERNAME, default=self.username): str,
vol.Required(CONF_TOKEN): str,
vol.Required(CONF_DOMAIN, default=AUTH_DOMAIN): str,
}
)

Expand Down Expand Up @@ -155,13 +158,15 @@ async def validate_input(hass: core.HomeAssistant, data):
email=data[CONF_USERNAME],
refresh_token=data[CONF_TOKEN],
update_interval=DEFAULT_SCAN_INTERVAL,
expiration=config.get(CONF_EXPIRATION, 0),
expiration=data.get(CONF_EXPIRATION, 0),
auth_domain=data.get(CONF_DOMAIN, AUTH_DOMAIN),
)
result = await controller.connect(test_login=True)
config[CONF_TOKEN] = result["refresh_token"]
config[CONF_ACCESS_TOKEN] = result["access_token"]
config[CONF_ACCESS_TOKEN] = result[CONF_ACCESS_TOKEN]
config[CONF_EXPIRATION] = result[CONF_EXPIRATION]
config[CONF_USERNAME] = data[CONF_USERNAME]
config[CONF_DOMAIN] = data.get(CONF_DOMAIN, AUTH_DOMAIN)

except IncompleteCredentials as ex:
_LOGGER.error("Authentication error: %s %s", ex.message, ex)
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tesla_custom/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"binary_sensor",
"device_tracker",
"switch",
"button",
]

ICONS = {
Expand All @@ -31,6 +32,8 @@
"location tracker": "mdi:crosshairs-gps",
"charging rate sensor": "mdi:speedometer",
"sentry mode switch": "mdi:shield-car",
"horn": "mdi:bullhorn",
"flash lights": "mdi:car-light-high",
"solar panel": "mdi:solar-panel",
}
AUTH_CALLBACK_PATH = "/auth/tesla/callback"
Expand Down
4 changes: 2 additions & 2 deletions custom_components/tesla_custom/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ async def async_lock(self, **kwargs):
"""Send the lock command."""
_LOGGER.debug("Locking doors for: %s", self.name)
await self.tesla_device.lock()
self.async_write_ha_state()

@TeslaDevice.Decorators.check_for_reauth
async def async_unlock(self, **kwargs):
"""Send the unlock command."""
_LOGGER.debug("Unlocking doors for: %s", self.name)
await self.tesla_device.unlock()
self.async_write_ha_state()

@property
def is_locked(self):
"""Get whether the lock is in locked state."""
if self.tesla_device.is_locked() is None:
return None
return self.tesla_device.is_locked()
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.2.1"],
"requirements": ["teslajsonpy==1.4.1"],
"codeowners": ["@alandtse"],
"dependencies": ["http"],
"dhcp": [
Expand Down
1 change: 1 addition & 0 deletions custom_components/tesla_custom/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def extra_state_attributes(self):
"added_range": self.tesla_device.added_range,
"charge_energy_added": self.tesla_device.charge_energy_added,
"charge_current_request": self.tesla_device.charge_current_request,
"charge_current_request_max": self.tesla_device.charge_current_request_max,
"charger_actual_current": self.tesla_device.charger_actual_current,
"charger_voltage": self.tesla_device.charger_voltage,
"charger_power": self.tesla_device.charger_power,
Expand Down
3 changes: 2 additions & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"climate",
"binary_sensor",
"device_tracker",
"switch"
"switch",
"button"
],
"iot_class": "Cloud Polling",
"homeassistant": "2021.4.0",
Expand Down
Loading

0 comments on commit 59c6d2c

Please sign in to comment.