'use client' import { useState, useEffect, useCallback } from 'react' import Sidebar from '@/components/Sidebar' import TopBar from '@/components/TopBar' import RoomTabs from '@/components/RoomTabs' import DeviceCard from '@/components/DeviceCard' type Tab = 'home' | 'rooms' | 'sensors' | 'settings' interface WeatherData { temp: string desc: string humidity: string windSpeed: string feelsLike: string forecast?: { date: string; maxTemp: string; minTemp: string; desc: string }[] } interface SensorData { temperature: number humidity: number pm25: number } interface HaStates { [key: string]: { state: string; attributes?: Record; _mock?: boolean } } const ROOMS = [ { id: 'living', name: 'Гостиная', emoji: '🛋️', deviceCount: 3 }, { id: 'bedroom', name: 'Спальня', emoji: '🛏️', deviceCount: 2 }, { id: 'kitchen', name: 'Кухня', emoji: '🍳', deviceCount: 0 }, { id: 'bathroom', name: 'Ванная', emoji: '🚿', deviceCount: 0 }, ] const DEVICES_BY_ROOM: Record = { living: [ { id: 'air_purifier', name: 'Очиститель воздуха', icon: '💨', entityId: 'fan.zhimi_rmb1_9528_air_purifier', domain: 'fan', haKey: 'fan.air_purifier', isMock: false, }, { id: 'light_living', name: 'Свет', icon: '💡', entityId: 'light.living_room', domain: 'light', haKey: 'light.living_room', isMock: true, }, { id: 'tv', name: 'Телевизор', icon: '📺', isMock: true, }, ], bedroom: [ { id: 'light_bedroom', name: 'Свет', icon: '💡', entityId: 'light.bedroom', domain: 'light', haKey: 'light.bedroom', isMock: true, }, { id: 'ac', name: 'Кондиционер', icon: '❄️', isMock: true, }, ], kitchen: [], bathroom: [], } export default function HomePage() { const [tab, setTab] = useState('home') const [activeRoom, setActiveRoom] = useState('living') const [weather, setWeather] = useState(null) const [sensors, setSensors] = useState(null) const [haStates, setHaStates] = useState({}) // Load weather useEffect(() => { const load = async () => { try { const r = await fetch('/api/weather') const d = await r.json() if (d.temp && d.temp !== '—') setWeather(d) } catch {} } load() const t = setInterval(load, 600_000) return () => clearInterval(t) }, []) // Load HA states + sensors const loadHA = useCallback(async () => { try { const r = await fetch('/api/ha') const d = await r.json() if (d.states) setHaStates(d.states) if (d.sensors) setSensors(d.sensors) } catch {} }, []) useEffect(() => { loadHA() const t = setInterval(loadHA, 30_000) return () => clearInterval(t) }, [loadHA]) const devicesInRoom = DEVICES_BY_ROOM[activeRoom] || [] const getDeviceState = (haKey?: string): boolean => { if (!haKey || !haStates[haKey]) return false return haStates[haKey].state === 'on' } const getDeviceExtra = (id: string): string | undefined => { if (id === 'air_purifier' && sensors) { return `PM2.5: ${sensors.pm25}` } return undefined } return (
{tab === 'home' && ( <>
{devicesInRoom.length === 0 ? (
🏠 Устройства не добавлены
) : (
{devicesInRoom.map(device => ( ))}
)}
)} {tab === 'rooms' && (
🏠 Управление комнатами Скоро
)} {tab === 'sensors' && sensors && (

Датчики

{[ { label: 'Температура', value: `${sensors.temperature}°C`, icon: '🌡️' }, { label: 'Влажность', value: `${sensors.humidity}%`, icon: '💧' }, { label: 'PM2.5', value: `${sensors.pm25} μg/m³`, icon: '💨' }, ].map(s => (
{s.icon}
{s.value}
{s.label}
))}
)} {tab === 'sensors' && !sensors && (
Загрузка датчиков...
)} {tab === 'settings' && (
⚙️ Настройки Скоро
)}
) }