Skip to content

Commit

Permalink
feat: add Auto seat heater option (#404)
Browse files Browse the repository at this point in the history
closes #302
  • Loading branch information
InTheDaylight14 committed Dec 9, 2022
1 parent be6c1f0 commit 7e02a42
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
49 changes: 43 additions & 6 deletions custom_components/tesla_custom/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
"High",
]

FRONT_HEATER_OPTIONS = [
"Off",
"Low",
"Medium",
"High",
"Auto",
]

OPERATION_MODE = [
"Self-Powered",
"Time-Based Control",
Expand Down Expand Up @@ -100,31 +108,60 @@ def __init__(
self._seat_name = seat_name
self.type = f"heated seat {seat_name}"
self._attr_icon = "mdi:car-seat-heater"
if SEAT_ID_MAP[self._seat_name] < 2:
self._is_auto_available = True
else:
self._is_auto_available = False

async def async_select_option(self, option: str, **kwargs):
"""Change the selected option."""
level: int = HEATER_OPTIONS.index(option)
if self._is_auto_available and option == FRONT_HEATER_OPTIONS[4]:
_LOGGER.debug("Setting %s to %s", SEAT_ID_MAP[self._seat_name], option)
await self._car.remote_auto_seat_climate_request(
SEAT_ID_MAP[self._seat_name], True
)
else:
if self.current_option == FRONT_HEATER_OPTIONS[4]:
await self._car.remote_auto_seat_climate_request(
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")
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])
_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."""
current_value = self._car.get_seat_heater_status(SEAT_ID_MAP[self._seat_name])
if self._is_auto_available and getattr(
self._car, "is_auto_seat_climate_" + self._seat_name
):
current_value = 4
else:
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:
return FRONT_HEATER_OPTIONS[current_value]
return HEATER_OPTIONS[current_value]

@property
def options(self):
"""Return heated seat options."""
if self._is_auto_available:
return FRONT_HEATER_OPTIONS
return HEATER_OPTIONS


Expand Down
25 changes: 23 additions & 2 deletions tests/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,36 @@ async def test_car_heated_seat_select(hass: HomeAssistant) -> None:
)
mock_remote_seat_heater_request.assert_awaited_with(3, 0)

with patch(
"teslajsonpy.car.TeslaCar.remote_auto_seat_climate_request"
) as mock_remote_auto_seat_climate_request:
# Test selecting "Auto"
assert await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_ENTITY_ID: "select.my_model_s_heated_seat_left", "option": "Auto"},
blocking=True,
)
mock_remote_auto_seat_climate_request.assert_awaited_once_with(0, True)
# Test from "Auto" selection
car_mock_data.VEHICLE_DATA["climate_state"]["auto_seat_climate_left"] = True
assert await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_ENTITY_ID: "select.my_model_s_heated_seat_left", "option": "Low"},
blocking=True,
)
mock_remote_auto_seat_climate_request.assert_awaited_with(0, False)

with patch("teslajsonpy.car.TeslaCar.set_hvac_mode") as mock_set_hvac_mode:
# Test selecting "Low"
# Test climate_on check
assert await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_ENTITY_ID: "select.my_model_s_heated_seat_left", "option": "Low"},
blocking=True,
)
mock_set_hvac_mode.assert_awaited_with("on")
mock_set_hvac_mode.assert_awaited_once_with("on")


async def test_cabin_overheat_protection(hass: HomeAssistant) -> None:
Expand Down

0 comments on commit 7e02a42

Please sign in to comment.