Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service to set the AC schedule of renault vehicles #125006

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions homeassistant/components/renault/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
},
"charge_set_schedules": {
"service": "mdi:calendar-clock"
},
"ac_set_schedules": {
"service": "mdi:calendar-clock"
}
}
}
12 changes: 12 additions & 0 deletions homeassistant/components/renault/renault_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ async def set_ac_start(
"""Start vehicle ac."""
return await self._vehicle.set_ac_start(temperature, when)

@with_error_wrapping
async def get_hvac_settings(self) -> models.KamereonVehicleHvacSettingsData:
"""Get vehicle hvac settings."""
return await self._vehicle.get_hvac_settings()

@with_error_wrapping
async def set_hvac_schedules(
self, schedules: list[models.HvacSchedule]
) -> models.KamereonVehicleHvacScheduleActionData:
"""Set vehicle hvac schedules."""
return await self._vehicle.set_hvac_schedules(schedules)

@with_error_wrapping
async def get_charging_settings(self) -> models.KamereonVehicleChargingSettingsData:
"""Get vehicle charging settings."""
Expand Down
60 changes: 59 additions & 1 deletion homeassistant/components/renault/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,43 @@
}
)

SERVICE_AC_SET_SCHEDULE_DAY_SCHEMA = vol.Schema(
{
vol.Required("readyAtTime"): cv.string,
}
)

SERVICE_AC_SET_SCHEDULE_SCHEMA = vol.Schema(
{
vol.Required("id"): cv.positive_int,
vol.Optional("activated"): cv.boolean,
vol.Optional("monday"): vol.Any(None, SERVICE_AC_SET_SCHEDULE_DAY_SCHEMA),
vol.Optional("tuesday"): vol.Any(None, SERVICE_AC_SET_SCHEDULE_DAY_SCHEMA),
vol.Optional("wednesday"): vol.Any(None, SERVICE_AC_SET_SCHEDULE_DAY_SCHEMA),
vol.Optional("thursday"): vol.Any(None, SERVICE_AC_SET_SCHEDULE_DAY_SCHEMA),
vol.Optional("friday"): vol.Any(None, SERVICE_AC_SET_SCHEDULE_DAY_SCHEMA),
vol.Optional("saturday"): vol.Any(None, SERVICE_AC_SET_SCHEDULE_DAY_SCHEMA),
vol.Optional("sunday"): vol.Any(None, SERVICE_AC_SET_SCHEDULE_DAY_SCHEMA),
}
)
SERVICE_AC_SET_SCHEDULES_SCHEMA = SERVICE_VEHICLE_SCHEMA.extend(
{
vol.Required(ATTR_SCHEDULES): vol.All(
cv.ensure_list, [SERVICE_AC_SET_SCHEDULE_SCHEMA]
),
}
)

SERVICE_AC_CANCEL = "ac_cancel"
SERVICE_AC_START = "ac_start"
SERVICE_CHARGE_SET_SCHEDULES = "charge_set_schedules"
SERVICES = [SERVICE_AC_CANCEL, SERVICE_AC_START, SERVICE_CHARGE_SET_SCHEDULES]
SERVICE_AC_SET_SCHEDULES = "ac_set_schedules"
SERVICES = [
SERVICE_AC_CANCEL,
SERVICE_AC_START,
SERVICE_CHARGE_SET_SCHEDULES,
SERVICE_AC_SET_SCHEDULES,
]


def setup_services(hass: HomeAssistant) -> None:
Expand Down Expand Up @@ -111,6 +144,25 @@ async def charge_set_schedules(service_call: ServiceCall) -> None:
"It may take some time before these changes are reflected in your vehicle"
)

async def ac_set_schedules(service_call: ServiceCall) -> None:
"""Set A/C schedules."""
schedules: list[dict[str, Any]] = service_call.data[ATTR_SCHEDULES]
proxy = get_vehicle_proxy(service_call.data)
hvac_schedules = await proxy.get_hvac_settings()

for schedule in schedules:
hvac_schedules.update(schedule)

if TYPE_CHECKING:
assert hvac_schedules.schedules is not None
LOGGER.debug("HVAC set schedules attempt: %s", schedules)
result = await proxy.set_hvac_schedules(hvac_schedules.schedules)

