Skip to content

Commit bdb3146

Browse files
committed
refactor: move charge port and valet to switches, add exists_fn
Per review feedback on Hyundai-Kia-Connect#1647: - Charge port: replace open/close buttons with single switch (value_fn=ev_charge_port_door_is_open, on=open, off=close) - Valet mode: add as switch (value_fn=valet_mode_active) instead of start/stop buttons - Windows (open/close/vent): keep as buttons — 3-state (closed/vent/open) doesn't fit switch on/off - Hazard lights: keep as buttons (fire-and-forget, no state) - Add exists_fn to HyundaiKiaButtonDescription, remove hardcoded if-else checks from async_setup_entry - Remove charge port and valet button translations, add switch ones
1 parent 2a745fc commit bdb3146

4 files changed

Lines changed: 41 additions & 59 deletions

File tree

custom_components/kia_uvo/button.py

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from collections.abc import Callable
56
from dataclasses import dataclass
67
import logging
78
from typing import Final
@@ -23,6 +24,7 @@
2324
@dataclass(frozen=True, kw_only=True)
2425
class HyundaiKiaButtonDescription(ButtonEntityDescription):
2526
press_action: str
27+
exists_fn: Callable[[Vehicle], bool] = lambda _: True
2628

2729

2830
BUTTON_DESCRIPTIONS: Final[tuple[HyundaiKiaButtonDescription, ...]] = (
@@ -32,59 +34,40 @@ class HyundaiKiaButtonDescription(ButtonEntityDescription):
3234
icon="mdi:refresh",
3335
press_action="async_force_refresh_vehicle",
3436
),
35-
HyundaiKiaButtonDescription(
36-
key="open_charge_port",
37-
translation_key="open_charge_port",
38-
icon="mdi:ev-plug-charging",
39-
press_action="async_open_charge_port",
40-
),
41-
HyundaiKiaButtonDescription(
42-
key="close_charge_port",
43-
translation_key="close_charge_port",
44-
icon="mdi:ev-plug",
45-
press_action="async_close_charge_port",
46-
),
4737
HyundaiKiaButtonDescription(
4838
key="start_hazard_lights",
4939
translation_key="start_hazard_lights",
5040
icon="mdi:hazard-lights",
5141
press_action="async_start_hazard_lights",
42+
exists_fn=lambda vehicle: vehicle.supports_window_control is not None,
5243
),
5344
HyundaiKiaButtonDescription(
5445
key="start_hazard_lights_and_horn",
5546
translation_key="start_hazard_lights_and_horn",
5647
icon="mdi:car-emergency",
5748
press_action="async_start_hazard_lights_and_horn",
58-
),
59-
HyundaiKiaButtonDescription(
60-
key="start_valet_mode",
61-
translation_key="start_valet_mode",
62-
icon="mdi:key-variant",
63-
press_action="async_start_valet_mode",
64-
),
65-
HyundaiKiaButtonDescription(
66-
key="stop_valet_mode",
67-
translation_key="stop_valet_mode",
68-
icon="mdi:key",
69-
press_action="async_stop_valet_mode",
49+
exists_fn=lambda vehicle: vehicle.supports_window_control is not None,
7050
),
7151
HyundaiKiaButtonDescription(
7252
key="open_all_windows",
7353
translation_key="open_all_windows",
7454
icon="mdi:window-maximize",
7555
press_action="async_open_all_windows",
56+
exists_fn=lambda vehicle: vehicle.front_left_window_is_open is not None,
7657
),
7758
HyundaiKiaButtonDescription(
7859
key="close_all_windows",
7960
translation_key="close_all_windows",
8061
icon="mdi:window-minimize",
8162
press_action="async_close_all_windows",
63+
exists_fn=lambda vehicle: vehicle.front_left_window_is_open is not None,
8264
),
8365
HyundaiKiaButtonDescription(
8466
key="vent_all_windows",
8567
translation_key="vent_all_windows",
8668
icon="mdi:window-open-variant",
8769
press_action="async_vent_all_windows",
70+
exists_fn=lambda vehicle: vehicle.front_left_window_is_open is not None,
8871
),
8972
)
9073

