mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 13:47:35 +00:00
Add select platform to Teslemetry (#117422)
* Add select platform * Add tests * Tests WIP * Add tests * Update homeassistant/components/teslemetry/select.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/teslemetry/select.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * use references * Fix typo * Update homeassistant/components/teslemetry/select.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update homeassistant/components/teslemetry/select.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Make less confusing for @joostlek * Update homeassistant/components/teslemetry/select.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/teslemetry/select.py --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: G Johansson <goran.johansson@shiftit.se>
This commit is contained in:
parent
c4c96be880
commit
4125b6e15f
@ -28,6 +28,7 @@ from .models import TeslemetryData, TeslemetryEnergyData, TeslemetryVehicleData
|
||||
|
||||
PLATFORMS: Final = [
|
||||
Platform.CLIMATE,
|
||||
Platform.SELECT,
|
||||
Platform.SENSOR,
|
||||
]
|
||||
|
||||
|
@ -88,6 +88,11 @@ class TeslemetryEntity(
|
||||
def _async_update_attrs(self) -> None:
|
||||
"""Update the attributes of the entity."""
|
||||
|
||||
def raise_for_scope(self):
|
||||
"""Raise an error if a scope is not available."""
|
||||
if not self.scoped:
|
||||
raise ServiceValidationError("Missing required scope")
|
||||
|
||||
|
||||
class TeslemetryVehicleEntity(TeslemetryEntity):
|
||||
"""Parent class for Teslemetry Vehicle entities."""
|
||||
@ -153,11 +158,6 @@ class TeslemetryVehicleEntity(TeslemetryEntity):
|
||||
# Response with result of true
|
||||
return result
|
||||
|
||||
def raise_for_scope(self):
|
||||
"""Raise an error if a scope is not available."""
|
||||
if not self.scoped:
|
||||
raise ServiceValidationError("Missing required scope")
|
||||
|
||||
|
||||
class TeslemetryEnergyLiveEntity(TeslemetryEntity):
|
||||
"""Parent class for Teslemetry Energy Site Live entities."""
|
||||
|
@ -14,6 +14,66 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"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"
|
||||
|
259
homeassistant/components/teslemetry/select.py
Normal file
259
homeassistant/components/teslemetry/select.py
Normal file
@ -0,0 +1,259 @@
|
||||
"""Select platform for Teslemetry 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.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .entity import TeslemetryEnergyInfoEntity, TeslemetryVehicleEntity
|
||||
from .models import TeslemetryEnergyData, TeslemetryVehicleData
|
||||
|
||||
OFF = "off"
|
||||
LOW = "low"
|
||||
MEDIUM = "medium"
|
||||
HIGH = "high"
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class SeatHeaterDescription(SelectEntityDescription):
|
||||
"""Seat Heater entity description."""
|
||||
|
||||
position: Seat
|
||||
available_fn: Callable[[TeslemetrySeatHeaterSelectEntity], 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: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the Teslemetry select platform from a config entry."""
|
||||
|
||||
async_add_entities(
|
||||
chain(
|
||||
(
|
||||
TeslemetrySeatHeaterSelectEntity(
|
||||
vehicle, description, entry.runtime_data.scopes
|
||||
)
|
||||
for description in SEAT_HEATER_DESCRIPTIONS
|
||||
for vehicle in entry.runtime_data.vehicles
|
||||
),
|
||||
(
|
||||
TeslemetryWheelHeaterSelectEntity(vehicle, entry.runtime_data.scopes)
|
||||
for vehicle in entry.runtime_data.vehicles
|
||||
),
|
||||
(
|
||||
TeslemetryOperationSelectEntity(energysite, entry.runtime_data.scopes)
|
||||
for energysite in entry.runtime_data.energysites
|
||||
if energysite.info_coordinator.data.get("components_battery")
|
||||
),
|
||||
(
|
||||
TeslemetryExportRuleSelectEntity(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 TeslemetrySeatHeaterSelectEntity(TeslemetryVehicleEntity, SelectEntity):
|
||||
"""Select entity for vehicle seat heater."""
|
||||
|
||||
entity_description: SeatHeaterDescription
|
||||
|
||||
_attr_options = [
|
||||
OFF,
|
||||
LOW,
|
||||
MEDIUM,
|
||||
HIGH,
|
||||
]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslemetryVehicleData,
|
||||
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_scope()
|
||||
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 self.handle_command(self.api.auto_conditioning_start())
|
||||
await self.handle_command(
|
||||
self.api.remote_seat_heater_request(self.entity_description.position, level)
|
||||
)
|
||||
self._attr_current_option = option
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class TeslemetryWheelHeaterSelectEntity(TeslemetryVehicleEntity, SelectEntity):
|
||||
"""Select entity for vehicle steering wheel heater."""
|
||||
|
||||
_attr_options = [
|
||||
OFF,
|
||||
LOW,
|
||||
HIGH,
|
||||
]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslemetryVehicleData,
|
||||
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_scope()
|
||||
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 self.handle_command(self.api.auto_conditioning_start())
|
||||
await self.handle_command(
|
||||
self.api.remote_steering_wheel_heat_level_request(level)
|
||||
)
|
||||
self._attr_current_option = option
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class TeslemetryOperationSelectEntity(TeslemetryEnergyInfoEntity, SelectEntity):
|
||||
"""Select entity for operation mode select entities."""
|
||||
|
||||
_attr_options: list[str] = [
|
||||
EnergyOperationMode.AUTONOMOUS,
|
||||
EnergyOperationMode.BACKUP,
|
||||
EnergyOperationMode.SELF_CONSUMPTION,
|
||||
]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslemetryEnergyData,
|
||||
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_scope()
|
||||
await self.handle_command(self.api.operation(option))
|
||||
self._attr_current_option = option
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class TeslemetryExportRuleSelectEntity(TeslemetryEnergyInfoEntity, SelectEntity):
|
||||
"""Select entity for export rules select entities."""
|
||||
|
||||
_attr_options: list[str] = [
|
||||
EnergyExportMode.NEVER,
|
||||
EnergyExportMode.BATTERY_OK,
|
||||
EnergyExportMode.PV_ONLY,
|
||||
]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslemetryEnergyData,
|
||||
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_scope()
|
||||
await self.handle_command(
|
||||
self.api.grid_import_export(customer_preferred_export_rule=option)
|
||||
)
|
||||
self._attr_current_option = option
|
||||
self.async_write_ha_state()
|
@ -31,6 +31,95 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"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::teslemetry::entity::select::climate_state_seat_heater_left::state::high%]",
|
||||
"low": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::low%]",
|
||||
"medium": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::medium%]",
|
||||
"off": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::off%]"
|
||||
}
|
||||
},
|
||||
"climate_state_seat_heater_rear_left": {
|
||||
"name": "Seat heater rear left",
|
||||
"state": {
|
||||
"high": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::high%]",
|
||||
"low": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::low%]",
|
||||
"medium": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::medium%]",
|
||||
"off": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::off%]"
|
||||
}
|
||||
},
|
||||
"climate_state_seat_heater_rear_right": {
|
||||
"name": "Seat heater rear right",
|
||||
"state": {
|
||||
"high": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::high%]",
|
||||
"low": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::low%]",
|
||||
"medium": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::medium%]",
|
||||
"off": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::off%]"
|
||||
}
|
||||
},
|
||||
"climate_state_seat_heater_right": {
|
||||
"name": "Seat heater front right",
|
||||
"state": {
|
||||
"high": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::high%]",
|
||||
"low": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::low%]",
|
||||
"medium": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::medium%]",
|
||||
"off": "[%key:component::teslemetry::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::teslemetry::entity::select::climate_state_seat_heater_left::state::high%]",
|
||||
"low": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::low%]",
|
||||
"medium": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::medium%]",
|
||||
"off": "[%key:component::teslemetry::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::teslemetry::entity::select::climate_state_seat_heater_left::state::high%]",
|
||||
"low": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::low%]",
|
||||
"medium": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::medium%]",
|
||||
"off": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::off%]"
|
||||
}
|
||||
},
|
||||
"climate_state_steering_wheel_heat_level": {
|
||||
"name": "Steering wheel heater",
|
||||
"state": {
|
||||
"high": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::high%]",
|
||||
"low": "[%key:component::teslemetry::entity::select::climate_state_seat_heater_left::state::low%]",
|
||||
"off": "[%key:component::teslemetry::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"
|
||||
|
585
tests/components/teslemetry/snapshots/test_select.ambr
Normal file
585
tests/components/teslemetry/snapshots/test_select.ambr
Normal file
@ -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([
|
||||
<EnergyExportMode.NEVER: 'never'>,
|
||||
<EnergyExportMode.BATTERY_OK: 'battery_ok'>,
|
||||
<EnergyExportMode.PV_ONLY: 'pv_only'>,
|
||||
]),
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Allow export',
|
||||
'platform': 'teslemetry',
|
||||
'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([
|
||||
<EnergyExportMode.NEVER: 'never'>,
|
||||
<EnergyExportMode.BATTERY_OK: 'battery_ok'>,
|
||||
<EnergyExportMode.PV_ONLY: 'pv_only'>,
|
||||
]),
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'select.energy_site_allow_export',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'pv_only',
|
||||
})
|
||||
# ---
|
||||
# name: test_select[select.energy_site_operation_mode-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'options': list([
|
||||
<EnergyOperationMode.AUTONOMOUS: 'autonomous'>,
|
||||
<EnergyOperationMode.BACKUP: 'backup'>,
|
||||
<EnergyOperationMode.SELF_CONSUMPTION: 'self_consumption'>,
|
||||
]),
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Operation mode',
|
||||
'platform': 'teslemetry',
|
||||
'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([
|
||||
<EnergyOperationMode.AUTONOMOUS: 'autonomous'>,
|
||||
<EnergyOperationMode.BACKUP: 'backup'>,
|
||||
<EnergyOperationMode.SELF_CONSUMPTION: 'self_consumption'>,
|
||||
]),
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'select.energy_site_operation_mode',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Seat heater front left',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'climate_state_seat_heater_left',
|
||||
'unique_id': 'VINVINVIN-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': <ANY>,
|
||||
'entity_id': 'select.test_seat_heater_front_left',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Seat heater front right',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'climate_state_seat_heater_right',
|
||||
'unique_id': 'VINVINVIN-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': <ANY>,
|
||||
'entity_id': 'select.test_seat_heater_front_right',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Seat heater rear center',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'climate_state_seat_heater_rear_center',
|
||||
'unique_id': 'VINVINVIN-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': <ANY>,
|
||||
'entity_id': 'select.test_seat_heater_rear_center',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Seat heater rear left',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'climate_state_seat_heater_rear_left',
|
||||
'unique_id': 'VINVINVIN-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': <ANY>,
|
||||
'entity_id': 'select.test_seat_heater_rear_left',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Seat heater rear right',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'climate_state_seat_heater_rear_right',
|
||||
'unique_id': 'VINVINVIN-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': <ANY>,
|
||||
'entity_id': 'select.test_seat_heater_rear_right',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Seat heater third row left',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'climate_state_seat_heater_third_row_left',
|
||||
'unique_id': 'VINVINVIN-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': <ANY>,
|
||||
'entity_id': 'select.test_seat_heater_third_row_left',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Seat heater third row right',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'climate_state_seat_heater_third_row_right',
|
||||
'unique_id': 'VINVINVIN-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': <ANY>,
|
||||
'entity_id': 'select.test_seat_heater_third_row_right',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Steering wheel heater',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'climate_state_steering_wheel_heat_level',
|
||||
'unique_id': 'VINVINVIN-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': <ANY>,
|
||||
'entity_id': 'select.test_steering_wheel_heater',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
114
tests/components/teslemetry/test_select.py
Normal file
114
tests/components/teslemetry/test_select.py
Normal file
@ -0,0 +1,114 @@
|
||||
"""Test the Teslemetry select platform."""
|
||||
|
||||
from unittest.mock import 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.teslemetry.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
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_select(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Tests that the select entities are correct."""
|
||||
|
||||
entry = await setup_platform(hass, [Platform.SELECT])
|
||||
assert_entities(hass, entry.entry_id, entity_registry, snapshot)
|
||||
|
||||
|
||||
async def test_select_offline(
|
||||
hass: HomeAssistant,
|
||||
mock_vehicle_data,
|
||||
) -> None:
|
||||
"""Tests that the select entities are correct when offline."""
|
||||
|
||||
mock_vehicle_data.side_effect = VehicleOffline
|
||||
await setup_platform(hass, [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) -> None:
|
||||
"""Tests that the select services work."""
|
||||
mock_vehicle_data.return_value = VEHICLE_DATA_ALT
|
||||
await setup_platform(hass, [Platform.SELECT])
|
||||
|
||||
entity_id = "select.test_seat_heater_front_left"
|
||||
with patch(
|
||||
"homeassistant.components.teslemetry.VehicleSpecific.remote_seat_heater_request",
|
||||
return_value=COMMAND_OK,
|
||||
) as call:
|
||||
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
|
||||
call.assert_called_once()
|
||||
|
||||
entity_id = "select.test_steering_wheel_heater"
|
||||
with patch(
|
||||
"homeassistant.components.teslemetry.VehicleSpecific.remote_steering_wheel_heat_level_request",
|
||||
return_value=COMMAND_OK,
|
||||
) as call:
|
||||
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
|
||||
call.assert_called_once()
|
||||
|
||||
entity_id = "select.energy_site_operation_mode"
|
||||
with patch(
|
||||
"homeassistant.components.teslemetry.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.teslemetry.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()
|
Loading…
x
Reference in New Issue
Block a user