From e3c2f81506aaa566445a8191d0679e56580eaaa2 Mon Sep 17 00:00:00 2001 From: Brett Adams Date: Mon, 16 Sep 2024 20:26:11 +1000 Subject: [PATCH] Add select platform to Tesla Fleet (#125931) * Add Select Platform * Add Select strings and icons * Add tests * Clean up fixture --- .../components/tesla_fleet/__init__.py | 1 + .../components/tesla_fleet/icons.json | 60 ++ .../components/tesla_fleet/select.py | 264 ++++++++ .../components/tesla_fleet/strings.json | 89 +++ .../tesla_fleet/snapshots/test_select.ambr | 585 ++++++++++++++++++ tests/components/tesla_fleet/test_select.py | 136 ++++ 6 files changed, 1135 insertions(+) create mode 100644 homeassistant/components/tesla_fleet/select.py create mode 100644 tests/components/tesla_fleet/snapshots/test_select.ambr create mode 100644 tests/components/tesla_fleet/test_select.py diff --git a/homeassistant/components/tesla_fleet/__init__.py b/homeassistant/components/tesla_fleet/__init__.py index 61a1d02c35564d..bfd1c8907edeb6 100644 --- a/homeassistant/components/tesla_fleet/__init__.py +++ b/homeassistant/components/tesla_fleet/__init__.py @@ -43,6 +43,7 @@ Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.DEVICE_TRACKER, + Platform.SELECT, Platform.SENSOR, Platform.SWITCH, ] diff --git a/homeassistant/components/tesla_fleet/icons.json b/homeassistant/components/tesla_fleet/icons.json index d25346fe2a718c..5927acaa1d9e17 100644 --- a/homeassistant/components/tesla_fleet/icons.json +++ b/homeassistant/components/tesla_fleet/icons.json @@ -60,6 +60,66 @@ "default": "mdi:routes" } }, + "select": { + "climate_state_seat_heater_left": { + "default": "mdi:car-seat-heater", + "state": { + "off": "mdi:car-seat" + } + }, + "climate_state_seat_heater_rear_center": { + "default": "mdi:car-seat-heater", + "state": { + "off": "mdi:car-seat" + } + }, + "climate_state_seat_heater_rear_left": { + "default": "mdi:car-seat-heater", + "state": { + "off": "mdi:car-seat" + } + }, + "climate_state_seat_heater_rear_right": { + "default": "mdi:car-seat-heater", + "state": { + "off": "mdi:car-seat" + } + }, + "climate_state_seat_heater_right": { + "default": "mdi:car-seat-heater", + "state": { + "off": "mdi:car-seat" + } + }, + "climate_state_seat_heater_third_row_left": { + "default": "mdi:car-seat-heater", + "state": { + "off": "mdi:car-seat" + } + }, + "climate_state_seat_heater_third_row_right": { + "default": "mdi:car-seat-heater", + "state": { + "off": "mdi:car-seat" + } + }, + "components_customer_preferred_export_rule": { + "default": "mdi:transmission-tower", + "state": { + "battery_ok": "mdi:battery-negative", + "never": "mdi:transmission-tower-off", + "pv_only": "mdi:solar-panel" + } + }, + "default_real_mode": { + "default": "mdi:home-battery", + "state": { + "autonomous": "mdi:auto-fix", + "backup": "mdi:battery-charging-100", + "self_consumption": "mdi:home-battery" + } + } + }, "sensor": { "battery_power": { "default": "mdi:home-battery" diff --git a/homeassistant/components/tesla_fleet/select.py b/homeassistant/components/tesla_fleet/select.py new file mode 100644 index 00000000000000..515a0e7c2e76de --- /dev/null +++ b/homeassistant/components/tesla_fleet/select.py @@ -0,0 +1,264 @@ +"""Select platform for Tesla Fleet integration.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass +from itertools import chain + +from tesla_fleet_api.const import EnergyExportMode, EnergyOperationMode, Scope, Seat + +from homeassistant.components.select import SelectEntity, SelectEntityDescription +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import TeslaFleetConfigEntry +from .entity import TeslaFleetEnergyInfoEntity, TeslaFleetVehicleEntity +from .helpers import handle_command, handle_vehicle_command +from .models import TeslaFleetEnergyData, TeslaFleetVehicleData + +OFF = "off" +LOW = "low" +MEDIUM = "medium" +HIGH = "high" + +PARALLEL_UPDATES = 0 + + +@dataclass(frozen=True, kw_only=True) +class SeatHeaterDescription(SelectEntityDescription): + """Seat Heater entity description.""" + + position: Seat + available_fn: Callable[[TeslaFleetSeatHeaterSelectEntity], bool] = lambda _: True + + +SEAT_HEATER_DESCRIPTIONS: tuple[SeatHeaterDescription, ...] = ( + SeatHeaterDescription( + key="climate_state_seat_heater_left", + position=Seat.FRONT_LEFT, + ), + SeatHeaterDescription( + key="climate_state_seat_heater_right", + position=Seat.FRONT_RIGHT, + ), + SeatHeaterDescription( + key="climate_state_seat_heater_rear_left", + position=Seat.REAR_LEFT, + available_fn=lambda self: self.get("vehicle_config_rear_seat_heaters") != 0, + entity_registry_enabled_default=False, + ), + SeatHeaterDescription( + key="climate_state_seat_heater_rear_center", + position=Seat.REAR_CENTER, + available_fn=lambda self: self.get("vehicle_config_rear_seat_heaters") != 0, + entity_registry_enabled_default=False, + ), + SeatHeaterDescription( + key="climate_state_seat_heater_rear_right", + position=Seat.REAR_RIGHT, + available_fn=lambda self: self.get("vehicle_config_rear_seat_heaters") != 0, + entity_registry_enabled_default=False, + ), + SeatHeaterDescription( + key="climate_state_seat_heater_third_row_left", + position=Seat.THIRD_LEFT, + available_fn=lambda self: self.get("vehicle_config_third_row_seats") != "None", + entity_registry_enabled_default=False, + ), + SeatHeaterDescription( + key="climate_state_seat_heater_third_row_right", + position=Seat.THIRD_RIGHT, + available_fn=lambda self: self.get("vehicle_config_third_row_seats") != "None", + entity_registry_enabled_default=False, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: TeslaFleetConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the TeslaFleet select platform from a config entry.""" + + async_add_entities( + chain( + ( + TeslaFleetSeatHeaterSelectEntity( + vehicle, description, entry.runtime_data.scopes + ) + for description in SEAT_HEATER_DESCRIPTIONS + for vehicle in entry.runtime_data.vehicles + ), + ( + TeslaFleetWheelHeaterSelectEntity(vehicle, entry.runtime_data.scopes) + for vehicle in entry.runtime_data.vehicles + ), + ( + TeslaFleetOperationSelectEntity(energysite, entry.runtime_data.scopes) + for energysite in entry.runtime_data.energysites + if energysite.info_coordinator.data.get("components_battery") + ), + ( + TeslaFleetExportRuleSelectEntity(energysite, entry.runtime_data.scopes) + for energysite in entry.runtime_data.energysites + if energysite.info_coordinator.data.get("components_battery") + and energysite.info_coordinator.data.get("components_solar") + ), + ) + ) + + +class TeslaFleetSeatHeaterSelectEntity(TeslaFleetVehicleEntity, SelectEntity): + """Select entity for vehicle seat heater.""" + + entity_description: SeatHeaterDescription + + _attr_options = [ + OFF, + LOW, + MEDIUM, + HIGH, + ] + + def __init__( + self, + data: TeslaFleetVehicleData, + description: SeatHeaterDescription, + scopes: list[Scope], + ) -> None: + """Initialize the vehicle seat select entity.""" + self.entity_description = description + self.scoped = Scope.VEHICLE_CMDS in scopes + super().__init__(data, description.key) + + def _async_update_attrs(self) -> None: + """Handle updated data from the coordinator.""" + self._attr_available = self.entity_description.available_fn(self) + value = self._value + if value is None: + self._attr_current_option = None + else: + self._attr_current_option = self._attr_options[value] + + async def async_select_option(self, option: str) -> None: + """Change the selected option.""" + self.raise_for_read_only(Scope.VEHICLE_CMDS) + await self.wake_up_if_asleep() + level = self._attr_options.index(option) + # AC must be on to turn on seat heater + if level and not self.get("climate_state_is_climate_on"): + await handle_vehicle_command(self.api.auto_conditioning_start()) + await handle_vehicle_command( + self.api.remote_seat_heater_request(self.entity_description.position, level) + ) + self._attr_current_option = option + self.async_write_ha_state() + + +class TeslaFleetWheelHeaterSelectEntity(TeslaFleetVehicleEntity, SelectEntity): + """Select entity for vehicle steering wheel heater.""" + + _attr_options = [ + OFF, + LOW, + HIGH, + ] + + def __init__( + self, + data: TeslaFleetVehicleData, + scopes: list[Scope], + ) -> None: + """Initialize the vehicle steering wheel select entity.""" + self.scoped = Scope.VEHICLE_CMDS in scopes + super().__init__( + data, + "climate_state_steering_wheel_heat_level", + ) + + def _async_update_attrs(self) -> None: + """Handle updated data from the coordinator.""" + + value = self._value + if value is None: + self._attr_current_option = None + else: + self._attr_current_option = self._attr_options[value] + + async def async_select_option(self, option: str) -> None: + """Change the selected option.""" + self.raise_for_read_only(Scope.VEHICLE_CMDS) + await self.wake_up_if_asleep() + level = self._attr_options.index(option) + # AC must be on to turn on steering wheel heater + if level and not self.get("climate_state_is_climate_on"): + await handle_vehicle_command(self.api.auto_conditioning_start()) + await handle_vehicle_command( + self.api.remote_steering_wheel_heat_level_request(level) + ) + self._attr_current_option = option + self.async_write_ha_state() + + +class TeslaFleetOperationSelectEntity(TeslaFleetEnergyInfoEntity, SelectEntity): + """Select entity for operation mode select entities.""" + + _attr_options: list[str] = [ + EnergyOperationMode.AUTONOMOUS, + EnergyOperationMode.BACKUP, + EnergyOperationMode.SELF_CONSUMPTION, + ] + + def __init__( + self, + data: TeslaFleetEnergyData, + scopes: list[Scope], + ) -> None: + """Initialize the operation mode select entity.""" + self.scoped = Scope.ENERGY_CMDS in scopes + super().__init__(data, "default_real_mode") + + def _async_update_attrs(self) -> None: + """Update the attributes of the entity.""" + self._attr_current_option = self._value + + async def async_select_option(self, option: str) -> None: + """Change the selected option.""" + self.raise_for_read_only(Scope.ENERGY_CMDS) + await handle_command(self.api.operation(option)) + self._attr_current_option = option + self.async_write_ha_state() + + +class TeslaFleetExportRuleSelectEntity(TeslaFleetEnergyInfoEntity, SelectEntity): + """Select entity for export rules select entities.""" + + _attr_options: list[str] = [ + EnergyExportMode.NEVER, + EnergyExportMode.BATTERY_OK, + EnergyExportMode.PV_ONLY, + ] + + def __init__( + self, + data: TeslaFleetEnergyData, + scopes: list[Scope], + ) -> None: + """Initialize the export rules select entity.""" + self.scoped = Scope.ENERGY_CMDS in scopes + super().__init__(data, "components_customer_preferred_export_rule") + + def _async_update_attrs(self) -> None: + """Update the attributes of the entity.""" + self._attr_current_option = self.get(self.key, EnergyExportMode.NEVER.value) + + async def async_select_option(self, option: str) -> None: + """Change the selected option.""" + self.raise_for_read_only(Scope.ENERGY_CMDS) + await handle_command( + self.api.grid_import_export(customer_preferred_export_rule=option) + ) + self._attr_current_option = option + self.async_write_ha_state() diff --git a/homeassistant/components/tesla_fleet/strings.json b/homeassistant/components/tesla_fleet/strings.json index 8a70fe0997a894..25011cd6d4590b 100644 --- a/homeassistant/components/tesla_fleet/strings.json +++ b/homeassistant/components/tesla_fleet/strings.json @@ -133,6 +133,95 @@ "name": "Route" } }, + "select": { + "climate_state_seat_heater_left": { + "name": "Seat heater front left", + "state": { + "high": "High", + "low": "Low", + "medium": "Medium", + "off": "Off" + } + }, + "climate_state_seat_heater_rear_center": { + "name": "Seat heater rear center", + "state": { + "high": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::high%]", + "low": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::low%]", + "medium": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::medium%]", + "off": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::off%]" + } + }, + "climate_state_seat_heater_rear_left": { + "name": "Seat heater rear left", + "state": { + "high": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::high%]", + "low": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::low%]", + "medium": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::medium%]", + "off": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::off%]" + } + }, + "climate_state_seat_heater_rear_right": { + "name": "Seat heater rear right", + "state": { + "high": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::high%]", + "low": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::low%]", + "medium": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::medium%]", + "off": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::off%]" + } + }, + "climate_state_seat_heater_right": { + "name": "Seat heater front right", + "state": { + "high": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::high%]", + "low": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::low%]", + "medium": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::medium%]", + "off": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::off%]" + } + }, + "climate_state_seat_heater_third_row_left": { + "name": "Seat heater third row left", + "state": { + "high": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::high%]", + "low": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::low%]", + "medium": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::medium%]", + "off": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::off%]" + } + }, + "climate_state_seat_heater_third_row_right": { + "name": "Seat heater third row right", + "state": { + "high": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::high%]", + "low": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::low%]", + "medium": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::medium%]", + "off": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::off%]" + } + }, + "climate_state_steering_wheel_heat_level": { + "name": "Steering wheel heater", + "state": { + "high": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::high%]", + "low": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::low%]", + "off": "[%key:component::tesla_fleet::entity::select::climate_state_seat_heater_left::state::off%]" + } + }, + "components_customer_preferred_export_rule": { + "name": "Allow export", + "state": { + "battery_ok": "Battery", + "never": "Never", + "pv_only": "Solar only" + } + }, + "default_real_mode": { + "name": "Operation mode", + "state": { + "autonomous": "Autonomous", + "backup": "Backup", + "self_consumption": "Self consumption" + } + } + }, "sensor": { "battery_power": { "name": "Battery power" diff --git a/tests/components/tesla_fleet/snapshots/test_select.ambr b/tests/components/tesla_fleet/snapshots/test_select.ambr new file mode 100644 index 00000000000000..f29ce8411131ba --- /dev/null +++ b/tests/components/tesla_fleet/snapshots/test_select.ambr @@ -0,0 +1,585 @@ +# serializer version: 1 +# name: test_select[select.energy_site_allow_export-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + , + , + , + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': None, + 'entity_id': 'select.energy_site_allow_export', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Allow export', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'components_customer_preferred_export_rule', + 'unique_id': '123456-components_customer_preferred_export_rule', + 'unit_of_measurement': None, + }) +# --- +# name: test_select[select.energy_site_allow_export-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Energy Site Allow export', + 'options': list([ + , + , + , + ]), + }), + 'context': , + 'entity_id': 'select.energy_site_allow_export', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'pv_only', + }) +# --- +# name: test_select[select.energy_site_operation_mode-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + , + , + , + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': None, + 'entity_id': 'select.energy_site_operation_mode', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Operation mode', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'default_real_mode', + 'unique_id': '123456-default_real_mode', + 'unit_of_measurement': None, + }) +# --- +# name: test_select[select.energy_site_operation_mode-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Energy Site Operation mode', + 'options': list([ + , + , + , + ]), + }), + 'context': , + 'entity_id': 'select.energy_site_operation_mode', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'self_consumption', + }) +# --- +# name: test_select[select.test_seat_heater_front_left-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': None, + 'entity_id': 'select.test_seat_heater_front_left', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Seat heater front left', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'climate_state_seat_heater_left', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_seat_heater_left', + 'unit_of_measurement': None, + }) +# --- +# name: test_select[select.test_seat_heater_front_left-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Seat heater front left', + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'context': , + 'entity_id': 'select.test_seat_heater_front_left', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_select[select.test_seat_heater_front_right-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': None, + 'entity_id': 'select.test_seat_heater_front_right', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Seat heater front right', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'climate_state_seat_heater_right', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_seat_heater_right', + 'unit_of_measurement': None, + }) +# --- +# name: test_select[select.test_seat_heater_front_right-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Seat heater front right', + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'context': , + 'entity_id': 'select.test_seat_heater_front_right', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_select[select.test_seat_heater_rear_center-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': None, + 'entity_id': 'select.test_seat_heater_rear_center', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Seat heater rear center', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'climate_state_seat_heater_rear_center', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_seat_heater_rear_center', + 'unit_of_measurement': None, + }) +# --- +# name: test_select[select.test_seat_heater_rear_center-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Seat heater rear center', + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'context': , + 'entity_id': 'select.test_seat_heater_rear_center', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_select[select.test_seat_heater_rear_left-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': None, + 'entity_id': 'select.test_seat_heater_rear_left', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Seat heater rear left', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'climate_state_seat_heater_rear_left', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_seat_heater_rear_left', + 'unit_of_measurement': None, + }) +# --- +# name: test_select[select.test_seat_heater_rear_left-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Seat heater rear left', + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'context': , + 'entity_id': 'select.test_seat_heater_rear_left', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_select[select.test_seat_heater_rear_right-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': None, + 'entity_id': 'select.test_seat_heater_rear_right', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Seat heater rear right', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'climate_state_seat_heater_rear_right', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_seat_heater_rear_right', + 'unit_of_measurement': None, + }) +# --- +# name: test_select[select.test_seat_heater_rear_right-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Seat heater rear right', + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'context': , + 'entity_id': 'select.test_seat_heater_rear_right', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_select[select.test_seat_heater_third_row_left-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': None, + 'entity_id': 'select.test_seat_heater_third_row_left', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Seat heater third row left', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'climate_state_seat_heater_third_row_left', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_seat_heater_third_row_left', + 'unit_of_measurement': None, + }) +# --- +# name: test_select[select.test_seat_heater_third_row_left-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Seat heater third row left', + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'context': , + 'entity_id': 'select.test_seat_heater_third_row_left', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unavailable', + }) +# --- +# name: test_select[select.test_seat_heater_third_row_right-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': None, + 'entity_id': 'select.test_seat_heater_third_row_right', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Seat heater third row right', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'climate_state_seat_heater_third_row_right', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_seat_heater_third_row_right', + 'unit_of_measurement': None, + }) +# --- +# name: test_select[select.test_seat_heater_third_row_right-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Seat heater third row right', + 'options': list([ + 'off', + 'low', + 'medium', + 'high', + ]), + }), + 'context': , + 'entity_id': 'select.test_seat_heater_third_row_right', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unavailable', + }) +# --- +# name: test_select[select.test_steering_wheel_heater-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'off', + 'low', + 'high', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': None, + 'entity_id': 'select.test_steering_wheel_heater', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Steering wheel heater', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'climate_state_steering_wheel_heat_level', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_steering_wheel_heat_level', + 'unit_of_measurement': None, + }) +# --- +# name: test_select[select.test_steering_wheel_heater-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Steering wheel heater', + 'options': list([ + 'off', + 'low', + 'high', + ]), + }), + 'context': , + 'entity_id': 'select.test_steering_wheel_heater', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/tesla_fleet/test_select.py b/tests/components/tesla_fleet/test_select.py new file mode 100644 index 00000000000000..902b28ddb7a2dc --- /dev/null +++ b/tests/components/tesla_fleet/test_select.py @@ -0,0 +1,136 @@ +"""Test the Tesla Fleet select platform.""" + +from unittest.mock import AsyncMock, patch + +import pytest +from syrupy import SnapshotAssertion +from tesla_fleet_api.const import EnergyExportMode, EnergyOperationMode +from tesla_fleet_api.exceptions import VehicleOffline + +from homeassistant.components.select import ( + ATTR_OPTION, + DOMAIN as SELECT_DOMAIN, + SERVICE_SELECT_OPTION, +) +from homeassistant.components.tesla_fleet.select import LOW +from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import assert_entities, setup_platform +from .const import COMMAND_OK, VEHICLE_DATA_ALT + +from tests.common import MockConfigEntry + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_select( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, + normal_config_entry: MockConfigEntry, +) -> None: + """Tests that the select entities are correct.""" + + await setup_platform(hass, normal_config_entry, [Platform.SELECT]) + assert_entities(hass, normal_config_entry.entry_id, entity_registry, snapshot) + + +async def test_select_offline( + hass: HomeAssistant, + mock_vehicle_data: AsyncMock, + normal_config_entry: MockConfigEntry, +) -> None: + """Tests that the select entities are correct when offline.""" + + mock_vehicle_data.side_effect = VehicleOffline + await setup_platform(hass, normal_config_entry, [Platform.SELECT]) + state = hass.states.get("select.test_seat_heater_front_left") + assert state.state == STATE_UNKNOWN + + +async def test_select_services( + hass: HomeAssistant, + mock_vehicle_data: AsyncMock, + normal_config_entry: MockConfigEntry, +) -> None: + """Tests that the select services work.""" + mock_vehicle_data.return_value = VEHICLE_DATA_ALT + await setup_platform(hass, normal_config_entry, [Platform.SELECT]) + + entity_id = "select.test_seat_heater_front_left" + with ( + patch( + "homeassistant.components.tesla_fleet.VehicleSpecific.remote_seat_heater_request", + return_value=COMMAND_OK, + ) as remote_seat_heater_request, + patch( + "homeassistant.components.tesla_fleet.VehicleSpecific.auto_conditioning_start", + return_value=COMMAND_OK, + ) as auto_conditioning_start, + ): + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + {ATTR_ENTITY_ID: entity_id, ATTR_OPTION: LOW}, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.state == LOW + auto_conditioning_start.assert_called_once() + remote_seat_heater_request.assert_called_once() + + entity_id = "select.test_steering_wheel_heater" + with ( + patch( + "homeassistant.components.tesla_fleet.VehicleSpecific.remote_steering_wheel_heat_level_request", + return_value=COMMAND_OK, + ) as remote_steering_wheel_heat_level_request, + patch( + "homeassistant.components.tesla_fleet.VehicleSpecific.auto_conditioning_start", + return_value=COMMAND_OK, + ) as auto_conditioning_start, + ): + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + {ATTR_ENTITY_ID: entity_id, ATTR_OPTION: LOW}, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.state == LOW + auto_conditioning_start.assert_called_once() + remote_steering_wheel_heat_level_request.assert_called_once() + + entity_id = "select.energy_site_operation_mode" + with patch( + "homeassistant.components.tesla_fleet.EnergySpecific.operation", + return_value=COMMAND_OK, + ) as call: + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + { + ATTR_ENTITY_ID: entity_id, + ATTR_OPTION: EnergyOperationMode.AUTONOMOUS.value, + }, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.state == EnergyOperationMode.AUTONOMOUS.value + call.assert_called_once() + + entity_id = "select.energy_site_allow_export" + with patch( + "homeassistant.components.tesla_fleet.EnergySpecific.grid_import_export", + return_value=COMMAND_OK, + ) as call: + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + {ATTR_ENTITY_ID: entity_id, ATTR_OPTION: EnergyExportMode.BATTERY_OK.value}, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.state == EnergyExportMode.BATTERY_OK.value + call.assert_called_once()