mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 17:27:52 +00:00
Add number platform to Teslemetry (#117470)
* Add number platform * Cast numbers * rework numbers * Add number platform * Update docstrings * fix json * Remove speed limit * Fix snapshot * remove speed limit icon * Remove speed limit strings * rework min max * Fix coverage * Fix snapshot * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Type callable * Fix types --------- Co-authored-by: G Johansson <goran.johansson@shiftit.se>
This commit is contained in:
parent
74f288286a
commit
28a6f9eae7
@ -34,6 +34,7 @@ PLATFORMS: Final = [
|
||||
Platform.DEVICE_TRACKER,
|
||||
Platform.LOCK,
|
||||
Platform.MEDIA_PLAYER,
|
||||
Platform.NUMBER,
|
||||
Platform.SELECT,
|
||||
Platform.SENSOR,
|
||||
Platform.SWITCH,
|
||||
|
@ -60,6 +60,12 @@ class TeslemetryEntity(
|
||||
"""Return a specific value from coordinator data."""
|
||||
return self.coordinator.data.get(key, default)
|
||||
|
||||
def get_number(self, key: str, default: float) -> float:
|
||||
"""Return a specific number from coordinator data."""
|
||||
if isinstance(value := self.coordinator.data.get(key), (int, float)):
|
||||
return value
|
||||
return default
|
||||
|
||||
@property
|
||||
def is_none(self) -> bool:
|
||||
"""Return if the value is a literal None."""
|
||||
|
201
homeassistant/components/teslemetry/number.py
Normal file
201
homeassistant/components/teslemetry/number.py
Normal file
@ -0,0 +1,201 @@
|
||||
"""Number platform for Teslemetry integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
from itertools import chain
|
||||
from typing import Any
|
||||
|
||||
from tesla_fleet_api import EnergySpecific, VehicleSpecific
|
||||
from tesla_fleet_api.const import Scope
|
||||
|
||||
from homeassistant.components.number import (
|
||||
NumberDeviceClass,
|
||||
NumberEntity,
|
||||
NumberEntityDescription,
|
||||
NumberMode,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import PERCENTAGE, PRECISION_WHOLE, UnitOfElectricCurrent
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.icon import icon_for_battery_level
|
||||
|
||||
from .entity import TeslemetryEnergyInfoEntity, TeslemetryVehicleEntity
|
||||
from .models import TeslemetryEnergyData, TeslemetryVehicleData
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class TeslemetryNumberVehicleEntityDescription(NumberEntityDescription):
|
||||
"""Describes Teslemetry Number entity."""
|
||||
|
||||
func: Callable[[VehicleSpecific, float], Awaitable[Any]]
|
||||
native_min_value: float
|
||||
native_max_value: float
|
||||
min_key: str | None = None
|
||||
max_key: str
|
||||
scopes: list[Scope]
|
||||
|
||||
|
||||
VEHICLE_DESCRIPTIONS: tuple[TeslemetryNumberVehicleEntityDescription, ...] = (
|
||||
TeslemetryNumberVehicleEntityDescription(
|
||||
key="charge_state_charge_current_request",
|
||||
native_step=PRECISION_WHOLE,
|
||||
native_min_value=0,
|
||||
native_max_value=32,
|
||||
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
|
||||
device_class=NumberDeviceClass.CURRENT,
|
||||
mode=NumberMode.AUTO,
|
||||
max_key="charge_state_charge_current_request_max",
|
||||
func=lambda api, value: api.set_charging_amps(value),
|
||||
scopes=[Scope.VEHICLE_CHARGING_CMDS],
|
||||
),
|
||||
TeslemetryNumberVehicleEntityDescription(
|
||||
key="charge_state_charge_limit_soc",
|
||||
native_step=PRECISION_WHOLE,
|
||||
native_min_value=50,
|
||||
native_max_value=100,
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
device_class=NumberDeviceClass.BATTERY,
|
||||
mode=NumberMode.AUTO,
|
||||
min_key="charge_state_charge_limit_soc_min",
|
||||
max_key="charge_state_charge_limit_soc_max",
|
||||
func=lambda api, value: api.set_charge_limit(value),
|
||||
scopes=[Scope.VEHICLE_CHARGING_CMDS, Scope.VEHICLE_CMDS],
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class TeslemetryNumberBatteryEntityDescription(NumberEntityDescription):
|
||||
"""Describes Teslemetry Number entity."""
|
||||
|
||||
func: Callable[[EnergySpecific, float], Awaitable[Any]]
|
||||
requires: str | None = None
|
||||
|
||||
|
||||
ENERGY_INFO_DESCRIPTIONS: tuple[TeslemetryNumberBatteryEntityDescription, ...] = (
|
||||
TeslemetryNumberBatteryEntityDescription(
|
||||
key="backup_reserve_percent",
|
||||
func=lambda api, value: api.backup(int(value)),
|
||||
requires="components_battery",
|
||||
),
|
||||
TeslemetryNumberBatteryEntityDescription(
|
||||
key="off_grid_vehicle_charging_reserve",
|
||||
func=lambda api, value: api.off_grid_vehicle_charging_reserve(int(value)),
|
||||
requires="components_off_grid_vehicle_charging_reserve_supported",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the Teslemetry number platform from a config entry."""
|
||||
|
||||
async_add_entities(
|
||||
chain(
|
||||
( # Add vehicle entities
|
||||
TeslemetryVehicleNumberEntity(
|
||||
vehicle,
|
||||
description,
|
||||
entry.runtime_data.scopes,
|
||||
)
|
||||
for vehicle in entry.runtime_data.vehicles
|
||||
for description in VEHICLE_DESCRIPTIONS
|
||||
),
|
||||
( # Add energy site entities
|
||||
TeslemetryEnergyInfoNumberSensorEntity(
|
||||
energysite,
|
||||
description,
|
||||
entry.runtime_data.scopes,
|
||||
)
|
||||
for energysite in entry.runtime_data.energysites
|
||||
for description in ENERGY_INFO_DESCRIPTIONS
|
||||
if description.requires is None
|
||||
or energysite.info_coordinator.data.get(description.requires)
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class TeslemetryVehicleNumberEntity(TeslemetryVehicleEntity, NumberEntity):
|
||||
"""Vehicle number entity base class."""
|
||||
|
||||
entity_description: TeslemetryNumberVehicleEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslemetryVehicleData,
|
||||
description: TeslemetryNumberVehicleEntityDescription,
|
||||
scopes: list[Scope],
|
||||
) -> None:
|
||||
"""Initialize the number entity."""
|
||||
self.scoped = any(scope in scopes for scope in description.scopes)
|
||||
self.entity_description = description
|
||||
super().__init__(
|
||||
data,
|
||||
description.key,
|
||||
)
|
||||
|
||||
def _async_update_attrs(self) -> None:
|
||||
"""Update the attributes of the entity."""
|
||||
self._attr_native_value = self._value
|
||||
|
||||
if (min_key := self.entity_description.min_key) is not None:
|
||||
self._attr_native_min_value = self.get_number(
|
||||
min_key,
|
||||
self.entity_description.native_min_value,
|
||||
)
|
||||
else:
|
||||
self._attr_native_min_value = self.entity_description.native_min_value
|
||||
|
||||
self._attr_native_max_value = self.get_number(
|
||||
self.entity_description.max_key,
|
||||
self.entity_description.native_max_value,
|
||||
)
|
||||
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set new value."""
|
||||
value = int(value)
|
||||
self.raise_for_scope()
|
||||
await self.wake_up_if_asleep()
|
||||
await self.handle_command(self.entity_description.func(self.api, value))
|
||||
self._attr_native_value = value
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class TeslemetryEnergyInfoNumberSensorEntity(TeslemetryEnergyInfoEntity, NumberEntity):
|
||||
"""Energy info number entity base class."""
|
||||
|
||||
entity_description: TeslemetryNumberBatteryEntityDescription
|
||||
_attr_native_step = PRECISION_WHOLE
|
||||
_attr_native_min_value = 0
|
||||
_attr_native_max_value = 100
|
||||
_attr_device_class = NumberDeviceClass.BATTERY
|
||||
_attr_native_unit_of_measurement = PERCENTAGE
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslemetryEnergyData,
|
||||
description: TeslemetryNumberBatteryEntityDescription,
|
||||
scopes: list[Scope],
|
||||
) -> None:
|
||||
"""Initialize the number entity."""
|
||||
self.scoped = Scope.ENERGY_CMDS in scopes
|
||||
self.entity_description = description
|
||||
super().__init__(data, description.key)
|
||||
|
||||
def _async_update_attrs(self) -> None:
|
||||
"""Update the attributes of the entity."""
|
||||
self._attr_native_value = self._value
|
||||
self._attr_icon = icon_for_battery_level(self.native_value)
|
||||
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set new value."""
|
||||
value = int(value)
|
||||
self.raise_for_scope()
|
||||
await self.handle_command(self.entity_description.func(self.api, value))
|
||||
self._attr_native_value = value
|
||||
self.async_write_ha_state()
|
@ -244,6 +244,20 @@
|
||||
"name": "[%key:component::media_player::title%]"
|
||||
}
|
||||
},
|
||||
"number": {
|
||||
"backup_reserve_percent": {
|
||||
"name": "Backup reserve"
|
||||
},
|
||||
"charge_state_charge_current_request": {
|
||||
"name": "Charge current"
|
||||
},
|
||||
"charge_state_charge_limit_soc": {
|
||||
"name": "Charge limit"
|
||||
},
|
||||
"off_grid_vehicle_charging_reserve": {
|
||||
"name": "Off grid reserve"
|
||||
}
|
||||
},
|
||||
"cover": {
|
||||
"charge_state_charge_port_door_open": {
|
||||
"name": "Charge port door"
|
||||
|
@ -26,7 +26,7 @@
|
||||
"storm_mode_capable": true,
|
||||
"flex_energy_request_capable": false,
|
||||
"car_charging_data_supported": false,
|
||||
"off_grid_vehicle_charging_reserve_supported": false,
|
||||
"off_grid_vehicle_charging_reserve_supported": true,
|
||||
"vehicle_charging_performance_view_enabled": false,
|
||||
"vehicle_charging_solar_offset_view_enabled": false,
|
||||
"battery_solar_offset_view_enabled": true,
|
||||
|
@ -62,7 +62,7 @@
|
||||
'components_grid_services_enabled': False,
|
||||
'components_load_meter': True,
|
||||
'components_net_meter_mode': 'battery_ok',
|
||||
'components_off_grid_vehicle_charging_reserve_supported': False,
|
||||
'components_off_grid_vehicle_charging_reserve_supported': True,
|
||||
'components_set_islanding_mode_enabled': True,
|
||||
'components_show_grid_import_battery_source_cards': True,
|
||||
'components_solar': True,
|
||||
|
461
tests/components/teslemetry/snapshots/test_number.ambr
Normal file
461
tests/components/teslemetry/snapshots/test_number.ambr
Normal file
@ -0,0 +1,461 @@
|
||||
# serializer version: 1
|
||||
# name: test_number[number.energy_site_backup_reserve-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'max': 100,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'number',
|
||||
'entity_category': None,
|
||||
'entity_id': 'number.energy_site_backup_reserve',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <NumberDeviceClass.BATTERY: 'battery'>,
|
||||
'original_icon': 'mdi:battery-alert',
|
||||
'original_name': 'Backup reserve',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'backup_reserve_percent',
|
||||
'unique_id': '123456-backup_reserve_percent',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.energy_site_backup_reserve-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'battery',
|
||||
'friendly_name': 'Energy Site Backup reserve',
|
||||
'icon': 'mdi:battery-alert',
|
||||
'max': 100,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.energy_site_backup_reserve',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '0',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.energy_site_battery-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'max': 100,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'number',
|
||||
'entity_category': None,
|
||||
'entity_id': 'number.energy_site_battery',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <NumberDeviceClass.BATTERY: 'battery'>,
|
||||
'original_icon': 'mdi:battery-alert',
|
||||
'original_name': 'Battery',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'backup_reserve_percent',
|
||||
'unique_id': '123456-backup_reserve_percent',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.energy_site_battery-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'battery',
|
||||
'friendly_name': 'Energy Site Battery',
|
||||
'icon': 'mdi:battery-alert',
|
||||
'max': 100,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.energy_site_battery',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '0',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.energy_site_battery_2-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'max': 100,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'number',
|
||||
'entity_category': None,
|
||||
'entity_id': 'number.energy_site_battery_2',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <NumberDeviceClass.BATTERY: 'battery'>,
|
||||
'original_icon': 'mdi:battery-unknown',
|
||||
'original_name': 'Battery',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'off_grid_vehicle_charging_reserve',
|
||||
'unique_id': '123456-off_grid_vehicle_charging_reserve',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.energy_site_battery_2-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'battery',
|
||||
'friendly_name': 'Energy Site Battery',
|
||||
'icon': 'mdi:battery-unknown',
|
||||
'max': 100,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.energy_site_battery_2',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.energy_site_off_grid_reserve-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'max': 100,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'number',
|
||||
'entity_category': None,
|
||||
'entity_id': 'number.energy_site_off_grid_reserve',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <NumberDeviceClass.BATTERY: 'battery'>,
|
||||
'original_icon': 'mdi:battery-unknown',
|
||||
'original_name': 'Off grid reserve',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'off_grid_vehicle_charging_reserve',
|
||||
'unique_id': '123456-off_grid_vehicle_charging_reserve',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.energy_site_off_grid_reserve-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'battery',
|
||||
'friendly_name': 'Energy Site Off grid reserve',
|
||||
'icon': 'mdi:battery-unknown',
|
||||
'max': 100,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.energy_site_off_grid_reserve',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.test_battery-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'max': 100,
|
||||
'min': 50,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'number',
|
||||
'entity_category': None,
|
||||
'entity_id': 'number.test_battery',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <NumberDeviceClass.BATTERY: 'battery'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Battery',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'charge_state_charge_limit_soc',
|
||||
'unique_id': 'VINVINVIN-charge_state_charge_limit_soc',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.test_battery-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'battery',
|
||||
'friendly_name': 'Test Battery',
|
||||
'max': 100,
|
||||
'min': 50,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.test_battery',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '80',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.test_charge_current-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'max': 16,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'number',
|
||||
'entity_category': None,
|
||||
'entity_id': 'number.test_charge_current',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <NumberDeviceClass.CURRENT: 'current'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Charge current',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'charge_state_charge_current_request',
|
||||
'unique_id': 'VINVINVIN-charge_state_charge_current_request',
|
||||
'unit_of_measurement': <UnitOfElectricCurrent.AMPERE: 'A'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.test_charge_current-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'current',
|
||||
'friendly_name': 'Test Charge current',
|
||||
'max': 16,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
'unit_of_measurement': <UnitOfElectricCurrent.AMPERE: 'A'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.test_charge_current',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '16',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.test_charge_limit-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'max': 100,
|
||||
'min': 50,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'number',
|
||||
'entity_category': None,
|
||||
'entity_id': 'number.test_charge_limit',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <NumberDeviceClass.BATTERY: 'battery'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Charge limit',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'charge_state_charge_limit_soc',
|
||||
'unique_id': 'VINVINVIN-charge_state_charge_limit_soc',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.test_charge_limit-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'battery',
|
||||
'friendly_name': 'Test Charge limit',
|
||||
'max': 100,
|
||||
'min': 50,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.test_charge_limit',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '80',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.test_current-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'max': 16,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'number',
|
||||
'entity_category': None,
|
||||
'entity_id': 'number.test_current',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <NumberDeviceClass.CURRENT: 'current'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Current',
|
||||
'platform': 'teslemetry',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'charge_state_charge_current_request',
|
||||
'unique_id': 'VINVINVIN-charge_state_charge_current_request',
|
||||
'unit_of_measurement': <UnitOfElectricCurrent.AMPERE: 'A'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_number[number.test_current-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'current',
|
||||
'friendly_name': 'Test Current',
|
||||
'max': 16,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.AUTO: 'auto'>,
|
||||
'step': 1,
|
||||
'unit_of_measurement': <UnitOfElectricCurrent.AMPERE: 'A'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.test_current',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '16',
|
||||
})
|
||||
# ---
|
113
tests/components/teslemetry/test_number.py
Normal file
113
tests/components/teslemetry/test_number.py
Normal file
@ -0,0 +1,113 @@
|
||||
"""Test the Teslemetry number platform."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
from tesla_fleet_api.exceptions import VehicleOffline
|
||||
|
||||
from homeassistant.components.number import (
|
||||
ATTR_VALUE,
|
||||
DOMAIN as NUMBER_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
)
|
||||
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_number(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Tests that the number entities are correct."""
|
||||
|
||||
entry = await setup_platform(hass, [Platform.NUMBER])
|
||||
assert_entities(hass, entry.entry_id, entity_registry, snapshot)
|
||||
|
||||
|
||||
async def test_number_offline(
|
||||
hass: HomeAssistant,
|
||||
mock_vehicle_data,
|
||||
) -> None:
|
||||
"""Tests that the number entities are correct when offline."""
|
||||
|
||||
mock_vehicle_data.side_effect = VehicleOffline
|
||||
await setup_platform(hass, [Platform.NUMBER])
|
||||
state = hass.states.get("number.test_charge_current")
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_number_services(hass: HomeAssistant, mock_vehicle_data) -> None:
|
||||
"""Tests that the number services work."""
|
||||
mock_vehicle_data.return_value = VEHICLE_DATA_ALT
|
||||
await setup_platform(hass, [Platform.NUMBER])
|
||||
|
||||
entity_id = "number.test_charge_current"
|
||||
with patch(
|
||||
"homeassistant.components.teslemetry.VehicleSpecific.set_charging_amps",
|
||||
return_value=COMMAND_OK,
|
||||
) as call:
|
||||
await hass.services.async_call(
|
||||
NUMBER_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 16},
|
||||
blocking=True,
|
||||
)
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == "16"
|
||||
call.assert_called_once()
|
||||
|
||||
entity_id = "number.test_charge_limit"
|
||||
with patch(
|
||||
"homeassistant.components.teslemetry.VehicleSpecific.set_charge_limit",
|
||||
return_value=COMMAND_OK,
|
||||
) as call:
|
||||
await hass.services.async_call(
|
||||
NUMBER_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 60},
|
||||
blocking=True,
|
||||
)
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == "60"
|
||||
call.assert_called_once()
|
||||
|
||||
entity_id = "number.energy_site_backup_reserve"
|
||||
with patch(
|
||||
"homeassistant.components.teslemetry.EnergySpecific.backup",
|
||||
return_value=COMMAND_OK,
|
||||
) as call:
|
||||
await hass.services.async_call(
|
||||
NUMBER_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{
|
||||
ATTR_ENTITY_ID: entity_id,
|
||||
ATTR_VALUE: 80,
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == "80"
|
||||
call.assert_called_once()
|
||||
|
||||
entity_id = "number.energy_site_off_grid_reserve"
|
||||
with patch(
|
||||
"homeassistant.components.teslemetry.EnergySpecific.off_grid_vehicle_charging_reserve",
|
||||
return_value=COMMAND_OK,
|
||||
) as call:
|
||||
await hass.services.async_call(
|
||||
NUMBER_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 88},
|
||||
blocking=True,
|
||||
)
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == "88"
|
||||
call.assert_called_once()
|
Loading…
x
Reference in New Issue
Block a user