Skip to content

Commit

Permalink
feat: add cooling seats (#994)
Browse files Browse the repository at this point in the history
closes #977
  • Loading branch information
dandelionclock authored Jun 9, 2024
1 parent f6953b7 commit 66fb89e
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 29 deletions.
2 changes: 1 addition & 1 deletion custom_components/tesla_custom/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/alandtse/tesla/issues",
"loggers": ["teslajsonpy"],
"requirements": ["teslajsonpy==3.11.0"],
"requirements": ["teslajsonpy==3.12.0"],
"version": "3.22.2"
}
118 changes: 97 additions & 21 deletions custom_components/tesla_custom/select.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Support for Tesla selects."""

import logging
import re

from homeassistant.components.select import SelectEntity
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -45,6 +46,17 @@
"Auto",
]

FRONT_COOL_HEAT_OPTIONS = [
"Off",
"Heat Low",
"Heat Medium",
"Heat High",
"Auto",
"Cool Low",
"Cool Medium",
"Cool High",
]

STEERING_HEATER_OPTIONS = [
"Off",
"Low",
Expand All @@ -70,6 +82,7 @@
"third row right": 7,
}

# Also used by cooled seats
AUTO_SEAT_ID_MAP = {
"left": 1,
"right": 2,
Expand Down Expand Up @@ -113,7 +126,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie


class TeslaCarHeatedSeat(TeslaCarEntity, SelectEntity):
"""Representation of a Tesla car heated seat select."""
"""Representation of a Tesla car heated/cooling seat select."""

_attr_icon = "mdi:car-seat-heater"

Expand All @@ -123,7 +136,7 @@ def __init__(
coordinator: TeslaDataUpdateCoordinator,
seat_name: str,
):
"""Initialize heated seat entity."""
"""Initialize heated/cooling seat entity."""
self._seat_name = seat_name
self.type = f"heated seat {seat_name}"
if SEAT_ID_MAP[self._seat_name] < 2:
Expand All @@ -134,51 +147,114 @@ def __init__(

async def async_select_option(self, option: str, **kwargs):
"""Change the selected option."""
# If selected auto
if self._is_auto_available and option == FRONT_HEATER_OPTIONS[4]:
_LOGGER.debug("Setting %s to %s", SEAT_ID_MAP[self._seat_name], option)
_LOGGER.debug("Setting %s to %s", self.name, option)
await self._car.remote_auto_seat_climate_request(
AUTO_SEAT_ID_MAP[self._seat_name], True
)
# If any options other than auto
else:
# First turn off auto if currently on
if self.current_option == FRONT_HEATER_OPTIONS[4]:
_LOGGER.debug("Turning off Auto heat/cool on %s", self.name)
await self._car.remote_auto_seat_climate_request(
AUTO_SEAT_ID_MAP[self._seat_name], False
)

level: int = HEATER_OPTIONS.index(option)

if not self._car.is_climate_on and level > 0:
await self._car.set_hvac_mode("on")

_LOGGER.debug("Setting %s to %s", self.name, level)
await self._car.remote_seat_heater_request(
level, SEAT_ID_MAP[self._seat_name]
)
# If front seat and car has seat cooling
if self._is_auto_available and self._car.has_seat_cooling:
level: int = FRONT_COOL_HEAT_OPTIONS.index(option)
if not self._car.is_climate_on and level > 0:
await self._car.set_hvac_mode("on")
# If turning off
if option == FRONT_COOL_HEAT_OPTIONS[0]:
_LOGGER.debug("Turning off Cooling/%s", self.name)
# If auto, turn off both heat and cool
if getattr(self._car, "is_auto_seat_climate_" + self._seat_name):
_LOGGER.debug(
"Currently on Auto, Turning off Both heat and cooling on Cooling/%s",
self.name,
)
await self._car.remote_seat_heater_request(
level, SEAT_ID_MAP[self._seat_name]
)
await self._car.remote_seat_cooler_request(
1, AUTO_SEAT_ID_MAP[self._seat_name]
)
# If heating, turn off heat
elif self._car.get_seat_heater_status(SEAT_ID_MAP[self._seat_name]):
await self._car.remote_seat_heater_request(
level, SEAT_ID_MAP[self._seat_name]
)
# If cooling, turn off cooling, 1 is off
else:
await self._car.remote_seat_cooler_request(
1, AUTO_SEAT_ID_MAP[self._seat_name]
)
# If heat levels selected
elif re.search("Heat", option):
_LOGGER.debug("Setting Cooling/%s to heat %s", self.name, level)
await self._car.remote_seat_heater_request(
level, SEAT_ID_MAP[self._seat_name]
)
# If cool levels selected
elif re.search("Cool", option):
# Cool Low == 2, Cool Medium == 3, Cool High ==4
level = level - (FRONT_COOL_HEAT_OPTIONS.index("Cool Low") - 2)
_LOGGER.debug("Setting Cooling/%s to cool %s", self.name, level)
await self._car.remote_seat_cooler_request(
level, AUTO_SEAT_ID_MAP[self._seat_name]
)
# If no seat cooling and not setting to auto
else:
level: int = HEATER_OPTIONS.index(option)
if not self._car.is_climate_on and level > 0:
await self._car.set_hvac_mode("on")
_LOGGER.debug("Setting %s to %s", self.name, level)
await self._car.remote_seat_heater_request(
level, SEAT_ID_MAP[self._seat_name]
)

await self.update_controller(force=True)

@property
def current_option(self):
"""Return current heated seat setting."""
"""Return current heated/cooling seat setting."""
if self._is_auto_available and getattr(
self._car, "is_auto_seat_climate_" + self._seat_name
):
current_value = 4
else:
return FRONT_HEATER_OPTIONS[current_value]
# If heated seats only
elif not self._car.has_seat_cooling:
current_value = self._car.get_seat_heater_status(
SEAT_ID_MAP[self._seat_name]
)

if current_value is None:
return HEATER_OPTIONS[0]

if self._is_auto_available:
if current_value is None:
return FRONT_HEATER_OPTIONS[0]
return FRONT_HEATER_OPTIONS[current_value]
return HEATER_OPTIONS[current_value]
# If cooling/heating front seats
else:
current_value = self._car.get_seat_heater_status(
SEAT_ID_MAP[self._seat_name]
)
if current_value is None:
current_value = self._car.get_seat_cooler_status(
AUTO_SEAT_ID_MAP[self._seat_name]
)
# Cooling seat level 1 is off
if current_value == 1:
current_value = 0
else:
# Low is 2 but is item 5 on select list
current_value = current_value + 3
return FRONT_COOL_HEAT_OPTIONS[current_value]

@property
def options(self):
"""Return heated seat options."""
if self._car.has_seat_cooling and self._is_auto_available:
return FRONT_COOL_HEAT_OPTIONS
if self._is_auto_available:
return FRONT_HEATER_OPTIONS
return HEATER_OPTIONS
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "Apache-2.0"

[tool.poetry.dependencies]
python = ">=3.12,<3.13"
teslajsonpy = "3.11.0"
teslajsonpy = "3.12.0"
async-timeout = ">=4.0.0"


Expand Down
2 changes: 1 addition & 1 deletion scripts/setup
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ poetry config virtualenvs.create false
poetry install --no-interaction

# Keep this inline with any requirements that are in manifest.json
pip install git+https://github.com/zabuldon/teslajsonpy.git@dev#teslajsonpy==3.11.0
pip install git+https://github.com/zabuldon/teslajsonpy.git@dev#teslajsonpy==3.12.0
pre-commit install --install-hooks
7 changes: 6 additions & 1 deletion tests/mock_data/car.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,13 @@
"passenger_temp_setting": 23.3,
"remote_heater_control_enabled": False,
"right_temp_direction": -309,
"seat_fan_front_left": 3,
"seat_fan_front_right": 1,
"seat_heater_left": 0,
"seat_heater_right": 0,
"seat_heater_rear_left": 0,
"seat_heater_rear_right": 0,
"seat_heater_rear_center": 0,
"side_mirror_heaters": False,
"supports_fan_only_cabin_overheat_protection": False,
"timestamp": 1661641175268,
Expand Down Expand Up @@ -166,7 +171,7 @@
"front_drive_unit": "NoneOrSmall",
"has_air_suspension": False,
"has_ludicrous_mode": False,
"has_seat_cooling": False,
"has_seat_cooling": True,
"headlamp_type": "Hid",
"interior_trim_type": "AllBlack",
"motorized_charge_port": True,
Expand Down
Loading

0 comments on commit 66fb89e

Please sign in to comment.