-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfork_u-house_card.js
More file actions
775 lines (676 loc) · 36.9 KB
/
fork_u-house_card.js
File metadata and controls
775 lines (676 loc) · 36.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/**
* Fork_U-House_Card v12.0 (AI Storyteller Edition)
* * FEATURE: Long, descriptive, "AI-like" status messages with context & reasoning.
* * FEATURE: Pollen support restored & integrated into advice logic.
* * FEATURE: Wind Chill logic (Wind + Cold temp = specific advice).
* * VISUALS: Prism Classic (Stars, Fog, No-Glow Rain) + Gaming Ambient Mode.
*/
const TRANSLATIONS = {
en: {
loading: "Analyzing environmental data...",
home_median: "Home",
// Conditions
clear_night: "Clear Night", cloudy: "Cloudy", fog: "Fog", hail: "Hail",
lightning: "Thunderstorm", lightning_rainy: "Thunderstorm & Rain",
partlycloudy: "Partly Cloudy", pouring: "Pouring Rain", rainy: "Rainy",
snowy: "Snowy", sunny: "Sunny", windy: "Windy",
// --- AI NARRATIVES ---
// 1. DANGER / STORM
alert_storm: "⚠️ CRITICAL ALERT: A storm with lightning is active nearby. Strong winds and heavy rain are expected. Please secure loose objects outside and stay indoors for safety.",
// 2. HEALTH (AQI / POLLEN)
alert_aqi_bad: "😷 SMOG ALERT: Air quality is critical (PM2.5: {val}). Prolonged exposure is dangerous. Keep windows closed and run your air purifier.",
alert_aqi_mod: "😶 AIR QUALITY WARNING: PM2.5 levels are elevated ({val}). Sensitive groups should limit outdoor exertion today.",
alert_pollen: "🤧 ALLERGY ALERT: High pollen concentration detected. If you suffer from allergies, keep windows shut and have your medication ready.",
// 3. FORECAST (FUTURE RAIN/SNOW)
advice_rain_soon: "☂️ PLAN AHEAD: Rain is approaching and expected around {time} (approx. {val} mm). Don't leave without an umbrella.",
advice_snow_soon: "❄️ WINTER ALERT: Snowfall is expected around {time}. Road conditions may deteriorate rapidly. Drive with caution.",
// 4. CURRENT WEATHER
advice_rain_now: "🌧️ CURRENTLY RAINING: Intensity is {val} mm/h. Wet surfaces and reduced visibility. Drive safely and wear waterproof gear.",
advice_snow_now: "🌨️ SNOWING: Snow is falling right now. Enjoy the view, but dress warmly if you head out.",
// 5. UV / SUN
alert_uv_high: "☀️ HIGH UV RADIATION: The UV Index is {val}. Unprotected skin can burn quickly. Use sunscreen and wear sunglasses if you go out.",
// 6. TEMPERATURE + WIND (Wind Chill)
advice_cold_wind: "🥶 WIND CHILL WARNING: It's {val}°C, but the strong wind makes it feel much colder. Wear windproof layers and a hat.",
advice_cold: "🧣 COLD WEATHER: Outside temperature is {val}°C. It's chilly—make sure to zip up your jacket and keep warm.",
advice_hot: "🔥 HEAT ADVISORY: Temperatures have reached {val}°C. Avoid strenuous activity in direct sunlight and drink plenty of water.",
advice_nice: "😎 COMFORTABLE CONDITIONS: Weather is stable at {val}°C with moderate wind. Great time for a walk or airing out the house.",
advice_gaming: "🎮 GAMING MODE: Immersive lighting active. Notifications silenced.",
},
pl: {
loading: "Analizuję dane środowiskowe...",
home_median: "Dom",
// Warunki
clear_night: "Bezchmurnie", cloudy: "Pochmurno", fog: "Mgła", hail: "Grad",
lightning: "Burza", lightning_rainy: "Burza z deszczem",
partlycloudy: "Częściowe zachm.", pouring: "Ulewa", rainy: "Deszcz",
snowy: "Śnieg", sunny: "Słonecznie", windy: "Wietrznie",
// --- AI NARRACJA ---
// 1. ZAGROŻENIE
alert_storm: "<span class='value-pill pill-1'>⚠️ <b>OSTRZEŻENIE KRYTYCZNE</b></span> W pobliżu wykryto burzę. Spodziewaj się wyładowań i silnego wiatru. Zabezpiecz ogród i pozostań w domu.",
// 2. ZDROWIE (SMOG / PYŁKI)
alert_aqi_bad: "<span class='value-pill pill-1'>😷 <b>ALARM SMOGOWY</b></span> Jakość powietrza jest fatalna <span class='value-pill'>PM2.5: <b>{val}</b></span>. Wyjście na zewnątrz grozi problemami oddechowymi. Zamknij okna i włącz oczyszczacz.",
alert_aqi_mod: "<span class='value-pill pill-1'>😶 <b>OSTRZEŻENIE</b></span> Podwyższone stężenie pyłów <span class='value-pill'>PM2.5: <b>{val}</b></span>. Jakość powietrza jest przeciętna. Osoby wrażliwe powinny unikać wysiłku na zewnątrz.",
alert_pollen: "<span class='value-pill pill-1'>🤧 <b>ALARM DLA ALERGIKÓW</b></span> Wykryto bardzo wysokie stężenie pyłków. Przygotuj leki przeciwhistaminowe i unikaj wietrzenia sypialni.",
// 3. PROGNOZA (NADCHODZĄCE)
advice_rain_soon: "<span class='value-pill pill-1'>☂️ <b>WEŹ PARASOL</b></span> Nadciągają opady deszczu. Spodziewaj się ich ok. godziny <span class='value-pill'><b>{time}</b></span>. Prognozowane <span class='value-pill'><b>{val}</b> mm</span>",
advice_snow_soon: "<span class='value-pill pill-1'>❄️ <b>ZACHOWAJ OSTROŻNOŚĆ</b></span> Ok. godziny <span class='value-pill'><b>{time}</b></span> zacznie padać śnieg. Warunki drogowe mogą się gwałtownie pogorszyć.",
// 4. AKTUALNA POGODA
advice_rain_now: "<span class='value-pill pill-1'>🌧️ <b>DESZCZ</b></span> Aktualny opad to <span class='value-pill'><b>{val}</b> mm</span>. Jest mokro i ślisko. Jeśli musisz wyjść, koniecznie weź kurtkę przeciwdeszczową.",
advice_snow_now: "<span class='value-pill pill-1'>🌨️ <b>ŚNIEG</b></span> Na zewnątrz sypie śnieg. Jest <span class='value-pill'><b>malowniczo</b></span>, ale pamiętaj o ciepłym ubraniu i czapce.",
// 5. UV
alert_uv_high: "<span class='value-pill pill-1'>☀️ <b>PROMIENIOWANIE</b></span> Indeks UV wynosi <span class='value-pill'><b>{val}</b></span>. Skóra może ulec poparzeniu. Koniecznie użyj kremu z filtrem i okularów przeciwsłonecznych.",
// 6. TEMPERATURA + WIATR
advice_cold_wind: "<span class='value-pill pill-1'>🥶 <b>WIATR</b></span> Jest <span class='value-pill'><b>{val}</b> °C</span>, ale silny wiatr sprawia, że temperatura odczuwalna jest znacznie niższa. Ubierz się „na cebulkę” i chroń uszy.",
advice_cold: "<span class='value-pill pill-1'>🧣 <b>ZIMNO</b></span> Temperatura wynosi <span class='value-pill'><b>{val}</b> °C</span>. Ubierz ciepłą kurtkę przed wyjściem. Warto sprawdzić szczelność okien.",
advice_hot: "<span class='value-pill pill-1'>🔥 <b>GORĄC</b></span> Temperatura osiągnęła <span class='value-pill'><b>{val}</b> °C</span>. Unikaj słońca w godzinach szczytu, pij dużo wody i zasłoń rolety.",
advice_nice: "😎 Pogoda jest stabilna, temperatura przyjemna <span class='value-pill'><b>{val}</b> °C</span>. To <span class='value-pill'>idealny</span> moment na spacer lub przewietrzenie mieszkania.",
advice_gaming: "<span class='value-pill pill-1'>🎮 <b>TRYB IMERSYJNY</b></span> Tryb kina lub gry aktywny. Sterowanie <span class='value-pill'><b>AmbiLight</b></span> włączone.",
}
};
class ForkUHouseCard extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this._hass = null;
this._config = {};
this._animationFrame = null;
this._canvas = null;
this._ctx = null;
this._resizeObserver = null;
// Visuals
this._particles = [];
this._clouds = [];
this._stars = [];
this._fogParticles = [];
// Lightning
this._lightningTimer = 0;
this._flashOpacity = 0;
this._lightningBolt = null;
}
static getStubConfig() {
return {
language: "pl",
image: "/local/community/fork_u-house_card/images/",
// Entities
weather_entity: "weather.forecast_home",
season_entity: "sensor.season",
sun_entity: "sun.sun",
cloud_coverage_entity: "sensor.openweathermap_cloud_coverage",
party_mode_entity: "input_boolean.gaming_mode", // enables gaming ambient
// AI Sensors
aqi_entity: "sensor.waqi_pm2_5",
pollen_entity: "sensor.pollen_level", // Returns: 'High', 'Moderate', or number
uv_entity: "sensor.uv_index",
wind_speed_entity: "sensor.wind_speed",
wind_direction_entity: "sensor.wind_bearing",
rooms: [{ name: "Salon", entity: "sensor.salon_temp", x: 50, y: 50, weight: 1 }]
};
}
setConfig(config) {
if (!config.rooms || !Array.isArray(config.rooms)) throw new Error("Missing 'rooms' list.");
this._config = config;
this._lang = config.language || 'en';
this._render();
}
set hass(hass) {
this._hass = hass;
this._updateData();
}
_t(key, repl = {}) {
let txt = TRANSLATIONS[this._lang]?.[key] || TRANSLATIONS['en'][key] || key;
Object.keys(repl).forEach(k => { txt = txt.replace(`{${k}}`, repl[k]); });
return txt;
}
connectedCallback() {
if (this.shadowRoot && !this._resizeObserver) {
const card = this.shadowRoot.querySelector('.card');
if (card) {
this._resizeObserver = new ResizeObserver(() => this._resizeCanvas());
this._resizeObserver.observe(card);
}
}
}
disconnectedCallback() {
if (this._resizeObserver) this._resizeObserver.disconnect();
if (this._animationFrame) cancelAnimationFrame(this._animationFrame);
}
// --- NOWA LOGIKA WYBORU OBRAZKA ---
_calculateImage() {
const path = this._config.image_path || "/local/community/fork_u-house_card/images/";
// 1. Pora Dnia
const sunState = this._hass.states[this._config.sun_entity || 'sun.sun']?.state || 'above_horizon';
const timeOfDay = sunState === 'below_horizon' ? 'night' : 'day';
// 2. Święta (Xmas Priority)
const now = new Date();
const month = now.getMonth() + 1;
const day = now.getDate();
if ((month === 12 && day >= 14) || (month === 1 && day <= 14)) {
return `${path}winter_xmas_${timeOfDay}.png`;
}
// 3. Sezon
let season = this._hass.states[this._config.season_entity]?.state || 'summer';
const seasonMap = { 'wiosna': 'spring', 'lato': 'summer', 'jesień': 'autumn', 'zima': 'winter' };
if (seasonMap[season]) season = seasonMap[season];
season = season.toLowerCase();
// 4. Ścisłe Mapowanie Pogody (Strict Mapping)
const wStateRaw = this._hass.states[this._config.weather_entity]?.state;
let weatherSuffix = null;
if (wStateRaw) {
const s = wStateRaw.toLowerCase();
// Tłumaczenie stanów HA na Twoje nazwy plików
if (['lightning', 'lightning-rainy'].includes(s)) {
weatherSuffix = 'lightning';
} else if (['rainy', 'pouring'].includes(s)) {
weatherSuffix = 'rainy';
} else if (['snowy', 'snowy-rainy'].includes(s)) {
weatherSuffix = 'snowy';
} else if (s === 'hail') {
weatherSuffix = 'hail';
} else if (s === 'fog') {
weatherSuffix = 'fog';
}
// Sunny, cloudy, partlycloudy -> weatherSuffix pozostaje null (czyli fallback do season_day.png)
}
// 5. Sprawdzenie Boolean w Configu
if (weatherSuffix) {
// Klucz np.: img_winter_day_rainy
const configKey = `img_${season}_${timeOfDay}_${weatherSuffix}`;
const configKey_alt = `img_${season}_${weatherSuffix}_${timeOfDay}`;
// Jeśli w YAML jest: img_winter_day_rainy: true
if (this._config[configKey] === true || this._config[configKey_alt] === true) {
return `${path}${season}_${weatherSuffix}_${timeOfDay}.png`;
}
}
// 6. Fallback (Neutralny)
return `${path}${season}_${timeOfDay}.png`;
}
// --- DATA LOGIC ---
_updateData() {
if (!this._hass || !this.shadowRoot.querySelector('.card')) return;
// --- AKTUALIZACJA TŁA (DYNAMICZNA) ---
const newImage = this._calculateImage();
// Sprawdzamy czy obrazek się zmienił, żeby nie mrugało
if (this._currentImageUrl !== newImage) {
this._currentImageUrl = newImage;
const bgEl = this.shadowRoot.querySelector('.bg-image');
if (bgEl) {
// Preload obrazka
const img = new Image();
img.onload = () => { bgEl.style.backgroundImage = `url('${newImage}')`; };
img.src = newImage;
}
}
// Rooms & Median
const roomsData = this._config.rooms.map(r => {
const s = this._hass.states[r.entity];
const v = s ? parseFloat(s.state) : null;
return { ...r, value: v, valid: !isNaN(v) };
});
const weighted = roomsData.filter(r => r.valid && (r.weight === undefined || r.weight > 0)).map(r => r.value).sort((a,b)=>a-b);
let median = 0;
if (weighted.length > 0) {
const mid = Math.floor(weighted.length/2);
median = weighted.length % 2 !== 0 ? weighted[mid] : (weighted[mid-1]+weighted[mid])/2;
}
// Updates
this._updateBadges(roomsData);
this._handleGamingMode();
this._handleDayNight();
this._generateAIStatus(median);
// Animation Loop
if (!this._animationFrame && this._canvas) {
this._initStars();
this._animate();
}
}
_updateBadges(rooms) {
const container = this.shadowRoot.querySelector('.badges-layer');
if (!container) return;
container.innerHTML = rooms.map(room => {
if (!room.valid) return '';
const top = room.y ?? 50; const left = room.x ?? 50;
const colorClass = this._getTempColorClass(room.value);
return `
<div class="badge ${colorClass}" style="top: ${top}%; left: ${left}%;">
<div class="badge-dot"></div>
<div class="badge-content">
<span class="badge-name">${room.name}</span>
<span class="badge-val">${room.value.toFixed(1)}°</span>
</div>
</div>`;
}).join('');
}
_getTempColorClass(t) {
if (t < 19) return 'is-cold'; if (t < 23) return 'is-optimal'; if (t < 25) return 'is-warm'; return 'is-hot';
}
_handleGamingMode() {
const partyEntity = this._config.party_mode_entity;
const isGaming = partyEntity && this._hass.states[partyEntity]?.state === 'on';
const card = this.shadowRoot.querySelector('.card');
if (card) {
isGaming ? card.classList.add('gaming-active') : card.classList.remove('gaming-active');
}
return isGaming;
}
_handleDayNight() {
const sunEnt = this._config.sun_entity || 'sun.sun';
const isNight = this._hass.states[sunEnt]?.state === 'below_horizon';
const dimLayer = this.shadowRoot.querySelector('.dim-layer');
if (dimLayer) dimLayer.style.opacity = isNight ? '0.1' : '0';
return isNight;
}
// --- AI STATUS LOGIC (Detailed & Explained) ---
_generateAIStatus(median) {
const wObj = this._hass.states[this._config.weather_entity];
if (!wObj) return;
const condition = this._config.test_weather_state || wObj.state;
const temp = wObj.attributes.temperature;
const forecast = wObj.attributes.forecast || [];
// Sensory
const aqiVal = this._getStateVal(this._config.aqi_entity);
const uvVal = this._getStateVal(this._config.uv_entity);
const { speed: windSpeed } = this._getWindData();
// Pollen Logic
let isHighPollen = false;
if (this._config.pollen_entity) {
const pState = this._hass.states[this._config.pollen_entity]?.state;
if (pState) {
// Obsługa tekstowa (high) lub liczbowa (>50)
if (['high', 'very_high', 'extreme', 'red'].includes(pState.toLowerCase())) isHighPollen = true;
if (!isNaN(parseFloat(pState)) && parseFloat(pState) > 50) isHighPollen = true;
}
}
let msg = "";
let level = "normal";
// Check for Gaming Mode
const isGaming = this._handleGamingMode();
// --- HIERARCHIA WAŻNOŚCI ---
// 1. ZAGROŻENIE ŻYCIA (Burze)
if (['lightning', 'lightning-rainy', 'hail'].includes(condition)) {
msg = this._t('alert_storm');
level = "danger";
}
// 2. ZDROWIE: SMOG
else if (aqiVal !== null && aqiVal > 50) {
if (aqiVal > 100) {
msg = this._t('alert_aqi_bad', {val: aqiVal});
level = "danger";
} else {
msg = this._t('alert_aqi_mod', {val: aqiVal});
level = "warn";
}
}
// 3. ZDROWIE: PYŁKI
else if (isHighPollen) {
msg = this._t('alert_pollen');
level = "warn";
}
// 4. PLANOWANIE: NADCHODZĄCY DESZCZ/ŚNIEG
else {
const nextRain = forecast.slice(0, 3).find(f => ['rainy', 'pouring', 'snowy'].includes(f.condition) || (f.precipitation > 0));
// Jeśli ma padać w ciągu 3h
if (nextRain) {
const time = new Date(nextRain.datetime).getHours() + ":00";
const p = nextRain.precipitation || "~";
msg = nextRain.condition === 'snowy'
? this._t('advice_snow_soon', {time})
: this._t('advice_rain_soon', {time, val: p});
level = "warn";
}
// 5. BIEŻĄCE WARUNKI
else if (['rainy', 'pouring'].includes(condition)) {
msg = this._t('advice_rain_now', {val: wObj.attributes.precipitation || "~"});
level = "warn";
}
else if (['snowy', 'snowy-rainy'].includes(condition)) {
msg = this._t('advice_snow_now');
level = "warn";
}
// 6. UV (LATO)
else if (uvVal !== null && uvVal > 6) {
msg = this._t('alert_uv_high', {val: uvVal});
level = "warn";
}
// 7. TEMPERATURA + WIATR (ZIMA)
else if (temp < 10 && windSpeed > 20) {
// Jest zimno i wieje - Wind Chill
msg = this._t('advice_cold_wind', {val: temp});
}
else if (temp < 5) {
msg = this._t('advice_cold', {val: temp});
} else if (temp > 28) {
msg = this._t('advice_hot', {val: temp});
level = "warn";
}
// 8. STABILNIE
else {
msg = this._t('advice_nice', {val: temp});
}
}
// Append Gaming status
if (isGaming && level === 'normal') {
msg = this._t('advice_gaming');
}
const medianEl = this.shadowRoot.querySelector('.median-pill');
const statusEl = this.shadowRoot.querySelector('.footer-content');
const footer = this.shadowRoot.querySelector('.footer');
if (medianEl) medianEl.innerHTML = `${this._t('home_median')}: <b>${median.toFixed(1)}°C</b>`;
if (statusEl) statusEl.innerHTML = msg;
if (footer) footer.setAttribute('data-status', level);
}
_getStateVal(id) {
if (!id || !this._hass.states[id]) return null;
const v = parseFloat(this._hass.states[id].state);
return isNaN(v) ? null : v;
}
_getWindData() {
let speed = 10, bearing = 270;
if(this._config.wind_speed_entity && this._hass.states[this._config.wind_speed_entity])
speed = parseFloat(this._hass.states[this._config.wind_speed_entity].state);
else if(this._hass.states[this._config.weather_entity]?.attributes?.wind_speed)
speed = parseFloat(this._hass.states[this._config.weather_entity].attributes.wind_speed);
if(this._config.wind_direction_entity && this._hass.states[this._config.wind_direction_entity])
bearing = parseFloat(this._hass.states[this._config.wind_direction_entity].state);
else if(this._hass.states[this._config.weather_entity]?.attributes?.wind_bearing)
bearing = parseFloat(this._hass.states[this._config.weather_entity].attributes.wind_bearing);
return { speed: isNaN(speed)?5:speed, bearing: isNaN(bearing)?270:bearing };
}
_getCloudCoverage() {
const cloudEnt = this._config.cloud_coverage_entity;
if (cloudEnt && this._hass.states[cloudEnt]) {
const val = parseFloat(this._hass.states[cloudEnt].state);
return isNaN(val) ? 0 : val;
}
return 0;
}
// --- RENDER (Prism Classic + Gaming Ambient) ---
_render() {
this.shadowRoot.innerHTML = `
<style>
:host { display: block; --fork-u-bg: #1e2024; --color-cold: #60A5FA; --color-opt: #34D399; --color-warm: #FBBF24; --color-hot: #F87171; }
.card {
position: relative; display: flex; flex-direction: column; width: 100%; height: 350px;
overflow: hidden;
text-shadow: rgba(0,0,0,0.4) 0 1px 0px;
box-shadow: 0 4px 2px rgba(0,0,0,0.3);
/* Please style borders and box shadow manually */
/*
background: var(--fork-u-bg);
border-radius: 20px;
font-family: 'Roboto', sans-serif;
border: 1px solid rgba(255,255,255,0.1);
*/
background: var(--card-background-color,var(--fork-u-bg));
border-radius: var(--ha-card-border-radius,var(--ha-border-radius-lg,20px));
}
.gradient-layer {
background: linear-gradient(to bottom, rgba(0,0,0,0.2) 0%, transparent 40px);
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
background-size: cover; background-position: center;
z-index: 0; transition: all 0.5s ease;
}
.bg-image {
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
background-size: cover; background-position: center;
z-index: 0; transition: all 0.5s ease;
}
.dim-layer {
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
background: #000; opacity: 0; z-index: 1; pointer-events: none; transition: opacity 2s ease;
}
/* GAMING AMBIENT LAYER */
.ambient-layer {
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
z-index: 2; pointer-events: none; opacity: 0; transition: opacity 1.5s ease;
}
.card.gaming-active .ambient-layer { opacity: 1; }
.ambient-light {
position: absolute; border-radius: 50%; filter: blur(70px);
mix-blend-mode: color-dodge; animation-iteration-count: infinite; animation-timing-function: ease-in-out;
}
.blob-1 { top: 20%; left: 10%; width: 300px; height: 300px; background: radial-gradient(circle, rgba(120,50,255,0.8) 0%, rgba(0,0,0,0) 70%); animation: float-1 6s infinite alternate; }
.blob-2 { bottom: 10%; right: 10%; width: 350px; height: 350px; background: radial-gradient(circle, rgba(255,0,150,0.7) 0%, rgba(0,0,0,0) 70%); animation: float-2 7s infinite alternate; }
.blob-3 { top: 40%; left: 40%; width: 250px; height: 250px; background: radial-gradient(circle, rgba(0,255,255,0.5) 0%, rgba(0,0,0,0) 70%); animation: pulse-3 5s infinite; mix-blend-mode: overlay; }
@keyframes float-1 { 0% { transform: translate(0,0) scale(1); opacity: 0.7; } 100% { transform: translate(20px, 30px) scale(1.1); opacity: 0.9; } }
@keyframes float-2 { 0% { transform: translate(0,0) scale(1); opacity: 0.6; } 100% { transform: translate(-30px, -20px) scale(1.15); opacity: 0.8; } }
@keyframes pulse-3 { 0% { transform: scale(0.9); opacity: 0.4; } 50% { transform: scale(1.2); opacity: 0.7; } 100% { transform: scale(0.9); opacity: 0.4; } }
canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 3; }
.badges-layer { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 5; pointer-events: none; }
.badge {
position: absolute; transform: translate(-50%, -50%);
padding: 6px 12px;
border-radius: 16px;
background: rgba(20, 20, 25, 0.75);
backdrop-filter: blur(8px);
border: 1px solid rgba(255,255,255,0.15);
box-shadow: 0 4px 8px rgba(0,0,0,0.4);
display: flex; align-items: center; gap: 8px; pointer-events: auto;
}
.badge-dot { width: 8px; height: 8px; border-radius: 50%; }
.is-cold .badge-dot { background: var(--color-cold); box-shadow: 0 0 5px var(--color-cold); }
.is-optimal .badge-dot { background: var(--color-opt); box-shadow: 0 0 5px var(--color-opt); }
.is-warm .badge-dot { background: var(--color-warm); box-shadow: 0 0 5px var(--color-warm); }
.is-hot .badge-dot { background: var(--color-hot); box-shadow: 0 0 5px var(--color-hot); }
.badge-content { display: flex; flex-direction: column; line-height: 1; }
.badge-name { font-size: 0.55rem; color: #aaa; text-transform: uppercase; margin-bottom: 2px; }
.badge-val { font-size: 0.80rem; font-weight: 700; color: #fff; }
.footer {
position: absolute; bottom: 0; left: 0; width: 100%; z-index: 5;
background: rgba(10, 10, 15, 0.25); backdrop-filter: blur(15px);
border-top: 1px solid rgba(255,255,255,0.05); padding: 12px 16px;
display: flex; align-items: center; gap: 12px; box-sizing: border-box; transition: background 0.3s;
min-height: 60px; /* Space for multi-line text */
}
.footer[data-status="warn"] { background: rgba(80, 50, 10, 0.65); border-top-color: var(--color-warm); }
.footer[data-status="danger"] { background: rgba(80, 20, 20, 0.65); border-top-color: var(--color-hot); }
.value-pill {
background: rgba(20, 20, 25, 0.75);
backdrop-filter: blur(8px);
border: 1px solid rgba(255,255,255,0.15);
box-shadow: 0 4px 8px rgba(0,0,0,0.4);
padding: 2px 8px;
border-radius: 20px;
color: rgba(255, 255, 255, 0.6);
white-space: nowrap;
transition: all 0.2s ease;
}
pill-1 {
margin-left: -5px;
margin-right: 5px;
}
.value-pill b { color: #fff; }
.median-pill {
display: none; /* Disabled mediana pill */
/* Disabled mediana pill */
background: rgba(20, 20, 25, 0.75);
backdrop-filter: blur(8px);
border: 1px solid rgba(255,255,255,0.15);
box-shadow: 0 4px 8px rgba(0,0,0,0.4);
padding: 4px 8px;
border-radius: 20px;
font-size: 0.8rem;
color: rgba(255, 255, 255, 0.6);
white-space: nowrap;
flex-shrink: 0;
align-self: flex-start;
margin-top: 2px;
transition: all 0.2s ease;
}
.median-pill b { color: #fff; }
/* Allow multi-line text for verbose AI messages */
.footer-content {
font-size: 0.85rem; color: #ccc;
white-space: normal; line-height: 1.8;
display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;
/*
overflow: hidden;
*/
}
</style>
<div class="card">
<div class="bg-image"></div>
<div class="gradient-layer"></div>
<div class="dim-layer"></div>
<div class="ambient-layer">
<div class="ambient-light blob-1"></div>
<div class="ambient-light blob-2"></div>
<div class="ambient-light blob-3"></div>
</div>
<canvas id="weatherCanvas"></canvas>
<div class="badges-layer"></div>
<div class="footer" data-status="normal">
<div class="median-pill">Dom: --</div>
<div class="footer-content">${this._t('loading')}</div>
</div>
</div>
`;
this._canvas = this.shadowRoot.getElementById('weatherCanvas');
this._ctx = this._canvas.getContext('2d');
setTimeout(() => this._resizeCanvas(), 100);
this.connectedCallback();
}
_resizeCanvas() {
if (!this._canvas) return;
const card = this.shadowRoot.querySelector('.card');
if (card) { this._canvas.width = card.clientWidth; this._canvas.height = card.clientHeight; }
}
// --- ANIMATIONS ---
_initStars() {
this._stars = [];
for (let i = 0; i < 60; i++) {
this._stars.push({
x: Math.random() * (this._canvas ? this._canvas.width : 300),
y: Math.random() * (this._canvas ? this._canvas.height : 200),
size: Math.random() * 1.5, opacity: Math.random(), speed: 0.01 + Math.random() * 0.02
});
}
}
_animate() {
if (!this._ctx) return;
const wEnt = this._config.weather_entity;
let wState = this._config.test_weather_state || (wEnt ? this._hass.states[wEnt]?.state : "");
const { speed, bearing } = this._getWindData();
const windDirX = (bearing > 180 || bearing < 0) ? -1 : 1;
let moveSpeed = speed / 15; if (moveSpeed < 0.2) moveSpeed = 0.2; if (moveSpeed > 6) moveSpeed = 6;
const sunEnt = this._config.sun_entity || 'sun.sun';
const isNight = this._hass.states[sunEnt]?.state === 'below_horizon';
const coverage = this._getCloudCoverage();
this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height);
if (isNight) this._drawStars(coverage);
if (wState === 'fog' || (isNight && ['rainy','cloudy'].includes(wState))) this._drawFog(moveSpeed);
if ((wState && !['clear-night','sunny'].includes(wState)) || coverage > 20) {
let density = 1; if(coverage>50) density=1.5; if(coverage>80) density=2;
this._drawClouds(windDirX, moveSpeed, density);
}
if (['rainy','pouring','lightning','lightning-rainy'].includes(wState)) {
this._drawRain(wState === 'pouring' ? 2 : 1, windDirX, moveSpeed);
} else if (['snowy','snowy-rainy'].includes(wState)) {
this._drawSnow(windDirX, moveSpeed);
}
if (['lightning','lightning-rainy'].includes(wState) || wState === 'lightning') this._handleLightning();
if (this._flashOpacity > 0) {
this._ctx.fillStyle = `rgba(255, 255, 255, ${this._flashOpacity})`;
this._ctx.fillRect(0,0, this._canvas.width, this._canvas.height);
this._flashOpacity -= 0.05;
}
this._animationFrame = requestAnimationFrame(() => this._animate());
}
_drawStars(coverage) {
const visibility = Math.max(0, 1 - (coverage / 80));
if (visibility <= 0) return;
this._ctx.fillStyle = "#FFF";
this._stars.forEach(star => {
this._ctx.globalAlpha = Math.abs(Math.sin(Date.now() * 0.001 * star.speed + star.x)) * star.opacity * visibility;
this._ctx.beginPath();
this._ctx.arc(star.x, star.y, star.size, 0, Math.PI * 2);
this._ctx.fill();
});
this._ctx.globalAlpha = 1.0;
}
_drawFog(speed) {
// FIXED: Organic Fog (Puffs) instead of Rectangular Bar
if (this._fogParticles.length < 10) {
this._fogParticles.push({
x: Math.random() * this._canvas.width,
y: this._canvas.height - (Math.random() * 50),
radius: 50 + Math.random() * 50,
speed: (Math.random() * 0.2) + 0.05
});
}
this._fogParticles.forEach(f => {
f.x += f.speed * (speed * 0.5);
if (f.x > this._canvas.width + 100) f.x = -100;
const g = this._ctx.createRadialGradient(f.x, f.y, 0, f.x, f.y, f.radius);
g.addColorStop(0, 'rgba(200, 200, 210, 0.15)');
g.addColorStop(1, 'rgba(200, 200, 210, 0)');
this._ctx.fillStyle = g;
this._ctx.beginPath();
this._ctx.arc(f.x, f.y, f.radius, 0, Math.PI * 2);
this._ctx.fill();
});
}
_drawClouds(dirX, baseSpeed, density) {
const target = Math.floor(5 * density);
if (this._clouds.length < target) {
const newCloud = this._createCloud(false); newCloud.x = dirX > 0 ? -200 : this._canvas.width + 200;
this._clouds.push(newCloud);
}
if (this._clouds.length > target) this._clouds.pop();
this._clouds.forEach((cloud, index) => {
cloud.x += baseSpeed * 0.3 * dirX;
if ((dirX > 0 && cloud.x > this._canvas.width + 200) || (dirX < 0 && cloud.x < -200)) { this._clouds.splice(index, 1); return; }
this._ctx.save(); this._ctx.translate(cloud.x, cloud.y); this._ctx.scale(cloud.scale, cloud.scale);
cloud.puffs.forEach(puff => {
const gradient = this._ctx.createRadialGradient(puff.xOffset, puff.yOffset, 0, puff.xOffset, puff.yOffset, puff.radius);
gradient.addColorStop(0, `rgba(255, 255, 255, ${puff.opacity * 0.8})`); gradient.addColorStop(1, 'rgba(255, 255, 255, 0)');
this._ctx.fillStyle = gradient; this._ctx.beginPath(); this._ctx.arc(puff.xOffset, puff.yOffset, puff.radius, 0, Math.PI * 2); this._ctx.fill();
});
this._ctx.restore();
});
}
_createCloud(randomX) {
const puffs = []; const numPuffs = 4 + Math.floor(Math.random() * 4); const cloudWidth = 100 + Math.random() * 80;
for (let j = 0; j < numPuffs; j++) puffs.push({ xOffset: (Math.random() * cloudWidth) - (cloudWidth/2), yOffset: (Math.random() * 30) - 15, radius: 25 + Math.random() * 20, opacity: 0.1 + Math.random() * 0.2 });
return { x: randomX ? Math.random() * (this._canvas ? this._canvas.width : 300) : -150, y: Math.random() * 100, scale: 0.8 + Math.random() * 0.4, puffs: puffs };
}
_drawRain(intensity, windDirX, windSpeed) {
if (this._particles.length < 150 * intensity) this._particles.push({ x: Math.random() * this._canvas.width, y: -20, speed: 15 + windSpeed, length: 15 + Math.random() * 10 });
this._ctx.strokeStyle = 'rgba(174, 194, 224, 0.6)'; this._ctx.lineWidth = 1; this._ctx.beginPath();
const angleX = windDirX * (windSpeed * 1.5);
for (let i = 0; i < this._particles.length; i++) {
const p = this._particles[i];
this._ctx.moveTo(p.x, p.y); this._ctx.lineTo(p.x + angleX, p.y + p.length);
p.y += p.speed; p.x += angleX;
if (p.y > this._canvas.height || p.x > this._canvas.width + 50 || p.x < -50) { this._particles.splice(i, 1); i--; }
}
this._ctx.stroke();
}
_drawSnow(windDirX, windSpeed) {
if (this._particles.length < 100) this._particles.push({ x: Math.random() * this._canvas.width, y: -10, speed: 1 + Math.random(), radius: 1.5 + Math.random() });
this._ctx.fillStyle = 'rgba(255, 255, 255, 0.8)'; this._ctx.beginPath();
for (let i = 0; i < this._particles.length; i++) {
const p = this._particles[i];
this._ctx.moveTo(p.x, p.y); this._ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
p.y += p.speed; p.x += (Math.sin(p.y * 0.03) * 0.5) + (windDirX * windSpeed * 0.5);
if (p.y > this._canvas.height || p.x > this._canvas.width + 50 || p.x < -50) { this._particles.splice(i, 1); i--; }
}
this._ctx.fill();
}
_handleLightning() {
this._lightningTimer++;
if (this._lightningTimer > 200 && Math.random() > 0.98) { this._triggerLightning(); this._lightningTimer = 0; }
if (this._lightningBolt && this._lightningBolt.life > 0) { this._drawBolt(this._lightningBolt); this._lightningBolt.life--; }
}
_triggerLightning() {
const startX = Math.random() * this._canvas.width; const path = [{x: startX, y: 0}]; let currX = startX, currY = 0;
while(currY < this._canvas.height * 0.8) { currY += Math.random() * 40 + 20; currX += (Math.random() * 60) - 30; path.push({x: currX, y: currY}); }
this._lightningBolt = { path, life: 10 }; this._flashOpacity = 0.5;
}
_drawBolt(bolt) {
this._ctx.strokeStyle = 'rgba(255, 255, 255, 0.9)'; this._ctx.lineWidth = 2; this._ctx.beginPath();
this._ctx.moveTo(bolt.path[0].x, bolt.path[0].y); for(let p of bolt.path) this._ctx.lineTo(p.x, p.y); this._ctx.stroke();
}
}
customElements.define('fork-u-house-card', ForkUHouseCard);
window.customCards = window.customCards || [];
window.customCards.push({ type: "fork-u-house-card", name: "Fork U-House Card V11.0", description: "AI Storyteller Edition" });