Skip to content

Commit

Permalink
Fix state for air conditioning
Browse files Browse the repository at this point in the history
Tests added.
  • Loading branch information
fursov committed Sep 27, 2024
1 parent 33db543 commit a916411
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
11 changes: 10 additions & 1 deletion myskoda/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from termcolor import colored

from .event import Event, EventType, ServiceEventTopic
from .models.air_conditioning import AirConditioningState
from .models.charging import MaxChargeCurrent
from .models.common import (
ActiveState,
Expand Down Expand Up @@ -145,7 +146,7 @@ async def air_conditioning(ctx: Context, vin: str) -> None:
f"{ac.target_temperature.temperature_value}°C"
)
print(f"{colored("steering wheel position:", "blue")} {ac.steering_wheel_position}")
print(f"{colored("air conditioning:", "blue")} {on(ac.state)}")
print(f"{colored("air conditioning:", "blue")} {ac_on(ac.state)}")
print(f"{colored("state:", "blue")} {ac.state}")
print(
f"{colored("charger:", "blue")} {connected(ac.charger_connection_state)}, "
Expand Down Expand Up @@ -498,6 +499,14 @@ def on(cond: OnOffState) -> str:
return colored("on", "green") if cond == OnOffState.ON else colored("off", "red")


def ac_on(cond: AirConditioningState) -> str:
return (
colored("on", "green")
if cond in (AirConditioningState.ON, AirConditioningState.HEATING)
else colored("off", "red")
)


def connected(cond: ConnectionState) -> str:
return (
colored("connected", "green")
Expand Down
8 changes: 7 additions & 1 deletion myskoda/models/air_conditioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class TimerMode(StrEnum):
RECURRING = "RECURRING"


class AirConditioningState(StrEnum):
ON = "ON"
OF = "OFF"
HEATING = "HEATING"


@dataclass
class Timer(DataClassORJSONMixin):
enabled: bool
Expand Down Expand Up @@ -68,7 +74,7 @@ class AirConditioning(DataClassORJSONMixin):
seat_heating_activated: SeatHeating = field(
metadata=field_options(alias="seatHeatingActivated")
)
state: OnOffState
state: AirConditioningState
steering_wheel_position: Side = field(metadata=field_options(alias="steeringWheelPosition"))
window_heating_enabled: bool = field(metadata=field_options(alias="windowHeatingEnabled"))
window_heating_state: WindowHeatingState = field(
Expand Down
49 changes: 49 additions & 0 deletions tests/fixtures/enyaq/air-conditioning-heating.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"estimatedDateTimeToReachTargetTemperature": "2024-09-27T03:15:23.655641679Z",
"state": "HEATING",
"runningRequests": [],
"targetTemperature": {
"temperatureValue": 22.0,
"unitInCar": "CELSIUS"
},
"airConditioningAtUnlock": true,
"windowHeatingEnabled": true,
"steeringWheelPosition": "LEFT",
"seatHeatingActivated": {
"frontLeft": false,
"frontRight": false
},
"chargerConnectionState": "CONNECTED",
"chargerLockState": "LOCKED",
"windowHeatingState": {
"front": "OFF",
"rear": "OFF",
"unspecified": "INVALID"
},
"timers": [
{
"id": 1,
"enabled": true,
"time": "05:15",
"type": "RECURRING",
"selectedDays": [
"MONDAY",
"TUESDAY",
"WEDNESDAY",
"THURSDAY",
"FRIDAY"
]
},
{
"id": 2,
"enabled": false,
"time": "00:00",
"type": "ONE_OFF",
"selectedDays": [
"SATURDAY"
]
}
],
"carCapturedTimestamp": "2024-09-27T03:08:00Z",
"errors": []
}
31 changes: 31 additions & 0 deletions tests/test_rest_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Unit tests for myskoda.rest_api."""

import datetime
import json
from pathlib import Path
from unittest.mock import ANY, AsyncMock, MagicMock
Expand Down Expand Up @@ -94,3 +95,33 @@ async def test_get_status(vehicle_statuses: list[str]) -> None:
f"https://mysmob.api.connect.skoda-auto.cz/api/v2/vehicle-status/{target_vin}",
headers=ANY,
)


@pytest.mark.asyncio
async def test_get_air_conditioning() -> None:
"""Example unit test for RestAPI.get_air_conditioning(). Needs more work."""
air_conditioning_status = (FIXTURES_DIR / "enyaq/air-conditioning-heating.json").read_text()
air_conditioning_status_json = json.loads(air_conditioning_status)
response_mock = AsyncMock()
response_mock.text.return_value = air_conditioning_status
session_mock = MagicMock()
session_mock.get.return_value.__aenter__.return_value = response_mock

authorization = Authorization(session_mock)
api = RestApi(session_mock, authorization)
api.authorization.get_access_token = AsyncMock()
target_vin = "TMBJM0CKV1N12345"
get_status_result = await api.get_air_conditioning(target_vin)

assert get_status_result.state == air_conditioning_status_json["state"]
assert (
get_status_result.timers[0].enabled == air_conditioning_status_json["timers"][0]["enabled"]
)
assert get_status_result.timers[0].time == datetime.time.fromisoformat(
air_conditioning_status_json["timers"][0]["time"]
)

session_mock.get.assert_called_with(
f"https://mysmob.api.connect.skoda-auto.cz/api/v2/air-conditioning/{target_vin}",
headers=ANY,
)

0 comments on commit a916411

Please sign in to comment.