Skip to content

Commit

Permalink
Split models/mqtt.py up into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
Prior99 committed Sep 19, 2024
1 parent 495e55f commit dbd7e9f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 65 deletions.
5 changes: 2 additions & 3 deletions myskoda/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
from asyncclick.core import Context
from termcolor import colored

from myskoda.models.mqtt import OperationName, OperationStatus
from myskoda.myskoda import MySkoda

from .event import Event, EventType, ServiceEventTopic
from .models.charging import MaxChargeCurrent
from .models.common import (
Expand All @@ -26,6 +23,8 @@
OnOffState,
OpenState,
)
from .models.operation_request import OperationName, OperationStatus
from .myskoda import MySkoda


@click.group()
Expand Down
3 changes: 2 additions & 1 deletion myskoda/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from enum import StrEnum
from typing import Literal

from .models.mqtt import OperationRequest, ServiceEvent, ServiceEventCharging
from .models.operation_request import OperationRequest
from .models.service_event import ServiceEvent, ServiceEventCharging


class ServiceEventTopic(StrEnum):
Expand Down
60 changes: 1 addition & 59 deletions myskoda/models/mqtt.py → myskoda/models/operation_request.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Models relaterd to the MQTT API."""
"""Models for operation requests, returned by the MQTT broker."""

from datetime import datetime
from enum import StrEnum
from typing import Generic, TypeVar

from pydantic import BaseModel, Field

Expand Down Expand Up @@ -51,66 +49,10 @@ class OperationName(StrEnum):
WINDOWS_HEATING = "windows-heating"


class ServiceEventName(StrEnum):
CHANGE_SOC = "change-soc"
CHANGE_ACCESS = "change-access"
CHANGE_LIGHTS = "change-lights"
CLIMATISATION_COMPLETED = "climatisation-completed"
CHANGE_REMAINING_TIME = "change-remaining-time"
CHANGE_CHARGE_MODE = "change-charge-mode"


class OperationRequest(BaseModel):
version: int
trace_id: str = Field(None, alias="traceId")
request_id: str = Field(None, alias="requestId")
operation: OperationName
status: OperationStatus
error_code: str = Field(None, alias="errorCode")


class ServiceEventData(BaseModel):
user_id: str = Field(None, alias="userId")
vin: str


T = TypeVar("T", bound=ServiceEventData)


class ServiceEvent(BaseModel, Generic[T]):
version: int
trace_id: str = Field(None, alias="traceId")
timestamp: datetime = Field(None, alias="requestId")
producer: str
name: ServiceEventName
data: T


class ServiceEventChargingState(StrEnum):
CHARGING = "charging"
CHARGED_NOT_CONSERVING = "chargePurposeReachedAndNotConservationCharging"
NOT_READY = "notReadyForCharging"
READY = "readyForCharging"


class ServiceEventChargeMode(StrEnum):
HOME_STORAGE_CHARGING = "homeStorageCharging"
IMMEDIATE_DISCHARGING = "immediateDischarging"
ONLY_OWN_CURRENT = "onlyOwnCurrent"
PREFERRED_CHARGING_TIMES = "preferredChargingTimes"
TIMER_CHARGING_WITH_CLIMATISATION = "timerChargingWithClimatisation"
TIMER = "timer"
MANUAL = "manual"
OFF = "off"


class ServiceEventChargingData(ServiceEventData):
mode: ServiceEventChargeMode
state: ServiceEventChargingState
soc: int
charged_range: int = Field(None, alias="chargedRange")
time_to_finish: int | None = Field(None, alias="timeToFinish")


class ServiceEventCharging(ServiceEvent):
data: ServiceEventChargingData
63 changes: 63 additions & 0 deletions myskoda/models/service_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Models related to service events from the MQTT broker."""

from datetime import datetime
from enum import StrEnum
from typing import Generic, TypeVar

from pydantic import BaseModel, Field


class ServiceEventName(StrEnum):
CHANGE_SOC = "change-soc"
CHANGE_ACCESS = "change-access"
CHANGE_LIGHTS = "change-lights"
CLIMATISATION_COMPLETED = "climatisation-completed"
CHANGE_REMAINING_TIME = "change-remaining-time"
CHANGE_CHARGE_MODE = "change-charge-mode"


class ServiceEventData(BaseModel):
user_id: str = Field(None, alias="userId")
vin: str


T = TypeVar("T", bound=ServiceEventData)


class ServiceEvent(BaseModel, Generic[T]):
version: int
trace_id: str = Field(None, alias="traceId")
timestamp: datetime = Field(None, alias="requestId")
producer: str
name: ServiceEventName
data: T


class ServiceEventChargingState(StrEnum):
CHARGING = "charging"
CHARGED_NOT_CONSERVING = "chargePurposeReachedAndNotConservationCharging"
NOT_READY = "notReadyForCharging"
READY = "readyForCharging"


class ServiceEventChargeMode(StrEnum):
HOME_STORAGE_CHARGING = "homeStorageCharging"
IMMEDIATE_DISCHARGING = "immediateDischarging"
ONLY_OWN_CURRENT = "onlyOwnCurrent"
PREFERRED_CHARGING_TIMES = "preferredChargingTimes"
TIMER_CHARGING_WITH_CLIMATISATION = "timerChargingWithClimatisation"
TIMER = "timer"
MANUAL = "manual"
OFF = "off"


class ServiceEventChargingData(ServiceEventData):
mode: ServiceEventChargeMode
state: ServiceEventChargingState
soc: int
charged_range: int = Field(None, alias="chargedRange")
time_to_finish: int | None = Field(None, alias="timeToFinish")


class ServiceEventCharging(ServiceEvent):
data: ServiceEventChargingData
2 changes: 1 addition & 1 deletion myskoda/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
EventOperation,
EventType,
)
from .models.mqtt import OperationName, OperationRequest, OperationStatus
from .models.operation_request import OperationName, OperationRequest, OperationStatus
from .models.user import User
from .rest_api import RestApi

Expand Down
2 changes: 1 addition & 1 deletion myskoda/myskoda.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from aiohttp import ClientSession

from .event import Event
from .models.mqtt import OperationName
from .models.operation_request import OperationName
from .mqtt import Mqtt
from .rest_api import RestApi

Expand Down

0 comments on commit dbd7e9f

Please sign in to comment.