-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for Heated Steering Wheel and Seats (#188)
- Loading branch information
1 parent
dc1ba93
commit c052539
Showing
5 changed files
with
212 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""Helpers module. | ||
A collection of functions which may be used accross entities | ||
""" | ||
from .const import DOMAIN as TESLA_DOMAIN | ||
|
||
import asyncio | ||
import async_timeout | ||
import logging | ||
|
||
from homeassistant.core import HomeAssistant | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def get_device( | ||
hass: HomeAssistant, | ||
config_entry_id: str, | ||
device_category: str, | ||
device_type: str, | ||
): | ||
"""Get a tesla Device for a Config Entry ID.""" | ||
|
||
entry_data = hass.data[TESLA_DOMAIN][config_entry_id] | ||
devices = entry_data["devices"].get(device_category, []) | ||
|
||
for device in devices: | ||
if device.type == device_type: | ||
return device | ||
|
||
return None | ||
|
||
|
||
async def wait_for_climate( | ||
hass: HomeAssistant, config_entry_id: str, timeout: int = 30 | ||
): | ||
"""Wait for HVac. | ||
Optional Timeout. defaults to 30 seconds | ||
""" | ||
climate_device = await get_device( | ||
hass, config_entry_id, "climate", "HVAC (climate) system" | ||
) | ||
|
||
if climate_device is None: | ||
return None | ||
|
||
async with async_timeout.timeout(timeout): | ||
while True: | ||
hvac_mode = climate_device.is_hvac_enabled() | ||
|
||
if hvac_mode is True: | ||
_LOGGER.debug("HVAC Enabled") | ||
return True | ||
else: | ||
_LOGGER.info("Enabing Climate to activate Heated Steering Wheel") | ||
|
||
# The below is a blocking funtion (it waits for a reponse from the API). | ||
# So it could eat into our timeout, and this is fine. | ||
# We'll try to turn the set the status, and check again | ||
try: | ||
await climate_device.set_status(True) | ||
continue | ||
except: | ||
# If we get an error, we'll just loop around and try again | ||
pass | ||
|
||
# Wait two second between API calls, in case we get an error like car unavail, | ||
# or any other random thing tesla throws at us | ||
await asyncio.sleep(2) | ||
|
||
# we'll return false if the timeout is reached. | ||
return False |
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,55 @@ | ||
"""Support for Tesla selects.""" | ||
import logging | ||
|
||
from homeassistant.components.select import SelectEntity | ||
|
||
from . import DOMAIN as TESLA_DOMAIN | ||
from .tesla_device import TeslaDevice | ||
from .helpers import wait_for_climate | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
OPTIONS = [ | ||
"Off", | ||
"Low", | ||
"Medium", | ||
"High", | ||
] | ||
|
||
|
||
async def async_setup_entry(hass, config_entry, async_add_entities): | ||
"""Set up the Tesla selects 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"]["select"]: | ||
if device.type.startswith("heated seat "): | ||
entities.append(HeatedSeatSelect(device, coordinator)) | ||
async_add_entities(entities, True) | ||
|
||
|
||
class HeatedSeatSelect(TeslaDevice, SelectEntity): | ||
"""Representation of a Tesla Heated Seat Select.""" | ||
|
||
@TeslaDevice.Decorators.check_for_reauth | ||
async def async_select_option(self, option: str, **kwargs): | ||
"""Change the selected option.""" | ||
level: int = OPTIONS.index(option) | ||
|
||
await wait_for_climate(self.hass, self.config_entry_id) | ||
_LOGGER.debug("Setting %s to %s", self.name, level) | ||
await self.tesla_device.set_seat_heat_level(level) | ||
self.async_write_ha_state() | ||
|
||
@property | ||
def current_option(self): | ||
"""Return the selected entity option to represent the entity state.""" | ||
current_value = self.tesla_device.get_seat_heat_level() | ||
|
||
if current_value is None: | ||
return OPTIONS[0] | ||
return OPTIONS[current_value] | ||
|
||
@property | ||
def options(self): | ||
"""Return a set of selectable options.""" | ||
return OPTIONS |
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