-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Horn & FlashLight buttons (#252)
Co-authored-by: raphael <[email protected]>
- Loading branch information
Showing
6 changed files
with
158 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
""" | ||
Python Package for controlling Tesla API. | ||
For more details about this api, please refer to the documentation at | ||
https://github.com/zabuldon/teslajsonpy | ||
""" | ||
|
||
from teslajsonpy.homeassistant.vehicle import VehicleDevice | ||
|
||
|
||
class Horn(VehicleDevice): | ||
"""Home-Assistant class for horn of Tesla vehicles.""" | ||
|
||
def __init__(self, data, controller): | ||
"""Initialize the horn for the vehicle. | ||
Parameters | ||
---------- | ||
data : dict | ||
The horn for a Tesla vehicle. | ||
https://tesla-api.timdorr.com/vehicle/commands/alerts | ||
controller : teslajsonpy.Controller | ||
The controller that controls updates to the Tesla API. | ||
Returns | ||
------- | ||
None | ||
""" | ||
super().__init__(data, controller) | ||
self.type = "horn" | ||
self.hass_type = "button" | ||
self.name = self._name() | ||
self.uniq_name = self._uniq_name() | ||
|
||
async def async_update(self, wake_if_asleep=False, force=False): | ||
"""Update the horn of the vehicle.""" | ||
await super().async_update(wake_if_asleep=wake_if_asleep) | ||
self.refresh() | ||
|
||
@staticmethod | ||
def has_battery() -> bool: | ||
"""Return whether the device has a battery.""" | ||
return False | ||
|
||
async def honk_horn(self) -> None: | ||
"""Horn.""" | ||
await self._controller.api( | ||
"HONK_HORN", | ||
path_vars={"vehicle_id": self._id}, | ||
on=True, | ||
wake_if_asleep=True, | ||
) | ||
|
||
|
||
class FlashLights(VehicleDevice): | ||
"""Home-Assistant class for flash lights of Tesla vehicles.""" | ||
|
||
def __init__(self, data, controller): | ||
"""Initialize the flash lights for the vehicle. | ||
Parameters | ||
---------- | ||
data : dict | ||
The flash lights for a Tesla vehicle. | ||
https://tesla-api.timdorr.com/vehicle/commands/alerts | ||
controller : teslajsonpy.Controller | ||
The controller that controls updates to the Tesla API. | ||
Returns | ||
------- | ||
None | ||
""" | ||
super().__init__(data, controller) | ||
self.type = "flash lights" | ||
self.hass_type = "button" | ||
self.name = self._name() | ||
self.uniq_name = self._uniq_name() | ||
|
||
async def async_update(self, wake_if_asleep=False, force=False): | ||
"""Update the flash lights of the vehicle.""" | ||
await super().async_update(wake_if_asleep=wake_if_asleep) | ||
self.refresh() | ||
|
||
@staticmethod | ||
def has_battery() -> bool: | ||
"""Return whether the device has a battery.""" | ||
return False | ||
|
||
async def flash_lights(self) -> None: | ||
"""Flash Lights.""" | ||
await self._controller.api( | ||
"FLASH_LIGHTS", | ||
path_vars={"vehicle_id": self._id}, | ||
on=True, | ||
wake_if_asleep=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Test sentry mode switch.""" | ||
|
||
import pytest | ||
|
||
from teslajsonpy.controller import Controller | ||
from teslajsonpy.homeassistant.alerts import Horn, FlashLights | ||
|
||
from tests.tesla_mock import TeslaMock | ||
|
||
|
||
def test_has_battery(monkeypatch): | ||
"""Test has_battery().""" | ||
|
||
_mock = TeslaMock(monkeypatch) | ||
_controller = Controller(None) | ||
|
||
_data = _mock.data_request_vehicle() | ||
_button = Horn(_data, _controller) | ||
|
||
assert not _button.has_battery() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_honk_horn(monkeypatch): | ||
"""Test test_honk_horn().""" | ||
|
||
_mock = TeslaMock(monkeypatch) | ||
_controller = Controller(None) | ||
|
||
_data = _mock.data_request_vehicle() | ||
_button = Horn(_data, _controller) | ||
|
||
await _button.honk_horn() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_flash_light(monkeypatch): | ||
"""Test test_flash_light().""" | ||
|
||
_mock = TeslaMock(monkeypatch) | ||
_controller = Controller(None) | ||
|
||
_data = _mock.data_request_vehicle() | ||
_button = FlashLights(_data, _controller) | ||
|
||
await _button.flash_lights() |