1 """Number platform for Tesla Fleet integration."""
3 from __future__
import annotations
5 from collections.abc
import Awaitable, Callable
6 from dataclasses
import dataclass
7 from itertools
import chain
10 from tesla_fleet_api
import EnergySpecific, VehicleSpecific
11 from tesla_fleet_api.const
import Scope
16 NumberEntityDescription,
24 from .
import TeslaFleetConfigEntry
25 from .entity
import TeslaFleetEnergyInfoEntity, TeslaFleetVehicleEntity
26 from .helpers
import handle_command, handle_vehicle_command
27 from .models
import TeslaFleetEnergyData, TeslaFleetVehicleData
32 @dataclass(frozen=True, kw_only=True)
34 """Describes TeslaFleet Number entity."""
36 func: Callable[[VehicleSpecific, float], Awaitable[Any]]
37 native_min_value: float
38 native_max_value: float
39 min_key: str |
None =
None
44 VEHICLE_DESCRIPTIONS: tuple[TeslaFleetNumberVehicleEntityDescription, ...] = (
46 key=
"charge_state_charge_current_request",
47 native_step=PRECISION_WHOLE,
50 native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
51 device_class=NumberDeviceClass.CURRENT,
53 max_key=
"charge_state_charge_current_request_max",
54 func=
lambda api, value: api.set_charging_amps(value),
55 scopes=[Scope.VEHICLE_CHARGING_CMDS],
58 key=
"charge_state_charge_limit_soc",
59 native_step=PRECISION_WHOLE,
62 native_unit_of_measurement=PERCENTAGE,
63 device_class=NumberDeviceClass.BATTERY,
65 min_key=
"charge_state_charge_limit_soc_min",
66 max_key=
"charge_state_charge_limit_soc_max",
67 func=
lambda api, value: api.set_charge_limit(value),
68 scopes=[Scope.VEHICLE_CHARGING_CMDS, Scope.VEHICLE_CMDS],
73 @dataclass(frozen=True, kw_only=True)
75 """Describes TeslaFleet Number entity."""
77 func: Callable[[EnergySpecific, float], Awaitable[Any]]
78 requires: str |
None =
None
81 ENERGY_INFO_DESCRIPTIONS: tuple[TeslaFleetNumberBatteryEntityDescription, ...] = (
83 key=
"backup_reserve_percent",
84 func=
lambda api, value: api.backup(
int(value)),
85 requires=
"components_battery",
88 key=
"off_grid_vehicle_charging_reserve_percent",
89 func=
lambda api, value: api.off_grid_vehicle_charging_reserve(
int(value)),
90 requires=
"components_off_grid_vehicle_charging_reserve_supported",
97 entry: TeslaFleetConfigEntry,
98 async_add_entities: AddEntitiesCallback,
100 """Set up the TeslaFleet number platform from a config entry."""
108 entry.runtime_data.scopes,
110 for vehicle
in entry.runtime_data.vehicles
111 for description
in VEHICLE_DESCRIPTIONS
117 entry.runtime_data.scopes,
119 for energysite
in entry.runtime_data.energysites
120 for description
in ENERGY_INFO_DESCRIPTIONS
121 if description.requires
is None
122 or energysite.info_coordinator.data.get(description.requires)
129 """Vehicle number entity base class."""
131 entity_description: TeslaFleetNumberVehicleEntityDescription
135 data: TeslaFleetVehicleData,
136 description: TeslaFleetNumberVehicleEntityDescription,
139 """Initialize the number entity."""
140 self.
scopedscoped = any(scope
in scopes
for scope
in description.scopes)
148 """Update the attributes of the entity."""
175 """Energy info number entity base class."""
177 entity_description: TeslaFleetNumberBatteryEntityDescription
178 _attr_native_step = PRECISION_WHOLE
179 _attr_native_min_value = 0
180 _attr_native_max_value = 100
181 _attr_device_class = NumberDeviceClass.BATTERY
182 _attr_native_unit_of_measurement = PERCENTAGE
186 data: TeslaFleetEnergyData,
187 description: TeslaFleetNumberBatteryEntityDescription,
190 """Initialize the number entity."""
191 self.
scopedscoped = Scope.ENERGY_CMDS
in scopes
193 super().
__init__(data, description.key)
196 """Update the attributes of the entity."""
float|None native_value(self)
None raise_for_read_only(self, Scope scope)
float get_number(self, str key, float default)
None wake_up_if_asleep(self)
None __init__(self, TeslaFleetEnergyData data, TeslaFleetNumberBatteryEntityDescription description, list[Scope] scopes)
None _async_update_attrs(self)
None async_set_native_value(self, float value)
None __init__(self, TeslaFleetVehicleData data, TeslaFleetNumberVehicleEntityDescription description, list[Scope] scopes)
None _async_update_attrs(self)
None async_set_native_value(self, float value)
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)
str icon_for_battery_level(int|None battery_level=None, bool charging=False)