@@ -99,17 +82,10 @@ async def async_setup_entry(
9982
for vehicle_id in coordinator.vehicle_manager.vehicles.keys():
10083
vehicle: Vehicle = coordinator.vehicle_manager.vehicles[vehicle_id]
10184
for description in BUTTON_DESCRIPTIONS:
102-
if description.key in ("open_charge_port", "close_charge_port"):
103-
if getattr(vehicle, "ev_charge_port_door_is_open", None) is None:
104-
continue
105-
if description.key in (
106-
"open_all_windows",
107-
"close_all_windows",
108-
"vent_all_windows",
109-
):
110-
if getattr(vehicle, "front_left_window_is_open", None) is None:
111-
continue
112-
entities.append(HyundaiKiaConnectButton(coordinator, description, vehicle))
85+
if description.exists_fn(vehicle):
86+
entities.append(
87+
HyundaiKiaConnectButton(coordinator, description, vehicle)
88+
)
11389

11490
async_add_entities(entities)
11591

custom_components/kia_uvo/strings.json

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -439,24 +439,12 @@
439439
"force_refresh": {
440440
"name": "Force Refresh"
441441
},
442-
"open_charge_port": {
443-
"name": "Open Charge Port"
444-
},
445-
"close_charge_port": {
446-
"name": "Close Charge Port"
447-
},
448442
"start_hazard_lights": {
449443
"name": "Start Hazard Lights"
450444
},
451445
"start_hazard_lights_and_horn": {
452446
"name": "Start Hazard Lights and Horn"
453447
},
454-
"start_valet_mode": {
455-
"name": "Start Valet Mode"
456-
},
457-
"stop_valet_mode": {
458-
"name": "Stop Valet Mode"
459-
},
460448
"open_all_windows": {
461449
"name": "Open All Windows"
462450
},
@@ -505,6 +493,12 @@
505493
},
506494
"ev_second_departure_enabled": {
507495
"name": "EV Scheduled Departure 2"
496+
},
497+
"ev_charge_port_door": {
498+
"name": "Charge Port"
499+
},
500+
"valet_mode_control": {
501+
"name": "Valet Mode"
508502
}
509503
},
510504
"climate": {

custom_components/kia_uvo/switch.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ class HyundaiKiaSwitchDescription(SwitchEntityDescription):
135135
vid, 2, False
136136
),
137137
),
138+
HyundaiKiaSwitchDescription(
139+
key="ev_charge_port_door_is_open",
140+
translation_key="ev_charge_port_door",
141+
icon="mdi:ev-plug-charging",
142+
value_fn=lambda vehicle: vehicle.ev_charge_port_door_is_open,
143+
exists_fn=lambda vehicle: vehicle.ev_charge_port_door_is_open is not None,
144+
on_fn=lambda coordinator, vid: coordinator.async_open_charge_port(vid),
145+
off_fn=lambda coordinator, vid: coordinator.async_close_charge_port(vid),
146+
),
147+
HyundaiKiaSwitchDescription(
148+
key="valet_mode_control",
149+
translation_key="valet_mode_control",
150+
icon="mdi:key-variant",
151+
value_fn=lambda vehicle: vehicle.valet_mode_active,
152+
exists_fn=lambda vehicle: vehicle.valet_mode_active is not None,
153+
on_fn=lambda coordinator, vid: coordinator.async_start_valet_mode(vid),
154+
off_fn=lambda coordinator, vid: coordinator.async_stop_valet_mode(vid),
155+
),
138156
)
139157

140158

custom_components/kia_uvo/translations/en.json

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -745,24 +745,12 @@
745745
"force_refresh": {
746746
"name": "Force Refresh"
747747
},
748-
"open_charge_port": {
749-
"name": "Open Charge Port"
750-
},
751-
"close_charge_port": {
752-
"name": "Close Charge Port"
753-
},
754748
"start_hazard_lights": {
755749
"name": "Start Hazard Lights"
756750
},
757751
"start_hazard_lights_and_horn": {
758752
"name": "Start Hazard Lights and Horn"
759753
},
760-
"start_valet_mode": {
761-
"name": "Start Valet Mode"
762-
},
763-
"stop_valet_mode": {
764-
"name": "Stop Valet Mode"
765-
},
766754
"open_all_windows": {
767755
"name": "Open All Windows"
768756
},
@@ -811,6 +799,12 @@
811799
},
812800
"ev_second_departure_enabled": {
813801
"name": "EV Scheduled Departure 2"
802+
},
803+
"ev_charge_port_door": {
804+
"name": "Charge Port"
805+
},
806+
"valet_mode_control": {
807+
"name": "Valet Mode"
814808
}
815809
},
816810
"climate": {

0 commit comments

Comments
 (0)