LOGGER.debug("HVAC set schedules result: %s", result)
LOGGER.debug(
"It may take some time before these changes are reflected in your vehicle"
)

def get_vehicle_proxy(service_call_data: Mapping) -> RenaultVehicleProxy:
"""Get vehicle from service_call data."""
device_registry = dr.async_get(hass)
Expand Down Expand Up @@ -148,3 +200,9 @@ def get_vehicle_proxy(service_call_data: Mapping) -> RenaultVehicleProxy:
charge_set_schedules,
schema=SERVICE_CHARGE_SET_SCHEDULES_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_AC_SET_SCHEDULES,
ac_set_schedules,
schema=SERVICE_AC_SET_SCHEDULES_SCHEMA,
)
37 changes: 37 additions & 0 deletions homeassistant/components/renault/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,43 @@ ac_cancel:
device:
integration: renault

ac_set_schedules:
fields:
vehicle:
required: true
selector:
device:
integration: renault
schedules:
example: >-
[
{
"id": 1,
"activated": false
},
{
"id": 2,
"activated": true,
"monday": { "readyAtTime": "T20:45Z" },
"sunday": { "readyAtTime": "T20:45Z" }
},
{
"id": 3,
"activated": false
},
{
"id": 4,
"activated": false
},
{
"id": 5,
"activated": false
}
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not create services that require the user to put in JSON objects. This is too error prone imo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed both examples in the file. Also updated the documentation.

required: true
selector:
object:

charge_set_schedules:
fields:
vehicle:
Expand Down
16 changes: 15 additions & 1 deletion homeassistant/components/renault/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
},
"ac_cancel": {
"name": "Cancel A/C",
"description": "Canceles A/C on vehicle.",
"description": "Cancels A/C on vehicle.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could have been a separate bugfix PR 😉 but I think it's ok to leave it in here.

"fields": {
"vehicle": {
"name": "Vehicle",
Expand All @@ -195,6 +195,20 @@
"description": "Schedule details."
}
}
},
"ac_set_schedules": {
"name": "Update A/C schedule",
"description": "Updates A/C schedule on vehicle.",
"fields": {
"vehicle": {
"name": "Vehicle",
"description": "[%key:component::renault::services::ac_start::fields::vehicle::description%]"
},
"schedules": {
"name": "Schedules",
"description": "[%key:component::renault::services::charge_set_schedules::fields::schedules::description%]"
}
}
}
}
}
20 changes: 20 additions & 0 deletions tests/components/renault/fixtures/action.set_ac_schedules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"data": {
"type": "HvacSchedule",
"id": "guid",
"attributes": {
"schedules": [
{
"id": 1,
"activated": true,
"tuesday": { "readyAtTime": "T04:30Z" },
"wednesday": { "readyAtTime": "T22:30Z" },
"thursday": { "readyAtTime": "T22:00Z" },
"friday": { "readyAtTime": "T23:30Z" },
"saturday": { "readyAtTime": "T18:30Z" },
"sunday": { "readyAtTime": "T12:45Z" }
}
]
}
}
}
41 changes: 41 additions & 0 deletions tests/components/renault/fixtures/hvac_settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"data": {
"type": "Car",
"id": "VF1AAAAA555777999",
"attributes": {
"dateTime": "2020-12-24T20:00:00.000Z",
"mode": "scheduled",
"schedules": [
{
"id": 1,
"activated": false
},
{
"id": 2,
"activated": true,
"wednesday": { "readyAtTime": "T15:15Z" },
"friday": { "readyAtTime": "T15:15Z" }
},
{
"id": 3,
"activated": false,
"monday": { "readyAtTime": "T23:30Z" },
"tuesday": { "readyAtTime": "T23:30Z" },
"wednesday": { "readyAtTime": "T23:30Z" },
"thursday": { "readyAtTime": "T23:30Z" },
"friday": { "readyAtTime": "T23:30Z" },
"saturday": { "readyAtTime": "T23:30Z" },
"sunday": { "readyAtTime": "T23:30Z" }
},
{
"id": 4,
"activated": false
},
{
"id": 5,
"activated": false
}
]
}
}
}
Loading