from datetime import datetime from typing import Any, Literal from pydantic import BaseModel, ConfigDict, Field InverterId = Field(min_length=1, max_length=64, pattern=r"^[A-Za-z0-9_.\-]+$") class ReadingIn(BaseModel): """One inverter's values from one poll cycle. Every parameter is optional — send what the poller could read. Unknown keys are accepted and stored in `extra` (as are keys of an explicit `extra` object), so new parameters can be logged before the backend knows about them. """ model_config = ConfigDict(extra="allow") inverter_id: str = InverterId timestamp: datetime | None = Field( default=None, description="UTC. Defaults to the time the backend receives it." ) ac_voltage_l1_v: float | None = None ac_voltage_l2_v: float | None = None ac_voltage_l3_v: float | None = None dc_voltage_mppt1_v: float | None = None dc_voltage_mppt2_v: float | None = None dc_current_mppt1_a: float | None = None dc_current_mppt2_a: float | None = None ac_current_l1_a: float | None = None ac_current_l2_a: float | None = None ac_current_l3_a: float | None = None ac_power_total_w: int | None = None grid_frequency_hz: float | None = None temperature_c: int | None = None daily_energy_wh: int | None = None total_energy_wh: int | None = None status_code: int | None = Field(default=None, description="Raw inverter state, if known") error_code: int | None = Field(default=None, description="Non-zero = inverter reports a fault") status_text: str | None = Field(default=None, max_length=200) extra: dict[str, Any] | None = None class IngestResult(BaseModel): received: int stored: int duplicates: int clamped_future: int = Field( description="Readings stamped in the future (clock wrong, or local time sent as UTC); " "stored at the time they were received instead" ) class InverterUpdate(BaseModel): name: str | None = Field(default=None, max_length=100) capacity_kwp: float | None = Field(default=None, ge=0, le=1000) mppt_count: int | None = Field(default=None, ge=1, le=4) sort_order: int | None = None notes: str | None = Field(default=None, max_length=2000) class Inverter(BaseModel): id: str name: str capacity_kwp: float | None mppt_count: int sort_order: int notes: str | None first_seen: datetime | None last_seen: datetime | None Severity = Literal["info", "warning", "critical"] State = Literal["ok", "standby", "warning", "fault", "offline", "sleeping", "unknown"] class Issue(BaseModel): code: str severity: Severity message: str params: dict[str, Any] = Field( default_factory=dict, description="Values inside the message, so clients can render it in their own language", ) class Health(BaseModel): online: bool = Field(description="A reading arrived within ONLINE_WINDOW_SECONDS") feeding: bool | None = Field( description="status_code is one of FEEDING_STATUS_CODES; null when no status_code is reported" ) state: State issues: list[Issue] avg_power_w: float | None = Field(description="Mean AC power over the comparison window") relative_output: float | None = Field( description="Output vs fleet median (1.0 = median), per kWp when capacities are set" ) online_window_seconds: int = Field( description="Silence after which this inverter counts as offline: ONLINE_WINDOW_SECONDS, " "stretched for units that report slowly (e.g. cloud-polled every 5 min)" ) class EnergyToday(BaseModel): wh: int | None estimated: bool = Field(description="Integrated from AC power, not the inverter's own counter") class RecentPower(BaseModel): timestamp: datetime ac_power_total_w: int | None class InverterStatus(BaseModel): inverter: Inverter health: Health energy_today: EnergyToday latest: dict[str, Any] | None recent_power: list[RecentPower] = Field( description="Up to the last 5 readings of the past 30 min, newest first — lets clients " "average out a single reading taken while a cloud shaded one array" ) class Totals(BaseModel): ac_power_w: int daily_energy_wh: int | None daily_energy_estimated: bool total_energy_wh: int | None = Field(description="Null until the inverters report a lifetime counter") inverter_count: int online_count: int feeding_count: int problem_count: int class Datalogger(BaseModel): online: bool last_ingest_at: datetime | None class Sun(BaseModel): elevation: float | None daylight: bool | None PowerRange = Literal["30m", "1h", "3h", "6h", "24h", "run", "3d", "7d", "30d"] class ViewSettings(BaseModel): """Dashboard display options. Shared by every viewer (there are no user accounts); unknown keys are ignored and missing ones take the default, so older and newer clients can coexist.""" model_config = ConfigDict(extra="ignore") # Output comparison comparison_readings: Literal[1, 3, 5] = Field( default=1, description="Average the last N readings per inverter (1 = latest only)" ) comparison_scale: Literal["auto", "max", "custom"] = Field( default="auto", description="Bar length: auto = rounded-up ceiling above the highest, " "max = highest inverter is a full bar, custom = comparison_custom_kw is a full bar", ) comparison_custom_kw: float | None = Field(default=None, gt=0, le=1000) comparison_reference: Literal["median", "best", "custom"] = Field( default="median", description="What the percentage is relative to" ) # Fleet power chart power_range: PowerRange = "24h" power_smoothing: Literal["off", "light", "strong"] = "off" # Display voltage_display: Literal["phase", "line"] = Field( default="phase", description="phase = line-to-neutral as sent (~240 V); line = x√3, matches the inverter screen (~415 V)", ) default_language: Literal["en", "el"] = "el" class Summary(BaseModel): generated_at: datetime datalogger: Datalogger sun: Sun totals: Totals fleet_median_w_per_kwp: float | None inverters: list[InverterStatus]