Moves V2C InstallationVoltage from Sensor to Number (#169771)

Co-authored-by: Samuel Cabrero <scabrero@suse.com>
Co-authored-by: Samuel Cabrero <samuel@orica.es>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Samuel Cabrero <samuel@orica.es>
This commit is contained in:
Diogo Gomes
2026-06-08 10:47:12 +01:00
committed by GitHub
parent 4f4aeff2b4
commit c27e43c570
10 changed files with 464 additions and 67 deletions
+5
View File
@@ -8,6 +8,11 @@
"default": "mdi:led-on"
}
},
"number": {
"voltage_installation": {
"default": "mdi:sine-wave"
}
},
"sensor": {
"battery_power": {
"default": "mdi:home-battery"
+21 -3
View File
@@ -11,7 +11,11 @@ from homeassistant.components.number import (
NumberEntity,
NumberEntityDescription,
)
from homeassistant.const import EntityCategory, UnitOfElectricCurrent
from homeassistant.const import (
EntityCategory,
UnitOfElectricCurrent,
UnitOfElectricPotential,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
@@ -20,13 +24,15 @@ from .entity import V2CBaseEntity
MIN_INTENSITY = 6
MAX_INTENSITY = 32
MIN_VOLTAGE = 1
MAX_VOLTAGE = 500
@dataclass(frozen=True, kw_only=True)
class V2CSettingsNumberEntityDescription(NumberEntityDescription):
"""Describes V2C EVSE number entity."""
value_fn: Callable[[TrydanData], int]
value_fn: Callable[[TrydanData], int | None]
update_fn: Callable[[Trydan, int], Coroutine[Any, Any, None]]
@@ -63,6 +69,18 @@ TRYDAN_NUMBER_SETTINGS = (
value_fn=lambda evse_data: evse_data.max_intensity,
update_fn=lambda evse, value: evse.max_intensity(value),
),
V2CSettingsNumberEntityDescription(
key="voltage_installation",
translation_key="voltage_installation",
device_class=NumberDeviceClass.VOLTAGE,
entity_category=EntityCategory.CONFIG,
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
native_min_value=MIN_VOLTAGE,
native_max_value=MAX_VOLTAGE,
value_fn=lambda evse_data: evse_data.voltage_installation,
update_fn=lambda evse, value: evse.voltage_installation(value),
entity_registry_enabled_default=False,
),
)
@@ -96,7 +114,7 @@ class V2CSettingsNumberEntity(V2CBaseEntity, NumberEntity):
self._attr_unique_id = f"{entry_id}_{description.key}"
@property
def native_value(self) -> float:
def native_value(self) -> float | None:
"""Return the state of the setting entity."""
return self.entity_description.value_fn(self.data)
+21 -4
View File
@@ -15,17 +15,20 @@ from homeassistant.components.sensor import (
)
from homeassistant.const import (
EntityCategory,
Platform,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfPower,
UnitOfTime,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.typing import StateType
from .coordinator import V2CConfigEntry, V2CUpdateCoordinator
from .entity import V2CBaseEntity
from .util import deprecate_entity
_LOGGER = logging.getLogger(__name__)
@@ -144,11 +147,25 @@ async def async_setup_entry(
) -> None:
"""Set up V2C sensor platform."""
coordinator = config_entry.runtime_data
entity_registry = er.async_get(hass)
async_add_entities(
V2CSensorBaseEntity(coordinator, description, config_entry.entry_id)
for description in TRYDAN_SENSORS
)
entities: list[V2CSensorBaseEntity] = []
for description in TRYDAN_SENSORS:
if description.key == "voltage_installation" and not deprecate_entity(
hass=hass,
entity_registry=entity_registry,
platform_domain=Platform.SENSOR,
entity_unique_id=f"{config_entry.entry_id}_{description.key}",
issue_id=f"deprecated_sensor_{config_entry.entry_id}_{description.key}",
issue_string="deprecated_sensor",
replacement_entity_unique_id=f"{config_entry.entry_id}_{description.key}",
replacement_entity_id=f"number.evse_{description.key}",
):
continue
entities.append(
V2CSensorBaseEntity(coordinator, description, config_entry.entry_id)
)
async_add_entities(entities)
class V2CSensorBaseEntity(V2CBaseEntity, SensorEntity):
+13
View File
@@ -47,6 +47,9 @@
},
"min_intensity": {
"name": "Min intensity"
},
"voltage_installation": {
"name": "Installation voltage"
}
},
"sensor": {
@@ -138,5 +141,15 @@
"name": "Charge point timer"
}
}
},
"issues": {
"deprecated_sensor": {
"description": "The sensor {entity_name} (`{entity_id}`) is deprecated because it has been replaced with `{replacement_entity_id}`.\n\nUpdate your dashboards, templates, automations and scripts to use the replacement entity, then disable the deprecated sensor to have it removed after the next restart.",
"title": "Deprecated sensor detected"
},
"deprecated_sensor_scripts": {
"description": "The sensor {entity_name} (`{entity_id}`) is deprecated because it has been replaced with `{replacement_entity_id}`.\n\nThe sensor was used in the following automations or scripts:\n{items}\n\nUpdate the above automations or scripts to use the replacement entity, then disable the deprecated sensor to have it removed after the next restart.",
"title": "[%key:component::v2c::issues::deprecated_sensor::title%]"
}
}
}
+99
View File
@@ -0,0 +1,99 @@
"""Utility helpers for the v2c integration."""
from homeassistant.components.automation import automations_with_entity
from homeassistant.components.script import scripts_with_entity
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.issue_registry import (
IssueSeverity,
async_create_issue,
async_delete_issue,
)
from .const import DOMAIN
def deprecate_entity(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
platform_domain: str,
entity_unique_id: str,
issue_id: str,
issue_string: str,
replacement_entity_unique_id: str,
replacement_entity_id: str,
version: str = "2026.12.0",
) -> bool:
"""Create an issue for deprecated entities."""
if entity_id := entity_registry.async_get_entity_id(
platform_domain, DOMAIN, entity_unique_id
):
entity_entry = entity_registry.async_get(entity_id)
if not entity_entry:
async_delete_issue(hass, DOMAIN, issue_id)
return False
items = get_automations_and_scripts_using_entity(hass, entity_id)
if entity_entry.disabled and not items:
entity_registry.async_remove(entity_id)
async_delete_issue(hass, DOMAIN, issue_id)
return False
translation_key = issue_string
placeholders = {
"entity_id": entity_id,
"entity_name": entity_entry.name or entity_entry.original_name or "Unknown",
"replacement_entity_id": (
entity_registry.async_get_entity_id(
Platform.NUMBER, DOMAIN, replacement_entity_unique_id
)
or replacement_entity_id
),
}
if items:
translation_key = f"{translation_key}_scripts"
placeholders["items"] = "\n".join(items)
async_create_issue(
hass,
DOMAIN,
issue_id,
breaks_in_ha_version=version,
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key=translation_key,
translation_placeholders=placeholders,
)
return True
async_delete_issue(hass, DOMAIN, issue_id)
return False
def get_automations_and_scripts_using_entity(
hass: HomeAssistant,
entity_id: str,
) -> list[str]:
"""Get automations and scripts using an entity."""
automations = automations_with_entity(hass, entity_id)
scripts = scripts_with_entity(hass, entity_id)
if not automations and not scripts:
return []
entity_registry = er.async_get(hass)
items: list[str] = []
for integration, entities in (
("automation", automations),
("script", scripts),
):
for used_entity_id in entities:
if item := entity_registry.async_get(used_entity_id):
items.append(
f"- [{item.original_name}](/config/{integration}/edit/{item.unique_id})"
)
else:
items.append(f"- `{used_entity_id}`")
return items
@@ -3,6 +3,7 @@
"ChargeState": 2,
"ReadyState": 0,
"ChargePower": 1500.27,
"VoltageInstallation": 230,
"ChargeEnergy": 1.8,
"SlaveError": 4,
"ChargeTime": 4355,
@@ -22,8 +22,8 @@
'unique_id': 'ABC123',
'version': 1,
}),
'data': "TrydanData(ID='ABC123', charge_state=<ChargeState.CONNECTED_CHARGING: 2>, ready_state=<ReadyState.NOT_READY: 0>, charge_power=1500.27, voltage_installation=None, charge_energy=1.8, charge_mode=None, slave_error=<SlaveCommunicationState.WAITING_WIFI: 4>, charge_time=4355, house_power=0.0, fv_power=0.0, battery_power=0.0, paused=<PauseState.NOT_PAUSED: 0>, locked=<LockState.DISABLED: 0>, timer=<ChargePointTimerState.TIMER_OFF: 0>, intensity=6, dynamic=<DynamicState.DISABLED: 0>, min_intensity=6, max_intensity=16, pause_dynamic=<PauseDynamicState.MODULATING: 0>, light_led=25, logo_led=75, dynamic_power_mode=<DynamicPowerMode.TIMED_POWER_DISABLED_AND_EXCLUSIVE_MODE_SETTED: 2>, contracted_power=4600, firmware_version='2.1.7', SSID=None, IP=None, signal_status=None)",
'data': "TrydanData(ID='ABC123', charge_state=<ChargeState.CONNECTED_CHARGING: 2>, ready_state=<ReadyState.NOT_READY: 0>, charge_power=1500.27, voltage_installation=230, charge_energy=1.8, charge_mode=None, slave_error=<SlaveCommunicationState.WAITING_WIFI: 4>, charge_time=4355, house_power=0.0, fv_power=0.0, battery_power=0.0, paused=<PauseState.NOT_PAUSED: 0>, locked=<LockState.DISABLED: 0>, timer=<ChargePointTimerState.TIMER_OFF: 0>, intensity=6, dynamic=<DynamicState.DISABLED: 0>, min_intensity=6, max_intensity=16, pause_dynamic=<PauseDynamicState.MODULATING: 0>, light_led=25, logo_led=75, dynamic_power_mode=<DynamicPowerMode.TIMED_POWER_DISABLED_AND_EXCLUSIVE_MODE_SETTED: 2>, contracted_power=4600, firmware_version='2.1.7', SSID=None, IP=None, signal_status=None)",
'host_status': 200,
'raw_data': '{"ID":"ABC123","ChargeState":2,"ReadyState":0,"ChargePower":1500.27,"ChargeEnergy":1.8,"SlaveError":4,"ChargeTime":4355,"HousePower":0.0,"FVPower":0.0,"BatteryPower":0.0,"Paused":0,"Locked":0,"Timer":0,"Intensity":6,"Dynamic":0,"MinIntensity":6,"MaxIntensity":16,"PauseDynamic":0,"LightLED":25,"LogoLED":75,"FirmwareVersion":"2.1.7","DynamicPowerMode":2,"ContractedPower":4600}',
'raw_data': '{"ID":"ABC123","ChargeState":2,"ReadyState":0,"ChargePower":1500.27,"VoltageInstallation":230,"ChargeEnergy":1.8,"SlaveError":4,"ChargeTime":4355,"HousePower":0.0,"FVPower":0.0,"BatteryPower":0.0,"Paused":0,"Locked":0,"Timer":0,"Intensity":6,"Dynamic":0,"MinIntensity":6,"MaxIntensity":16,"PauseDynamic":0,"LightLED":25,"LogoLED":75,"FirmwareVersion":"2.1.7","DynamicPowerMode":2,"ContractedPower":4600}',
})
# ---
@@ -0,0 +1,245 @@
# serializer version: 1
# name: test_number[number.evse_1_1_1_1_installation_voltage-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': dict({
'max': 500,
'min': 1,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1.0,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'number',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'number.evse_1_1_1_1_installation_voltage',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Installation voltage',
'options': dict({
}),
'original_device_class': <NumberDeviceClass.VOLTAGE: 'voltage'>,
'original_icon': None,
'original_name': 'Installation voltage',
'platform': 'v2c',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'voltage_installation',
'unique_id': 'da58ee91f38c2406c2a36d0a1a7f8569_voltage_installation',
'unit_of_measurement': <UnitOfElectricPotential.VOLT: 'V'>,
})
# ---
# name: test_number[number.evse_1_1_1_1_installation_voltage-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'voltage',
'friendly_name': 'EVSE 1.1.1.1 Installation voltage',
'max': 500,
'min': 1,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1.0,
'unit_of_measurement': <UnitOfElectricPotential.VOLT: 'V'>,
}),
'context': <ANY>,
'entity_id': 'number.evse_1_1_1_1_installation_voltage',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '230',
})
# ---
# name: test_number[number.evse_1_1_1_1_intensity-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': dict({
'max': 32,
'min': 6,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1.0,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'number',
'entity_category': None,
'entity_id': 'number.evse_1_1_1_1_intensity',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Intensity',
'options': dict({
}),
'original_device_class': <NumberDeviceClass.CURRENT: 'current'>,
'original_icon': None,
'original_name': 'Intensity',
'platform': 'v2c',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'intensity',
'unique_id': 'da58ee91f38c2406c2a36d0a1a7f8569_intensity',
'unit_of_measurement': <UnitOfElectricCurrent.AMPERE: 'A'>,
})
# ---
# name: test_number[number.evse_1_1_1_1_intensity-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'current',
'friendly_name': 'EVSE 1.1.1.1 Intensity',
'max': 32,
'min': 6,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1.0,
'unit_of_measurement': <UnitOfElectricCurrent.AMPERE: 'A'>,
}),
'context': <ANY>,
'entity_id': 'number.evse_1_1_1_1_intensity',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '6',
})
# ---
# name: test_number[number.evse_1_1_1_1_max_intensity-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': dict({
'max': 32,
'min': 6,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1.0,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'number',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'number.evse_1_1_1_1_max_intensity',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Max intensity',
'options': dict({
}),
'original_device_class': <NumberDeviceClass.CURRENT: 'current'>,
'original_icon': None,
'original_name': 'Max intensity',
'platform': 'v2c',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'max_intensity',
'unique_id': 'da58ee91f38c2406c2a36d0a1a7f8569_max_intensity',
'unit_of_measurement': <UnitOfElectricCurrent.AMPERE: 'A'>,
})
# ---
# name: test_number[number.evse_1_1_1_1_max_intensity-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'current',
'friendly_name': 'EVSE 1.1.1.1 Max intensity',
'max': 32,
'min': 6,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1.0,
'unit_of_measurement': <UnitOfElectricCurrent.AMPERE: 'A'>,
}),
'context': <ANY>,
'entity_id': 'number.evse_1_1_1_1_max_intensity',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '16',
})
# ---
# name: test_number[number.evse_1_1_1_1_min_intensity-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': dict({
'max': 32,
'min': 6,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1.0,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'number',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'number.evse_1_1_1_1_min_intensity',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Min intensity',
'options': dict({
}),
'original_device_class': <NumberDeviceClass.CURRENT: 'current'>,
'original_icon': None,
'original_name': 'Min intensity',
'platform': 'v2c',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'min_intensity',
'unique_id': 'da58ee91f38c2406c2a36d0a1a7f8569_min_intensity',
'unit_of_measurement': <UnitOfElectricCurrent.AMPERE: 'A'>,
})
# ---
# name: test_number[number.evse_1_1_1_1_min_intensity-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'current',
'friendly_name': 'EVSE 1.1.1.1 Min intensity',
'max': 32,
'min': 6,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1.0,
'unit_of_measurement': <UnitOfElectricCurrent.AMPERE: 'A'>,
}),
'context': <ANY>,
'entity_id': 'number.evse_1_1_1_1_min_intensity',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '6',
})
# ---
@@ -289,64 +289,6 @@
'state': '0.0',
})
# ---
# name: test_sensor[sensor.evse_1_1_1_1_installation_voltage-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.evse_1_1_1_1_installation_voltage',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Installation voltage',
'options': dict({
'sensor': dict({
'suggested_display_precision': 0,
}),
}),
'original_device_class': <SensorDeviceClass.VOLTAGE: 'voltage'>,
'original_icon': None,
'original_name': 'Installation voltage',
'platform': 'v2c',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'voltage_installation',
'unique_id': 'da58ee91f38c2406c2a36d0a1a7f8569_voltage_installation',
'unit_of_measurement': <UnitOfElectricPotential.VOLT: 'V'>,
})
# ---
# name: test_sensor[sensor.evse_1_1_1_1_installation_voltage-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'voltage',
'friendly_name': 'EVSE 1.1.1.1 Installation voltage',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfElectricPotential.VOLT: 'V'>,
}),
'context': <ANY>,
'entity_id': 'sensor.evse_1_1_1_1_installation_voltage',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_sensor[sensor.evse_1_1_1_1_ip_address-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
+57
View File
@@ -0,0 +1,57 @@
"""Test the V2C number platform."""
from unittest.mock import AsyncMock, patch
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.number import (
ATTR_VALUE,
DOMAIN as NUMBER_DOMAIN,
SERVICE_SET_VALUE,
)
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import init_integration
from tests.common import MockConfigEntry, snapshot_platform
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_number(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
mock_v2c_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test states of the number entities."""
with patch("homeassistant.components.v2c.PLATFORMS", [Platform.NUMBER]):
await init_integration(hass, mock_config_entry)
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_number_set_value(
hass: HomeAssistant,
mock_v2c_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test setting number values."""
with patch("homeassistant.components.v2c.PLATFORMS", [Platform.NUMBER]):
await init_integration(hass, mock_config_entry)
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: "number.evse_1_1_1_1_installation_voltage",
ATTR_VALUE: 240,
},
blocking=True,
)
mock_v2c_client.voltage_installation.assert_called_once_with(240)