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 ability to update schedules #84

Merged
merged 2 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 42 additions & 0 deletions src/renault_api/kamereon/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Helpers for Kamereon models."""
from __future__ import annotations

from typing import Any
from typing import Dict

from . import models


_DAYS = [
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday",
]


def update_schedule(schedule: models.ChargeSchedule, settings: Dict[str, Any]) -> None:
"""Update schedule."""
for day in _DAYS:
if day in settings.keys():
day_settings = settings[day]

if day_settings: # pragma: no branch
start_time = day_settings["startTime"]
duration = day_settings["duration"]

setattr(
schedule,
day,
models.ChargeDaySchedule(day_settings, start_time, duration),
)


def create_schedule(
settings: Dict[str, Any]
) -> models.ChargeSchedule: # pragma: no cover
"""Update schedule."""
raise NotImplementedError
13 changes: 13 additions & 0 deletions src/renault_api/kamereon/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from . import enums
from . import exceptions
from . import helpers
from renault_api.models import BaseModel

COMMON_ERRRORS: List[Dict[str, Any]] = [
Expand Down Expand Up @@ -367,6 +368,18 @@ class KamereonVehicleChargingSettingsData(KamereonVehicleDataAttributes):
mode: Optional[str]
schedules: Optional[List[ChargeSchedule]]

def update(self, args: Dict[str, Any]) -> None:
"""Update schedule."""
if "id" not in args: # pragma: no cover
raise ValueError("id not provided for update.")
if self.schedules is None: # pragma: no cover
self.schedules = []
for schedule in self.schedules:
if schedule.id == args["id"]: # pragma: no branch
helpers.update_schedule(schedule, args)
return
self.schedules.append(helpers.create_schedule(args)) # pragma: no cover


@dataclass
class KamereonVehicleNotificationSettingsData(KamereonVehicleDataAttributes):
Expand Down
3 changes: 0 additions & 3 deletions tests/kamereon/test_kamereon_vehicle_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,6 @@ def test_charging_settings() -> None:
assert schedule_data.sunday.startTime == "T12:45Z"
assert schedule_data.sunday.duration == 45

# Check that for_json returns the same as the original data
assert schedule_data.for_json() == schedule_data.raw_data


def test_location() -> None:
"""Test vehicle data for location.json."""
Expand Down
91 changes: 91 additions & 0 deletions tests/kamereon/test_kamereon_vehicle_schedule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""Tests for Kamereon models."""
from typing import cast

from tests import get_response_content

from renault_api.kamereon import models
from renault_api.kamereon import schemas


FIXTURE_PATH = "tests/fixtures/kamereon/vehicle_data"

TEST_UPDATE = {
"id": 1,
"tuesday": {"startTime": "T12:00Z", "duration": 15},
}


def test_for_json() -> None:
"""Test for updating charge settings."""
response: models.KamereonVehicleDataResponse = get_response_content(
f"{FIXTURE_PATH}/charging-settings.json",
schemas.KamereonVehicleDataResponseSchema,
)
response.raise_for_error_code()
vehicle_data = cast(
models.KamereonVehicleChargingSettingsData,
response.get_attributes(schemas.KamereonVehicleChargingSettingsDataSchema),
)

# Check that for_json returns the same as the original data
for_json = {
"schedules": list(schedule.for_json() for schedule in vehicle_data.schedules)
}
assert for_json == {
"schedules": [
{
"id": 1,
"activated": True,
"monday": {"startTime": "T12:00Z", "duration": 15},
"tuesday": {"startTime": "T04:30Z", "duration": 420},
"wednesday": {"startTime": "T22:30Z", "duration": 420},
"thursday": {"startTime": "T22:00Z", "duration": 420},
"friday": {"startTime": "T12:15Z", "duration": 15},
"saturday": {"startTime": "T12:30Z", "duration": 30},
"sunday": {"startTime": "T12:45Z", "duration": 45},
}
]
}

# Check that for_json returns the same as the original data
for_json = {
"schedules": list(schedule.for_json() for schedule in vehicle_data.schedules)
}
assert for_json == {
"schedules": [
{
"id": 1,
"activated": True,
"monday": {"startTime": "T12:00Z", "duration": 15},
"tuesday": {"startTime": "T04:30Z", "duration": 420},
"wednesday": {"startTime": "T22:30Z", "duration": 420},
"thursday": {"startTime": "T22:00Z", "duration": 420},
"friday": {"startTime": "T12:15Z", "duration": 15},
"saturday": {"startTime": "T12:30Z", "duration": 30},
"sunday": {"startTime": "T12:45Z", "duration": 45},
}
]
}

vehicle_data.update(TEST_UPDATE)
assert vehicle_data.schedules[0].tuesday.startTime == "T12:00Z"
assert vehicle_data.schedules[0].tuesday.duration == 15

for_json = {
"schedules": list(schedule.for_json() for schedule in vehicle_data.schedules)
}
assert for_json == {
"schedules": [
{
"id": 1,
"activated": True,
"monday": {"startTime": "T12:00Z", "duration": 15},
"tuesday": {"startTime": "T12:00Z", "duration": 15},
"wednesday": {"startTime": "T22:30Z", "duration": 420},
"thursday": {"startTime": "T22:00Z", "duration": 420},
"friday": {"startTime": "T12:15Z", "duration": 15},
"saturday": {"startTime": "T12:30Z", "duration": 30},
"sunday": {"startTime": "T12:45Z", "duration": 45},
}
]
}