1 """Switch platform for Tesla Fleet integration."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from itertools
import chain
10 from tesla_fleet_api.const
import Scope, Seat
15 SwitchEntityDescription,
20 from .
import TeslaFleetConfigEntry
21 from .entity
import TeslaFleetEnergyInfoEntity, TeslaFleetVehicleEntity
22 from .helpers
import handle_command, handle_vehicle_command
23 from .models
import TeslaFleetEnergyData, TeslaFleetVehicleData
28 @dataclass(frozen=True, kw_only=True)
30 """Describes TeslaFleet Switch entity."""
37 VEHICLE_DESCRIPTIONS: tuple[TeslaFleetSwitchEntityDescription, ...] = (
39 key=
"vehicle_state_sentry_mode",
40 on_func=
lambda api: api.set_sentry_mode(on=
True),
41 off_func=
lambda api: api.set_sentry_mode(on=
False),
42 scopes=[Scope.VEHICLE_CMDS],
45 key=
"climate_state_auto_seat_climate_left",
46 on_func=
lambda api: api.remote_auto_seat_climate_request(Seat.FRONT_LEFT,
True),
47 off_func=
lambda api: api.remote_auto_seat_climate_request(
48 Seat.FRONT_LEFT,
False
50 scopes=[Scope.VEHICLE_CMDS],
53 key=
"climate_state_auto_seat_climate_right",
54 on_func=
lambda api: api.remote_auto_seat_climate_request(
55 Seat.FRONT_RIGHT,
True
57 off_func=
lambda api: api.remote_auto_seat_climate_request(
58 Seat.FRONT_RIGHT,
False
60 scopes=[Scope.VEHICLE_CMDS],
63 key=
"climate_state_auto_steering_wheel_heat",
64 on_func=
lambda api: api.remote_auto_steering_wheel_heat_climate_request(
67 off_func=
lambda api: api.remote_auto_steering_wheel_heat_climate_request(
70 scopes=[Scope.VEHICLE_CMDS],
73 key=
"climate_state_defrost_mode",
74 on_func=
lambda api: api.set_preconditioning_max(on=
True, manual_override=
False),
75 off_func=
lambda api: api.set_preconditioning_max(
76 on=
False, manual_override=
False
78 scopes=[Scope.VEHICLE_CMDS],
83 key=
"charge_state_user_charge_enable_request",
84 on_func=
lambda api: api.charge_start(),
85 off_func=
lambda api: api.charge_stop(),
86 scopes=[Scope.VEHICLE_CHARGING_CMDS, Scope.VEHICLE_CMDS],
92 entry: TeslaFleetConfigEntry,
93 async_add_entities: AddEntitiesCallback,
95 """Set up the TeslaFleet Switch platform from a config entry."""
101 vehicle, description, entry.runtime_data.scopes
103 for vehicle
in entry.runtime_data.vehicles
104 for description
in VEHICLE_DESCRIPTIONS
108 vehicle, VEHICLE_CHARGE_DESCRIPTION, entry.runtime_data.scopes
110 for vehicle
in entry.runtime_data.vehicles
115 entry.runtime_data.scopes,
117 for energysite
in entry.runtime_data.energysites
118 if energysite.info_coordinator.data.get(
"components_battery")
119 and energysite.info_coordinator.data.get(
"components_solar")
123 for energysite
in entry.runtime_data.energysites
124 if energysite.info_coordinator.data.get(
"components_storm_mode_capable")
131 """Base class for all TeslaFleet switch entities."""
133 _attr_device_class = SwitchDeviceClass.SWITCH
134 entity_description: TeslaFleetSwitchEntityDescription
138 """Base class for TeslaFleet vehicle switch entities."""
142 data: TeslaFleetVehicleData,
143 description: TeslaFleetSwitchEntityDescription,
146 """Initialize the Switch."""
147 super().
__init__(data, description.key)
149 self.
scopedscoped = any(scope
in scopes
for scope
in description.scopes)
152 """Update the attributes of the sensor."""
159 """Turn on the Switch."""
167 """Turn off the Switch."""
176 """Entity class for TeslaFleet charge switch."""
179 """Update the attributes of the entity."""
187 TeslaFleetEnergyInfoEntity, TeslaFleetSwitchEntity
189 """Entity class for Charge From Grid switch."""
193 data: TeslaFleetEnergyData,
196 """Initialize the Switch."""
197 self.
scopedscoped = Scope.ENERGY_CMDS
in scopes
199 data,
"components_disallow_charge_from_grid_with_solar_installed"
203 """Update the attributes of the entity."""
209 """Turn on the Switch."""
213 disallow_charge_from_grid_with_solar_installed=
False
220 """Turn off the Switch."""
224 disallow_charge_from_grid_with_solar_installed=
True
232 TeslaFleetEnergyInfoEntity, TeslaFleetSwitchEntity
234 """Entity class for Storm Mode switch."""
238 data: TeslaFleetEnergyData,
241 """Initialize the Switch."""
242 super().
__init__(data,
"user_settings_storm_mode_enabled")
243 self.
scopedscoped = Scope.ENERGY_CMDS
in scopes
246 """Update the attributes of the sensor."""
251 """Turn on the Switch."""
258 """Turn off the Switch."""
Any|None get(self, str key, Any|None default=None)
None raise_for_read_only(self, Scope scope)
None wake_up_if_asleep(self)
None async_turn_on(self, **Any kwargs)
None _async_update_attrs(self)
None __init__(self, TeslaFleetEnergyData data, list[Scope] scopes)
None async_turn_off(self, **Any kwargs)
None _async_update_attrs(self)
None _async_update_attrs(self)
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
None __init__(self, TeslaFleetEnergyData data, list[Scope] scopes)
None async_turn_off(self, **Any kwargs)
None _async_update_attrs(self)
None __init__(self, TeslaFleetVehicleData data, TeslaFleetSwitchEntityDescription description, list[Scope] scopes)
None async_turn_on(self, **Any kwargs)
None async_write_ha_state(self)
bool handle_vehicle_command(Awaitable command)
dict[str, Any] handle_command(Awaitable command)
None async_setup_entry(HomeAssistant hass, TeslaFleetConfigEntry entry, AddEntitiesCallback async_add_entities)