-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
191 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,135 @@ | ||
"""Charge-Amps Local API Client""" | ||
|
||
from .base import ChargeAmpsClient | ||
import logging | ||
import uuid | ||
from typing import Self | ||
from urllib.parse import urlparse | ||
|
||
from pydantic import BaseModel, ConfigDict | ||
|
||
class ChargeAmpsLocalClient(ChargeAmpsClient): | ||
pass | ||
from .models import ( | ||
ChargePointConnectorMode, | ||
ChargePointConnectorSettings, | ||
ChargePointType, | ||
) | ||
|
||
DEFAULT_HOST = "localhost" | ||
DEFAULT_PORT = 8080 | ||
|
||
|
||
class LocalChargePoint(BaseModel): | ||
name: str | ||
host: str | ||
port: int | ||
pin: str | ||
|
||
model_config = ConfigDict(frozen=True) | ||
|
||
@classmethod | ||
def from_url(cls, url: str, pin: str, name: str | None = None) -> Self: | ||
res = urlparse(url) | ||
if res.scheme != "ws": | ||
raise ValueError("Only ws URLs supported") | ||
return cls( | ||
name=name or str(uuid.uuid4()), | ||
host=res.hostname or DEFAULT_HOST, | ||
port=res.port or DEFAULT_PORT, | ||
pin=pin, | ||
) | ||
|
||
|
||
class LocalChargePointStatus(BaseModel): | ||
chargepoint: LocalChargePoint | ||
type: ChargePointType | None = None | ||
charge_point_id: str | None = None | ||
connector_settings: list[ChargePointConnectorSettings] | None = None | ||
dimmer: int = 0 | ||
light: bool = False | ||
|
||
@staticmethod | ||
def get_default_halo_connectors() -> list[ChargePointConnectorSettings]: | ||
return [ | ||
ChargePointConnectorSettings( | ||
charge_point_id="", | ||
connector_id=0, | ||
mode=str(ChargePointConnectorMode.OFF), | ||
rfid_lock=False, | ||
cable_lock=False, | ||
max_current=None, | ||
), | ||
ChargePointConnectorSettings( | ||
charge_point_id="", | ||
connector_id=1, | ||
mode=str(ChargePointConnectorMode.OFF), | ||
rfid_lock=False, | ||
cable_lock=False, | ||
max_current=None, | ||
), | ||
] | ||
|
||
@staticmethod | ||
def get_default_aura_connectors() -> list[ChargePointConnectorSettings]: | ||
return [ | ||
ChargePointConnectorSettings( | ||
charge_point_id="", | ||
connector_id=0, | ||
mode=str(ChargePointConnectorMode.OFF), | ||
rfid_lock=False, | ||
cable_lock=False, | ||
max_current=None, | ||
), | ||
ChargePointConnectorSettings( | ||
charge_point_id="", | ||
connector_id=1, | ||
mode=str(ChargePointConnectorMode.OFF), | ||
rfid_lock=False, | ||
cable_lock=False, | ||
max_current=None, | ||
), | ||
] | ||
|
||
def update_settings(self, message: str) -> None: | ||
parameters = [int(x) for x in message.split(",")] | ||
|
||
match parameters[0]: | ||
case 1: | ||
self.type = ChargePointType.HALO | ||
if self.connector_settings is None: | ||
self.connector_settings = self.get_default_halo_connectors() | ||
assert len(parameters) == 8 | ||
self.connector_settings[0].mode = "On" if parameters[1] else "Off" | ||
self.connector_settings[1].mode = "On" if parameters[2] else "Off" | ||
self.connector_settings[0].rfid_lock = bool(parameters[3]) | ||
self.light = bool(parameters[4]) | ||
self.connector_settings[0].max_current = parameters[5] // 10 | ||
self.dimmer = bool(parameters[6]) | ||
assert parameters[7] == 4 | ||
|
||
case 101: | ||
self.type = ChargePointType.AURA | ||
if self.connector_settings is None: | ||
self.connector_settings = self.get_default_aura_connectors() | ||
assert len(parameters) == 12 | ||
self.connector_settings[0].mode = "On" if parameters[1] else "Off" | ||
self.connector_settings[0].cable_lock = bool(parameters[2]) | ||
self.connector_settings[0].rfid_lock = bool(parameters[3]) | ||
self.connector_settings[0].max_current = parameters[4] // 10 | ||
assert parameters[5] == 4 | ||
self.connector_settings[1].mode = "On" if parameters[6] else "Off" | ||
self.connector_settings[1].cable_lock = bool(parameters[7]) | ||
self.connector_settings[1].rfid_lock = bool(parameters[8]) | ||
self.connector_settings[1].max_current = parameters[9] // 10 | ||
assert parameters[10] == 4 | ||
self.dimmer = parameters[11] | ||
case _: | ||
logging.warning("Unknown settings preamble: %d", parameters[0]) | ||
|
||
def update_status(self, message: str) -> None: | ||
pass | ||
|
||
|
||
class ChargeAmpsLocalClient: # (ChargeAmpsClient): | ||
def __init__(self, chargepoints: list[LocalChargePoint]) -> None: | ||
self.chargepoints: list[LocalChargePointStatus] = [ | ||
LocalChargePointStatus(chargepoint=cp) for cp in chargepoints | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters