Skip to content

Commit

Permalink
Add support for mqtt departure-error-plug message (#348)
Browse files Browse the repository at this point in the history
* Add support for mqtt departure-error-plug message
  • Loading branch information
Giermann authored Feb 6, 2025
1 parent 373505a commit 3299421
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 15 deletions.
4 changes: 0 additions & 4 deletions myskoda/models/charging.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ class PlugUnlockMode(StrEnum):
OFF = "OFF"


class ServiceEventChargingError(StrEnum):
STOPPED_DEVICE = "STOPPED_DEVICE"


@dataclass
class Settings(DataClassORJSONMixin):
available_charge_modes: list[ChargeMode] = field(
Expand Down
25 changes: 21 additions & 4 deletions myskoda/models/service_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mashumaro import field_options
from mashumaro.mixins.orjson import DataClassORJSONMixin

from .charging import ChargeMode, ChargingState, ServiceEventChargingError
from .charging import ChargeMode, ChargingState


class ServiceEventName(StrEnum):
Expand All @@ -25,6 +25,12 @@ class ServiceEventName(StrEnum):
CLIMATISATION_COMPLETED = "climatisation-completed"
DEPARTURE_READY = "departure-ready"
DEPARTURE_STATUS_CHANGED = "departure-status-changed"
DEPARTURE_ERROR_PLUG = "departure-error-plug"


class ServiceEventError(StrEnum):
STOPPED_DEVICE = "STOPPED_DEVICE"
CLIMA = "CLIMA"


@dataclass
Expand Down Expand Up @@ -115,16 +121,27 @@ class ServiceEventChargingData(ServiceEventData):
default=None,
metadata=field_options(alias="timeToFinish", deserialize=_deserialize_time_to_finish),
)
error_code: ServiceEventChargingError | None = field(
default=None, metadata=field_options(alias="errorCode")
)


@dataclass
class ServiceEventWithChargingData(ServiceEvent):
data: ServiceEventChargingData


@dataclass
class ServiceEventErrorData(ServiceEventData):
"""Error code inside charging or departure service."""

error_code: ServiceEventError | None = field(
default=None, metadata=field_options(alias="errorCode")
)


@dataclass
class ServiceEventWithErrorData(ServiceEvent):
data: ServiceEventErrorData


class UnexpectedChargeModeError(Exception):
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"producer": "SKODA_MHUB",
"name": "charging-error",
"data": {
"errorCode": "STOPPED_DEVICE",
"errorCode": "STOPPED_DEVICE",
"userId": "ad0d7945-4814-43d0-801f-charging-error",
"vin": "TMBAXXXXXXXXXXXXX"
}
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/events/service_event_departure_error_plug.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"traceId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"timestamp": "2024-11-15T13:14:41Z",
"producer": "SKODA_MHUB",
"name": "departure-error-plug",
"data": {
"errorCode": "CLIMA",
"userId": "ad0d7945-4814-43d0-801f-departure-error-plug",
"vin": "TMBAXXXXXXXXXXXXX"
}
}
1 change: 0 additions & 1 deletion tests/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ def on_event(event: Event) -> None:
soc=None,
charged_range=None,
time_to_finish=None,
error_code=None,
),
),
),
Expand Down
27 changes: 22 additions & 5 deletions tests/test_service_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
ChargingState,
ServiceEvent,
ServiceEventChargingData,
ServiceEventChargingError,
ServiceEventError,
ServiceEventErrorData,
ServiceEventData,
ServiceEventName,
ServiceEventWithChargingData,
ServiceEventWithErrorData,
)

FIXTURES_DIR = Path(__file__).parent.joinpath("fixtures")
Expand All @@ -32,6 +34,7 @@ def load_service_events() -> list[str]:
"events/service_event_charging_charging_error.json",
"events/service_event_departure_ready.json",
"events/service_event_departure_status_changed.json",
"events/service_event_departure_error_plug.json",
]:
json_file = FIXTURES_DIR / path
service_events.append(json_file.read_text())
Expand All @@ -45,7 +48,6 @@ def test_parse_service_events(service_events: list[str]) -> None:
if event.name in [
ServiceEventName.CHANGE_SOC,
ServiceEventName.CHARGING_STATUS_CHANGED,
ServiceEventName.CHARGING_ERROR,
]:
try:
event = ServiceEventWithChargingData.from_json(service_event)
Expand All @@ -67,11 +69,26 @@ def test_parse_service_events(service_events: list[str]) -> None:
user_id=f"ad0d7945-4814-43d0-801f-{event.name.value}",
vin="TMBAXXXXXXXXXXXXX",
)
elif event.name == ServiceEventName.CHARGING_ERROR:
assert event.data == ServiceEventChargingData(
elif event.name in [
ServiceEventName.CHARGING_ERROR,
ServiceEventName.DEPARTURE_ERROR_PLUG,
]:
try:
event = ServiceEventWithErrorData.from_json(service_event)
except ValueError:
event = ServiceEvent.from_json(service_event)

if event.name == ServiceEventName.CHARGING_ERROR:
assert event.data == ServiceEventErrorData(
user_id=f"ad0d7945-4814-43d0-801f-{event.name.value}",
vin="TMBAXXXXXXXXXXXXX",
error_code=ServiceEventError.STOPPED_DEVICE,
)
elif event.name == ServiceEventName.DEPARTURE_ERROR_PLUG:
assert event.data == ServiceEventErrorData(
user_id=f"ad0d7945-4814-43d0-801f-{event.name.value}",
vin="TMBAXXXXXXXXXXXXX",
error_code=ServiceEventChargingError.STOPPED_DEVICE,
error_code=ServiceEventError.CLIMA,
)
else:
assert event.data == ServiceEventData(
Expand Down

0 comments on commit 3299421

Please sign in to comment.