{
  "id": "ha-light-control",
  "name": "Smart Light Control",
  "category": "smart-home",
  "tags": ["smart home", "lights", "ha", "home assistant", "lighting", "automation", "iot"],
  "description": "Control Home Assistant lights. Auto-discovers light.* entities, shows on/off + brightness slider for each. Uses TITAN's HA skill.",
  "triggers": ["smart lights", "control lights", "home lights", "ha lights", "kitchen lights", "bedroom lights", "lighting control", "smart home lights"],
  "defaultSize": { "w": 5, "h": 5 },
  "source": "function HaLightControl() {\n  const [lights, setLights] = React.useState([]);\n  const [error, setError] = React.useState('');\n  const [busy, setBusy] = React.useState({});\n  const refresh = async () => {\n    try {\n      const r = await titan.api.call('/api/skills/ha_states', { method: 'POST', body: { domain: 'light' } });\n      const list = (r?.entities || r || []).filter(e => (e.entity_id||'').startsWith('light.'));\n      setLights(list); setError('');\n    } catch (e) { setError(String(e?.message||e)); }\n  };\n  React.useEffect(() => { refresh(); const id = setInterval(refresh, 10000); return () => clearInterval(id); }, []);\n  const toggle = async (l) => {\n    setBusy(b => ({ ...b, [l.entity_id]: true }));\n    try {\n      await titan.api.call('/api/skills/ha_call', { method: 'POST', body: { domain: 'light', service: l.state==='on'?'turn_off':'turn_on', entity_id: l.entity_id } });\n      await refresh();\n    } catch (e) { setError(String(e?.message||e)); }\n    finally { setBusy(b => ({ ...b, [l.entity_id]: false })); }\n  };\n  const setBrightness = async (l, pct) => {\n    try { await titan.api.call('/api/skills/ha_call', { method: 'POST', body: { domain: 'light', service: 'turn_on', entity_id: l.entity_id, brightness_pct: pct } }); refresh(); } catch (e) { setError(String(e?.message||e)); }\n  };\n  return (\n    <div style={{padding:14,fontFamily:'system-ui',height:'100%',display:'flex',flexDirection:'column'}}>\n      <div style={{display:'flex',justifyContent:'space-between',alignItems:'center',marginBottom:12}}>\n        <div style={{fontSize:16,fontWeight:600}}>💡 Lights ({lights.filter(l=>l.state==='on').length}/{lights.length} on)</div>\n        <button onClick={refresh} style={{padding:'4px 10px',border:'1px solid #ccc',background:'#fff',borderRadius:4,cursor:'pointer',fontSize:12}}>↻</button>\n      </div>\n      {error && <div style={{color:'#FF3B30',fontSize:12,marginBottom:6}}>{error.includes('not configured')?'Configure Home Assistant in Settings → Integrations.':error}</div>}\n      <div style={{flex:1,overflow:'auto',display:'grid',gridTemplateColumns:'repeat(auto-fill,minmax(200px,1fr))',gap:8}}>\n        {lights.map(l => {\n          const on = l.state === 'on';\n          const brightness = l.attributes?.brightness ? Math.round(l.attributes.brightness/255*100) : 0;\n          return (\n            <div key={l.entity_id} style={{padding:10,borderRadius:10,background:on?'linear-gradient(135deg,#FFE082,#FFC107)':'#f5f5f5',transition:'background .3s'}}>\n              <div style={{display:'flex',alignItems:'center',gap:8,marginBottom:8}}>\n                <button onClick={()=>toggle(l)} disabled={busy[l.entity_id]} style={{width:36,height:36,borderRadius:'50%',border:'none',background:on?'#fff':'#ccc',cursor:'pointer',fontSize:18,boxShadow:on?'0 0 12px rgba(255,193,7,0.6)':'none'}}>{on?'💡':'🌑'}</button>\n                <div style={{flex:1,fontSize:12,fontWeight:600}}>{l.attributes?.friendly_name || l.entity_id.replace('light.','')}</div>\n              </div>\n              {on && <input type=\"range\" min=\"1\" max=\"100\" value={brightness} onChange={e=>setBrightness(l, Number(e.target.value))} style={{width:'100%'}}/>}\n            </div>\n          );\n        })}\n        {lights.length === 0 && !error && <div style={{gridColumn:'1/-1',textAlign:'center',padding:30,color:'#888'}}>No lights found.</div>}\n      </div>\n    </div>\n  );\n}\nrender(<HaLightControl/>);",
  "placeholders": []
}
