Skip to content

Commit 250d025

Browse files
committed
Add validation to require time when enabling scheduled charging
1 parent 7acf9ea commit 250d025

File tree

2 files changed

+15
-41
lines changed

2 files changed

+15
-41
lines changed

homeassistant/components/teslemetry/services.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,25 @@ async def set_scheduled_charging(call: ServiceCall) -> None:
159159
)
160160
)
161161

162+
def _validate_scheduled_charging(obj):
163+
"""Validate the scheduled charging schema."""
164+
if obj[ATTR_ENABLE] and ATTR_TIME not in obj:
165+
raise vol.Invalid(f"{ATTR_TIME} is required when {ATTR_ENABLE} is true")
166+
return obj
167+
162168
hass.services.async_register(
163169
DOMAIN,
164170
SERVICE_SET_SCHEDULED_CHARGING,
165171
set_scheduled_charging,
166-
schema=vol.Schema(
167-
{
168-
vol.Required(CONF_DEVICE_ID): cv.string,
169-
vol.Required(ATTR_ENABLE): bool,
170-
vol.Optional(ATTR_TIME): cv.time,
171-
}
172+
schema=vol.All(
173+
vol.Schema(
174+
{
175+
vol.Required(CONF_DEVICE_ID): cv.string,
176+
vol.Required(ATTR_ENABLE): bool,
177+
vol.Optional(ATTR_TIME): cv.time,
178+
}
179+
),
180+
_validate_scheduled_charging,
172181
),
173182
)
174183

tests/components/teslemetry/test_services.py

-35
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,6 @@ async def test_services(
9494
)
9595
set_scheduled_charging.assert_called_once()
9696

97-
with pytest.raises(ServiceValidationError):
98-
await hass.services.async_call(
99-
DOMAIN,
100-
SERVICE_SET_SCHEDULED_CHARGING,
101-
{
102-
CONF_DEVICE_ID: vehicle_device,
103-
ATTR_ENABLE: True,
104-
},
105-
blocking=True,
106-
)
107-
10897
with patch(
10998
"tesla_fleet_api.teslemetry.Vehicle.set_scheduled_departure",
11099
return_value=COMMAND_OK,
@@ -126,30 +115,6 @@ async def test_services(
126115
)
127116
set_scheduled_departure.assert_called_once()
128117

129-
with pytest.raises(ServiceValidationError):
130-
await hass.services.async_call(
131-
DOMAIN,
132-
SERVICE_SET_SCHEDULED_DEPARTURE,
133-
{
134-
CONF_DEVICE_ID: vehicle_device,
135-
ATTR_ENABLE: True,
136-
ATTR_PRECONDITIONING_ENABLED: True,
137-
},
138-
blocking=True,
139-
)
140-
141-
with pytest.raises(ServiceValidationError):
142-
await hass.services.async_call(
143-
DOMAIN,
144-
SERVICE_SET_SCHEDULED_DEPARTURE,
145-
{
146-
CONF_DEVICE_ID: vehicle_device,
147-
ATTR_ENABLE: True,
148-
ATTR_OFF_PEAK_CHARGING_ENABLED: True,
149-
},
150-
blocking=True,
151-
)
152-
153118
with patch(
154119
"tesla_fleet_api.teslemetry.Vehicle.set_valet_mode",
155120
return_value=COMMAND_OK,

0 commit comments

Comments
 (0)