Skip to content

Commit

Permalink
feat: add horn and flash lights buttons (#114)
Browse files Browse the repository at this point in the history
This requires HA >= 2021.12.x to have access to the buttons. For older versions of HA, you will see an error we cannot hide: `[homeassistant.setup] Setup failed for button: Integration not found.`

Co-authored-by: raphael <raphael.dauchy@kwote.fr>
Co-authored-by: Raph <rafal83@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 8, 2021
1 parent 2f0c8e0 commit 1c39e63
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 113 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
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()
1 change: 0 additions & 1 deletion custom_components/tesla_custom/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ async def async_set_temperature(self, **kwargs):
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):
"""Set new target hvac mode."""
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
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.3.0"],
"requirements": ["teslajsonpy==1.4.0"],
"codeowners": ["@alandtse"],
"dependencies": ["http"],
"dhcp": [
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
135 changes: 28 additions & 107 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ license = "Apache-2.0"

[tool.poetry.dependencies]
python = "^3.9"
teslajsonpy = "^1.3.0"
teslajsonpy = "^1.4.0"

[tool.poetry.dev-dependencies]
homeassistant = "^2021.3.4"
pytest-homeassistant-custom-component = "~=0.3.1"
bandit = "^1.7.0"
black = {version = "^20.8b1", allow-prereleases = true}
black = {version = "^21.12b0", allow-prereleases = true}
mypy = "^0.812"
pre-commit = "^2.11.1"
pydocstyle = "^6.0.0"
Expand Down

0 comments on commit 1c39e63

Please sign in to comment.