Skip to content

Commit 27b32c5

Browse files
thomasddnballoob
authored andcommitted
Show charging power as 0 when not charging for the Volvo integration (home-assistant#150797)
1 parent 332996c commit 27b32c5

File tree

10 files changed

+168
-16
lines changed

10 files changed

+168
-16
lines changed

homeassistant/components/volvo/coordinator.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,40 @@ def __init__(
260260
"Volvo medium interval coordinator",
261261
)
262262

263+
self._supported_capabilities: list[str] = []
264+
263265
async def _async_determine_api_calls(
264266
self,
265267
) -> list[Callable[[], Coroutine[Any, Any, Any]]]:
266268
if self.vehicle.has_battery_engine():
267269
capabilities = await self.api.async_get_energy_capabilities()
268270

269271
if capabilities.get("isSupported", False):
270-
return [self.api.async_get_energy_state]
272+
self._supported_capabilities = [
273+
key
274+
for key, value in capabilities.items()
275+
if isinstance(value, dict) and value.get("isSupported", False)
276+
]
277+
278+
return [self._async_get_energy_state]
271279

272280
return []
281+
282+
async def _async_get_energy_state(
283+
self,
284+
) -> dict[str, VolvoCarsValueStatusField | None]:
285+
def _mark_ok(
286+
field: VolvoCarsValueStatusField | None,
287+
) -> VolvoCarsValueStatusField | None:
288+
if field:
289+
field.status = "OK"
290+
291+
return field
292+
293+
energy_state = await self.api.async_get_energy_state()
294+
295+
return {
296+
key: _mark_ok(value)
297+
for key, value in energy_state.items()
298+
if key in self._supported_capabilities
299+
}

homeassistant/components/volvo/sensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def _calculate_time_to_service(field: VolvoCarsValue) -> int:
6767

6868
def _charging_power_value(field: VolvoCarsValue) -> int:
6969
return (
70-
int(field.value)
71-
if isinstance(field, VolvoCarsValueStatusField) and field.status == "OK"
70+
field.value
71+
if isinstance(field, VolvoCarsValueStatusField) and isinstance(field.value, int)
7272
else 0
7373
)
7474

tests/components/volvo/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from volvocarsapi.models import (
1010
VolvoCarsAvailableCommand,
1111
VolvoCarsLocation,
12-
VolvoCarsValueField,
12+
VolvoCarsValueStatusField,
1313
VolvoCarsVehicle,
1414
)
1515

@@ -98,7 +98,7 @@ async def mock_api(hass: HomeAssistant, full_model: str) -> AsyncGenerator[Async
9898
hass, "energy_state", full_model
9999
)
100100
energy_state = {
101-
key: VolvoCarsValueField.from_dict(value)
101+
key: VolvoCarsValueStatusField.from_dict(value)
102102
for key, value in energy_state_data.items()
103103
}
104104
engine_status = await async_load_fixture_as_value_field(

tests/components/volvo/fixtures/ex30_2024/energy_capabilities.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"chargerConnectionStatus": {
1010
"isSupported": true
1111
},
12-
"chargingSystemStatus": {
12+
"chargingStatus": {
1313
"isSupported": true
1414
},
1515
"chargingType": {
@@ -25,7 +25,7 @@
2525
"isSupported": true
2626
},
2727
"chargingCurrentLimit": {
28-
"isSupported": true
28+
"isSupported": false
2929
},
3030
"chargingPower": {
3131
"isSupported": true

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
},
5151
"chargingPower": {
5252
"status": "ERROR",
53-
"code": "NOT_SUPPORTED",
54-
"message": "Resource is not supported for this vehicle"
53+
"code": "PROPERTY_NOT_FOUND",
54+
"message": "No valid value could be found for the requested property"
5555
}
5656
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"chargerConnectionStatus": {
1010
"isSupported": true
1111
},
12-
"chargingSystemStatus": {
12+
"chargingStatus": {
1313
"isSupported": true
1414
},
1515
"chargingType": {

tests/components/volvo/fixtures/xc60_phev_2020/energy_capabilities.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"chargerConnectionStatus": {
1010
"isSupported": true
1111
},
12-
"chargingSystemStatus": {
12+
"chargingStatus": {
1313
"isSupported": true
1414
},
1515
"chargingType": {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@
4040
"message": "Resource is not supported for this vehicle"
4141
},
4242
"targetBatteryChargeLevel": {
43-
"status": "ERROR",
44-
"code": "NOT_SUPPORTED",
45-
"message": "Resource is not supported for this vehicle"
43+
"status": "OK",
44+
"value": 80,
45+
"unit": "percentage",
46+
"updatedAt": "2024-09-22T09:40:12Z"
4647
},
4748
"chargingPower": {
4849
"status": "ERROR",

tests/components/volvo/snapshots/test_sensor.ambr

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,62 @@
232232
'state': 'connected',
233233
})
234234
# ---
235+
# name: test_sensor[ex30_2024][sensor.volvo_ex30_charging_power-entry]
236+
EntityRegistryEntrySnapshot({
237+
'aliases': set({
238+
}),
239+
'area_id': None,
240+
'capabilities': dict({
241+
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
242+
}),
243+
'config_entry_id': <ANY>,
244+
'config_subentry_id': <ANY>,
245+
'device_class': None,
246+
'device_id': <ANY>,
247+
'disabled_by': None,
248+
'domain': 'sensor',
249+
'entity_category': None,
250+
'entity_id': 'sensor.volvo_ex30_charging_power',
251+
'has_entity_name': True,
252+
'hidden_by': None,
253+
'icon': None,
254+
'id': <ANY>,
255+
'labels': set({
256+
}),
257+
'name': None,
258+
'options': dict({
259+
'sensor': dict({
260+
'suggested_display_precision': 0,
261+
}),
262+
}),
263+
'original_device_class': <SensorDeviceClass.POWER: 'power'>,
264+
'original_icon': None,
265+
'original_name': 'Charging power',
266+
'platform': 'volvo',
267+
'previous_unique_id': None,
268+
'suggested_object_id': None,
269+
'supported_features': 0,
270+
'translation_key': 'charging_power',
271+
'unique_id': 'yv1abcdefg1234567_charging_power',
272+
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
273+
})
274+
# ---
275+
# name: test_sensor[ex30_2024][sensor.volvo_ex30_charging_power-state]
276+
StateSnapshot({
277+
'attributes': ReadOnlyDict({
278+
'device_class': 'power',
279+
'friendly_name': 'Volvo EX30 Charging power',
280+
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
281+
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
282+
}),
283+
'context': <ANY>,
284+
'entity_id': 'sensor.volvo_ex30_charging_power',
285+
'last_changed': <ANY>,
286+
'last_reported': <ANY>,
287+
'last_updated': <ANY>,
288+
'state': '0',
289+
})
290+
# ---
235291
# name: test_sensor[ex30_2024][sensor.volvo_ex30_charging_power_status-entry]
236292
EntityRegistryEntrySnapshot({
237293
'aliases': set({
@@ -2164,7 +2220,7 @@
21642220
'last_changed': <ANY>,
21652221
'last_reported': <ANY>,
21662222
'last_updated': <ANY>,
2167-
'state': '0',
2223+
'state': '1386',
21682224
})
21692225
# ---
21702226
# name: test_sensor[xc40_electric_2024][sensor.volvo_xc40_charging_power_status-entry]
@@ -3601,6 +3657,58 @@
36013657
'state': '30000',
36023658
})
36033659
# ---
3660+
# name: test_sensor[xc60_phev_2020][sensor.volvo_xc60_target_battery_charge_level-entry]
3661+
EntityRegistryEntrySnapshot({
3662+
'aliases': set({
3663+
}),
3664+
'area_id': None,
3665+
'capabilities': None,
3666+
'config_entry_id': <ANY>,
3667+
'config_subentry_id': <ANY>,
3668+
'device_class': None,
3669+
'device_id': <ANY>,
3670+
'disabled_by': None,
3671+
'domain': 'sensor',
3672+
'entity_category': None,
3673+
'entity_id': 'sensor.volvo_xc60_target_battery_charge_level',
3674+
'has_entity_name': True,
3675+
'hidden_by': None,
3676+
'icon': None,
3677+
'id': <ANY>,
3678+
'labels': set({
3679+
}),
3680+
'name': None,
3681+
'options': dict({
3682+
'sensor': dict({
3683+
'suggested_display_precision': 0,
3684+
}),
3685+
}),
3686+
'original_device_class': None,
3687+
'original_icon': None,
3688+
'original_name': 'Target battery charge level',
3689+
'platform': 'volvo',
3690+
'previous_unique_id': None,
3691+
'suggested_object_id': None,
3692+
'supported_features': 0,
3693+
'translation_key': 'target_battery_charge_level',
3694+
'unique_id': 'yv1abcdefg1234567_target_battery_charge_level',
3695+
'unit_of_measurement': '%',
3696+
})
3697+
# ---
3698+
# name: test_sensor[xc60_phev_2020][sensor.volvo_xc60_target_battery_charge_level-state]
3699+
StateSnapshot({
3700+
'attributes': ReadOnlyDict({
3701+
'friendly_name': 'Volvo XC60 Target battery charge level',
3702+
'unit_of_measurement': '%',
3703+
}),
3704+
'context': <ANY>,
3705+
'entity_id': 'sensor.volvo_xc60_target_battery_charge_level',
3706+
'last_changed': <ANY>,
3707+
'last_reported': <ANY>,
3708+
'last_updated': <ANY>,
3709+
'state': '80',
3710+
})
3711+
# ---
36043712
# name: test_sensor[xc60_phev_2020][sensor.volvo_xc60_time_to_engine_service-entry]
36053713
EntityRegistryEntrySnapshot({
36063714
'aliases': set({

tests/components/volvo/test_sensor.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,20 @@ async def test_skip_invalid_api_fields(
6868
with patch("homeassistant.components.volvo.PLATFORMS", [Platform.SENSOR]):
6969
assert await setup_integration()
7070

71-
assert not hass.states.get(f"sensor.volvo_{short_model}_charging_power")
71+
assert not hass.states.get(f"sensor.volvo_{short_model}_charging_current_limit")
72+
73+
74+
@pytest.mark.parametrize(
75+
"full_model",
76+
["ex30_2024"],
77+
)
78+
async def test_charging_power_value(
79+
hass: HomeAssistant,
80+
setup_integration: Callable[[], Awaitable[bool]],
81+
) -> None:
82+
"""Test if charging_power_value is zero if supported, but not charging."""
83+
84+
with patch("homeassistant.components.volvo.PLATFORMS", [Platform.SENSOR]):
85+
assert await setup_integration()
86+
87+
assert hass.states.get("sensor.volvo_ex30_charging_power").state == "0"

0 commit comments

Comments
 (0)