Add news and hourly weather to dashboard
This commit is contained in:
parent
320f273c3e
commit
076ee15c83
1 changed files with 28 additions and 3 deletions
31
src/main.py
31
src/main.py
|
|
@ -37,6 +37,7 @@ HA_URL = os.getenv("HA_URL", "https://homeassistant.daddelolymp.de")
|
|||
HA_TOKEN = os.getenv("HA_TOKEN", "")
|
||||
WEATHER_LOCATION = os.getenv("WEATHER_LOCATION", "Leverkusen")
|
||||
WEATHER_LOCATION_SECONDARY = os.getenv("WEATHER_LOCATION_SECONDARY", "Rab,Croatia")
|
||||
DASHBOARD_DATA_PATH = os.getenv("DASHBOARD_DATA_PATH", "/app/data/dashboard_data.json")
|
||||
|
||||
# Caching
|
||||
class Cache:
|
||||
|
|
@ -49,6 +50,7 @@ class Cache:
|
|||
"ha": 30,
|
||||
"vikunja": 30,
|
||||
"system": 10,
|
||||
"briefing_data": 300, # 5 minutes for news/hourly weather
|
||||
}
|
||||
|
||||
def get(self, key: str) -> Optional[Any]:
|
||||
|
|
@ -93,6 +95,7 @@ MAX_CHAT_HISTORY = 50
|
|||
@app.get("/")
|
||||
async def dashboard(request: Request):
|
||||
"""Main dashboard view"""
|
||||
briefing_data = await get_briefing_data()
|
||||
data = {
|
||||
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M"),
|
||||
"weather": await get_weather(),
|
||||
|
|
@ -100,6 +103,8 @@ async def dashboard(request: Request):
|
|||
"ha_status": await get_homeassistant_status(),
|
||||
"vikunja_all": await get_vikunja_all_tasks(),
|
||||
"system_status": await get_system_status(),
|
||||
"news": briefing_data.get("news", []),
|
||||
"hourly_weather": briefing_data.get("hourly_weather", {}),
|
||||
}
|
||||
return templates.TemplateResponse("dashboard.html", {
|
||||
"request": request,
|
||||
|
|
@ -109,12 +114,13 @@ async def dashboard(request: Request):
|
|||
@app.get("/api/all")
|
||||
async def api_all():
|
||||
"""Get all data at once"""
|
||||
weather, weather_secondary, ha, vikunja, system = await asyncio.gather(
|
||||
weather, weather_secondary, ha, vikunja, system, briefing_data = await asyncio.gather(
|
||||
get_weather(),
|
||||
get_weather_secondary(),
|
||||
get_homeassistant_status(),
|
||||
get_vikunja_all_tasks(),
|
||||
get_system_status()
|
||||
get_system_status(),
|
||||
get_briefing_data()
|
||||
)
|
||||
return {
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
|
|
@ -122,9 +128,28 @@ async def api_all():
|
|||
"weather_secondary": weather_secondary,
|
||||
"ha_status": ha,
|
||||
"vikunja_all": vikunja,
|
||||
"system_status": system
|
||||
"system_status": system,
|
||||
"news": briefing_data.get("news", []),
|
||||
"hourly_weather": briefing_data.get("hourly_weather", {})
|
||||
}
|
||||
|
||||
async def get_briefing_data() -> dict:
|
||||
"""Read briefing data (news, hourly weather) from JSON file"""
|
||||
cached = cache.get("briefing_data")
|
||||
if cached:
|
||||
return cached
|
||||
|
||||
try:
|
||||
if os.path.exists(DASHBOARD_DATA_PATH):
|
||||
with open(DASHBOARD_DATA_PATH, "r") as f:
|
||||
data = json.load(f)
|
||||
cache.set("briefing_data", data)
|
||||
return data
|
||||
except Exception as e:
|
||||
print(f"Error reading briefing data: {e}")
|
||||
|
||||
return {"news": [], "hourly_weather": {}}
|
||||
|
||||
@app.websocket("/ws")
|
||||
async def websocket_endpoint(websocket: WebSocket):
|
||||
await manager.connect(websocket)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue