forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Select platform to Tessie (home-assistant#105423)
* Add select platform * Add error coverage * Fix case * fix value * Remove virtual key issue * Add TessieSeatHeaterOptions enum and update TessieSeatHeaterSelectEntity options * use ENUM in tests * Porting other fixes * Update entity
- Loading branch information
Showing
5 changed files
with
204 additions
and
1 deletion.
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,58 @@ | ||
"""Select platform for Tessie integration.""" | ||
from __future__ import annotations | ||
|
||
from tessie_api import set_seat_heat | ||
|
||
from homeassistant.components.select import SelectEntity | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from .const import DOMAIN, TessieSeatHeaterOptions | ||
from .entity import TessieEntity | ||
|
||
SEAT_HEATERS = { | ||
"climate_state_seat_heater_left": "front_left", | ||
"climate_state_seat_heater_right": "front_right", | ||
"climate_state_seat_heater_rear_left": "rear_left", | ||
"climate_state_seat_heater_rear_center": "rear_center", | ||
"climate_state_seat_heater_rear_right": "rear_right", | ||
"climate_state_seat_heater_third_row_left": "third_row_left", | ||
"climate_state_seat_heater_third_row_right": "third_row_right", | ||
} | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback | ||
) -> None: | ||
"""Set up the Tessie select platform from a config entry.""" | ||
coordinators = hass.data[DOMAIN][entry.entry_id] | ||
|
||
async_add_entities( | ||
TessieSeatHeaterSelectEntity(coordinator, key) | ||
for coordinator in coordinators | ||
for key in SEAT_HEATERS | ||
if key in coordinator.data | ||
) | ||
|
||
|
||
class TessieSeatHeaterSelectEntity(TessieEntity, SelectEntity): | ||
"""Select entity for current charge.""" | ||
|
||
_attr_options = [ | ||
TessieSeatHeaterOptions.OFF, | ||
TessieSeatHeaterOptions.LOW, | ||
TessieSeatHeaterOptions.MEDIUM, | ||
TessieSeatHeaterOptions.HIGH, | ||
] | ||
|
||
@property | ||
def current_option(self) -> str | None: | ||
"""Return the current selected option.""" | ||
return self._attr_options[self._value] | ||
|
||
async def async_select_option(self, option: str) -> None: | ||
"""Change the selected option.""" | ||
level = self._attr_options.index(option) | ||
await self.run(set_seat_heat, seat=SEAT_HEATERS[self.key], level=level) | ||
self.set((self.key, level)) |
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,65 @@ | ||
"""Test the Tessie select platform.""" | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from homeassistant.components.select import ( | ||
DOMAIN as SELECT_DOMAIN, | ||
SERVICE_SELECT_OPTION, | ||
) | ||
from homeassistant.components.tessie.const import TessieSeatHeaterOptions | ||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_OPTION, STATE_OFF | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import HomeAssistantError | ||
|
||
from .common import ERROR_UNKNOWN, TEST_RESPONSE, setup_platform | ||
|
||
|
||
async def test_select(hass: HomeAssistant) -> None: | ||
"""Tests that the select entity is correct.""" | ||
|
||
assert len(hass.states.async_all(SELECT_DOMAIN)) == 0 | ||
|
||
await setup_platform(hass) | ||
|
||
assert len(hass.states.async_all(SELECT_DOMAIN)) == 5 | ||
|
||
entity_id = "select.test_seat_heater_left" | ||
assert hass.states.get(entity_id).state == STATE_OFF | ||
|
||
# Test changing select | ||
with patch( | ||
"homeassistant.components.tessie.select.set_seat_heat", | ||
return_value=TEST_RESPONSE, | ||
) as mock_set: | ||
await hass.services.async_call( | ||
SELECT_DOMAIN, | ||
SERVICE_SELECT_OPTION, | ||
{ATTR_ENTITY_ID: [entity_id], ATTR_OPTION: TessieSeatHeaterOptions.LOW}, | ||
blocking=True, | ||
) | ||
mock_set.assert_called_once() | ||
assert mock_set.call_args[1]["seat"] == "front_left" | ||
assert mock_set.call_args[1]["level"] == 1 | ||
assert hass.states.get(entity_id).state == TessieSeatHeaterOptions.LOW | ||
|
||
|
||
async def test_errors(hass: HomeAssistant) -> None: | ||
"""Tests unknown error is handled.""" | ||
|
||
await setup_platform(hass) | ||
entity_id = "select.test_seat_heater_left" | ||
|
||
# Test setting cover open with unknown error | ||
with patch( | ||
"homeassistant.components.tessie.select.set_seat_heat", | ||
side_effect=ERROR_UNKNOWN, | ||
) as mock_set, pytest.raises(HomeAssistantError) as error: | ||
await hass.services.async_call( | ||
SELECT_DOMAIN, | ||
SERVICE_SELECT_OPTION, | ||
{ATTR_ENTITY_ID: [entity_id], ATTR_OPTION: TessieSeatHeaterOptions.LOW}, | ||
blocking=True, | ||
) | ||
mock_set.assert_called_once() | ||
assert error.from_exception == ERROR_UNKNOWN |