Skip to content

Commit

Permalink
Add set-minimum-charge-limit feature (#293)
Browse files Browse the repository at this point in the history
* add set-minimum-charge-limit feature

* update api_endpoints.md

---------

Co-authored-by: Tomas Prvak <[email protected]>
  • Loading branch information
prvakt and Tomas Prvak authored Dec 20, 2024
1 parent 77fd272 commit 374a7c6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/api_endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
| sonar98 | POST | api/v1/authentication/revoke-token | | |
| sonar98 | GET | api/v1/vehicle-automatization/{vin}/departure/timers | | |
| sonar98 | POST | api/v1/vehicle-automatization/{vin}/departure/timers | | |
| sonar98 | POST | api/v1/vehicle-automatization/{vin}/departure/timers/settings | | |
| sonar98 | POST | api/v1/vehicle-automatization/{vin}/departure/timers/settings | | |
| sonar98 | GET | api/v1/discover-news | | |
| sonar98 | GET | api/v1/service-partners/{servicePartnerId}/encoded-url | | |
| sonar98 | GET | api/v1/service-partners | | |
Expand Down
2 changes: 2 additions & 0 deletions myskoda/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
set_auto_unlock_plug,
set_charge_limit,
set_departure_timer,
set_minimum_charge_limit,
set_reduced_current_limit,
set_seats_heating,
set_target_temperature,
Expand Down Expand Up @@ -160,6 +161,7 @@ async def disconnect( # noqa: PLR0913
cli.add_command(start_window_heating)
cli.add_command(stop_window_heating)
cli.add_command(set_charge_limit)
cli.add_command(set_minimum_charge_limit)
cli.add_command(set_reduced_current_limit)
cli.add_command(wakeup)
cli.add_command(wait_for_operation)
Expand Down
13 changes: 13 additions & 0 deletions myskoda/cli/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ async def set_charge_limit(ctx: Context, timeout: float, vin: str, limit: int) -
await myskoda.set_charge_limit(vin, limit)


@click.command()
@click.option("timeout", "--timeout", type=float, default=300)
@click.argument("vin")
@click.option("limit", "--limit", type=float, required=True)
@click.pass_context
@mqtt_required
async def set_minimum_charge_limit(ctx: Context, timeout: float, vin: str, limit: int) -> None: # noqa: ASYNC109
"""Set the minimum charge limit in percent."""
myskoda: MySkoda = ctx.obj["myskoda"]
async with asyncio.timeout(timeout):
await myskoda.set_minimum_charge_limit(vin, limit)


@click.command()
@click.option("timeout", "--timeout", type=float, default=300)
@click.argument("vin")
Expand Down
1 change: 1 addition & 0 deletions myskoda/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"charging/update-charging-profiles",
"charging/update-charging-current",
"departure/update-departure-timers",
"departure/update-minimal-soc",
"vehicle-access/honk-and-flash",
"vehicle-access/lock-vehicle",
"vehicle-services-backup/apply-backup",
Expand Down
6 changes: 6 additions & 0 deletions myskoda/myskoda.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ async def set_charge_limit(self, vin: str, limit: int) -> None:
await self.rest_api.set_charge_limit(vin, limit)
await future

async def set_minimum_charge_limit(self, vin: str, limit: int) -> None:
"""Set minimum battery SoC in percent for departure timer."""
future = self._wait_for_operation(OperationName.UPDATE_MINIMAL_SOC)
await self.rest_api.set_minimum_charge_limit(vin, limit)
await future

async def stop_window_heating(self, vin: str) -> None:
"""Stop heating both the front and rear window."""
future = self._wait_for_operation(OperationName.STOP_WINDOW_HEATING)
Expand Down
14 changes: 14 additions & 0 deletions myskoda/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,20 @@ async def set_charge_limit(self, vin: str, limit: int) -> None:
json=json_data,
)

async def set_minimum_charge_limit(self, vin: str, limit: int) -> None:
"""Set minimum battery SoC in percent for departure timer."""
_LOGGER.debug(
"Setting minimum SoC for departure timers for vehicle %s to %r",
vin,
limit,
)

json_data = {"minimumBatteryStateOfChargeInPercent": limit}
await self._make_post_request(
url=f"/v1/vehicle-automatization/{vin}/departure/timers/settings",
json=json_data,
)

# TODO @dvx76: Maybe refactor for FBT001
async def set_battery_care_mode(self, vin: str, enabled: bool) -> None:
"""Enable or disable the battery care mode."""
Expand Down
22 changes: 22 additions & 0 deletions tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,28 @@ async def test_set_charge_limit(
)


@pytest.mark.asyncio
@pytest.mark.parametrize("limit", [0, 20, 30, 50])
async def test_set_minimum_charge_limit(
responses: aioresponses, mqtt_client: MQTTClient, myskoda: MySkoda, limit: int
) -> None:
url = f"{BASE_URL_SKODA}/api/v1/vehicle-automatization/{VIN}/departure/timers/settings"
responses.post(url=url)

future = myskoda.set_minimum_charge_limit(VIN, limit)

topic = f"{USER_ID}/{VIN}/operation-request/departure/update-minimal-soc"
await mqtt_client.publish(topic, create_completed_json("update-minimal-soc"), QOS_2)

await future
responses.assert_called_with(
url=url,
method="POST",
headers={"authorization": f"Bearer {ACCESS_TOKEN}"},
json={"minimumBatteryStateOfChargeInPercent": limit},
)


@pytest.mark.asyncio
@pytest.mark.parametrize(("enabled", "expected"), [(True, "ACTIVATED"), (False, "DEACTIVATED")])
async def test_set_battery_care_mode(
Expand Down

0 comments on commit 374a7c6

Please sign in to comment.