diff --git a/sample2.py b/sample2.py index d38a34c..033a633 100644 --- a/sample2.py +++ b/sample2.py @@ -11,13 +11,6 @@ attach_w42_state(rx_var=w42_state, system_id="79476e53-dea6-44fa-976c-eff6260bae state_as_text = panel.bind(lambda s: f"W42 State:\n\n```\n{json.dumps(s, indent=2)}\n```\n\nReplace this with some awesome visuals", w42_state) -class LoadForecast(BaseModel): - at: datetime = datetime.now() - slots: list[float] = [] - - async def update(self, now: datetime) -> None: - pass - class Output(BaseModel): target_storage: float target_geyser_temperature: float @@ -25,12 +18,21 @@ class Output(BaseModel): grid_sell: list[float] grid_buy: list[float] + +class PvForecast(BaseModel): + at: datetime = datetime.now() + slots: list[float] = [] + +class LoadForecast(BaseModel): + at: datetime = datetime.now() + slots: list[float] = [] + class SystemState(BaseModel): load_forecast: LoadForecast = LoadForecast() + pv_forecast: PvForecast = PvForecast() now: datetime = datetime.now() output: Output | None = None - state: SystemState = panel.rx(lambda s: SystemState.model_validate(s) if s else SystemState())(w42_state) state_pane = panel.pane.Markdown(state_as_text, sizing_mode='stretch_width') @@ -49,12 +51,13 @@ def load_fc_chart(state: SystemState) -> dict: at = state.load_forecast.at storage = state.output.storage if state.output else [0] * len(slots) battery = [storage[ix] - storage[ix-1] if ix > 0 else 0 for ix in range(len(slots))] if state.output else [0] * len(slots) + grid = [state.output.grid_buy[ix] - state.output.grid_sell[ix] if state.output else 0 for ix in range(len(slots))] return { 'title': { 'text': '24hr Forecast' }, 'legend': { - 'data': ['Load Forecast', 'Battery Storage', 'Battery in/out'] + 'data': ['Load Forecast', 'Battery Storage', 'Battery in/out', 'PV Forecast', 'Grid in/out'] }, 'tooltip': { 'trigger': 'axis' @@ -81,7 +84,17 @@ def load_fc_chart(state: SystemState) -> dict: 'data': battery, 'type': 'line', 'color': 'lightblue' - } + }, { + 'name': 'PV Forecast', + 'data': state.pv_forecast.slots, + 'type': 'line', + 'color': 'green' + }, { + 'name': 'Grid in/out', + 'data': grid, + 'type': 'line', + 'color': 'red' + } ] }