|
| 1 | +""" |
| 2 | +Report context providing shared data for all report components. |
| 3 | +
|
| 4 | +The ReportContext encapsulates all configuration and results needed by report |
| 5 | +components, avoiding the need to pass many parameters to each component. |
| 6 | +""" |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from dataclasses import dataclass |
| 10 | +from typing import TYPE_CHECKING |
| 11 | + |
| 12 | +import streamlit as st |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from solarbatteryield.models import SimulationConfig, AnalysisResult, ScenarioResult |
| 16 | + |
| 17 | + |
| 18 | +def get_short_place_name() -> str | None: |
| 19 | + """ |
| 20 | + Get a short, readable place name from session state. |
| 21 | +
|
| 22 | + The location name is stored in session state by the sidebar, |
| 23 | + either from geocoding (forward lookup) or reverse geocoding. |
| 24 | +
|
| 25 | + Returns: |
| 26 | + Short place name like "München, Bayern" or None if not available. |
| 27 | + """ |
| 28 | + return st.session_state.get("_location_display_name") |
| 29 | + |
| 30 | + |
| 31 | +@dataclass |
| 32 | +class ReportContext: |
| 33 | + """ |
| 34 | + Shared context for all report components. |
| 35 | + |
| 36 | + Encapsulates configuration and results, providing convenient accessors |
| 37 | + for commonly used values. |
| 38 | + """ |
| 39 | + config: SimulationConfig |
| 40 | + results: AnalysisResult |
| 41 | + |
| 42 | + # ─── Convenience properties ───────────────────────────────────────────────── |
| 43 | + |
| 44 | + @property |
| 45 | + def e_price(self) -> float: |
| 46 | + """Electricity price in EUR/kWh.""" |
| 47 | + return self.config.economics.e_price |
| 48 | + |
| 49 | + @property |
| 50 | + def e_inc(self) -> float: |
| 51 | + """Annual electricity price increase (decimal).""" |
| 52 | + return self.config.economics.e_inc |
| 53 | + |
| 54 | + @property |
| 55 | + def etf_ret(self) -> float: |
| 56 | + """Annual ETF return (decimal).""" |
| 57 | + return self.config.economics.etf_ret |
| 58 | + |
| 59 | + @property |
| 60 | + def feed_in_tariff(self) -> float: |
| 61 | + """Feed-in tariff in EUR/kWh.""" |
| 62 | + return self.config.feed_in_tariff_eur |
| 63 | + |
| 64 | + @property |
| 65 | + def analysis_years(self) -> int: |
| 66 | + """Analysis horizon in years.""" |
| 67 | + return self.config.economics.analysis_years |
| 68 | + |
| 69 | + @property |
| 70 | + def reinvest_savings(self) -> bool: |
| 71 | + """Whether to reinvest savings with ETF return.""" |
| 72 | + return self.config.economics.reinvest_savings |
| 73 | + |
| 74 | + @property |
| 75 | + def total_consumption(self) -> float: |
| 76 | + """Total annual consumption in kWh.""" |
| 77 | + return self.results.total_consumption |
| 78 | + |
| 79 | + @property |
| 80 | + def scenarios(self) -> list[ScenarioResult]: |
| 81 | + """List of scenario results.""" |
| 82 | + return self.results.scenarios |
| 83 | + |
| 84 | + @property |
| 85 | + def pv_generation_total(self) -> float: |
| 86 | + """Total PV generation in kWh/year.""" |
| 87 | + return self.results.pv_generation_total |
| 88 | + |
| 89 | + @property |
| 90 | + def lat(self) -> float | None: |
| 91 | + """Latitude of location.""" |
| 92 | + return self.config.lat |
| 93 | + |
| 94 | + @property |
| 95 | + def lon(self) -> float | None: |
| 96 | + """Longitude of location.""" |
| 97 | + return self.config.lon |
| 98 | + |
| 99 | + @property |
| 100 | + def data_year(self) -> int: |
| 101 | + """PVGIS data year.""" |
| 102 | + return self.config.pv_system.data_year |
| 103 | + |
| 104 | + @property |
| 105 | + def total_peak_kwp(self) -> float: |
| 106 | + """Total PV peak power in kWp.""" |
| 107 | + return self.config.total_peak_kwp |
| 108 | + |
0 commit comments