Skip to content

Commit a1731cd

Browse files
thomasddnfrenck
authored andcommitted
Volvo: fix distance to empty battery (home-assistant#150278)
1 parent 0c31ec9 commit a1731cd

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

homeassistant/components/volvo/sensor.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from collections.abc import Callable
6-
from dataclasses import dataclass, replace
6+
from dataclasses import dataclass
77
import logging
88
from typing import Any, cast
99

@@ -47,7 +47,6 @@
4747
class VolvoSensorDescription(VolvoEntityDescription, SensorEntityDescription):
4848
"""Describes a Volvo sensor entity."""
4949

50-
source_fields: list[str] | None = None
5150
value_fn: Callable[[VolvoCarsValue], Any] | None = None
5251

5352

@@ -240,11 +239,15 @@ def _charging_power_status_value(field: VolvoCarsValue) -> str | None:
240239
"none",
241240
],
242241
),
243-
# statistics & energy state endpoint
242+
# statistics endpoint
243+
# We're not using `electricRange` from the energy state endpoint because
244+
# the official app seems to use `distanceToEmptyBattery`.
245+
# In issue #150213, a user described to behavior as follows:
246+
# - For a `distanceToEmptyBattery` of 250km, the `electricRange` was 150mi
247+
# - For a `distanceToEmptyBattery` of 260km, the `electricRange` was 160mi
244248
VolvoSensorDescription(
245249
key="distance_to_empty_battery",
246-
api_field="",
247-
source_fields=["distanceToEmptyBattery", "electricRange"],
250+
api_field="distanceToEmptyBattery",
248251
native_unit_of_measurement=UnitOfLength.KILOMETERS,
249252
device_class=SensorDeviceClass.DISTANCE,
250253
state_class=SensorStateClass.MEASUREMENT,
@@ -362,12 +365,7 @@ def _add_entity(
362365
if description.key in added_keys:
363366
continue
364367

365-
if description.source_fields:
366-
for field in description.source_fields:
367-
if field in coordinator.data:
368-
description = replace(description, api_field=field)
369-
_add_entity(coordinator, description)
370-
elif description.api_field in coordinator.data:
368+
if description.api_field in coordinator.data:
371369
_add_entity(coordinator, description)
372370

373371
async_add_entities(entities)

tests/components/volvo/fixtures/xc40_electric_2024/energy_state.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
},
88
"electricRange": {
99
"status": "OK",
10-
"value": 220,
11-
"unit": "km",
10+
"value": 150,
11+
"unit": "mi",
1212
"updatedAt": "2025-07-02T08:51:23Z"
1313
},
1414
"chargerConnectionStatus": {

tests/components/volvo/snapshots/test_sensor.ambr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@
585585
'last_changed': <ANY>,
586586
'last_reported': <ANY>,
587587
'last_updated': <ANY>,
588-
'state': '327',
588+
'state': '250',
589589
})
590590
# ---
591591
# name: test_sensor[ex30_2024][sensor.volvo_ex30_distance_to_service-entry]
@@ -2514,7 +2514,7 @@
25142514
'last_changed': <ANY>,
25152515
'last_reported': <ANY>,
25162516
'last_updated': <ANY>,
2517-
'state': '220',
2517+
'state': '250',
25182518
})
25192519
# ---
25202520
# name: test_sensor[xc40_electric_2024][sensor.volvo_xc40_distance_to_service-entry]

tests/components/volvo/test_sensor.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,19 @@ async def test_sensor(
3030
assert await setup_integration()
3131

3232
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
33+
34+
35+
@pytest.mark.parametrize(
36+
"full_model",
37+
["xc40_electric_2024"],
38+
)
39+
async def test_distance_to_empty_battery(
40+
hass: HomeAssistant,
41+
setup_integration: Callable[[], Awaitable[bool]],
42+
) -> None:
43+
"""Test using `distanceToEmptyBattery` instead of `electricRange`."""
44+
45+
with patch("homeassistant.components.volvo.PLATFORMS", [Platform.SENSOR]):
46+
assert await setup_integration()
47+
48+
assert hass.states.get("sensor.volvo_xc40_distance_to_empty_battery").state == "250"

0 commit comments

Comments
 (0)