-
Notifications
You must be signed in to change notification settings - Fork 6
/
moon.js
142 lines (132 loc) · 4.74 KB
/
moon.js
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
/**
* Normalize moon phase number
*
* @param {String} phase
*
* @return {String}
*/
const getMoonPhase = (phase) => {
switch(phase) {
case 'new_moon':
return {
name: 'New Moon',
link: 'https://www.timeanddate.com/astronomy/moon/new-moon.html'
};
case 'waxing_crescent':
return {
name: 'Waxing Crescent Moon',
link: 'https://www.timeanddate.com/astronomy/moon/waxing-crescent.html'
};
case 'first_quarter':
return {
name: 'First Quarter Moon',
link: 'https://www.timeanddate.com/astronomy/moon/first-quarter.html'
};
case 'waxing_gibbous':
return {
name: 'Waxing Gibbous Moon',
link: 'https://www.timeanddate.com/astronomy/moon/waxing-gibbous.html'
};
case 'full_moon':
return {
name: 'Full Moon',
link: 'https://www.timeanddate.com/astronomy/moon/full-moon.html'
};
case 'waning_gibbous':
return {
name: 'Waning Gibbous Moon',
link: 'https://www.timeanddate.com/astronomy/moon/waning-gibbous.html'
};
case 'last_quarter':
return {
name: 'Last Quarter Moon',
link: 'https://www.timeanddate.com/astronomy/moon/third-quarter.html'
};
case 'waning_crescent':
return {
name: 'Waning Crescent Moon',
link: 'https://www.timeanddate.com/astronomy/moon/waning-crescent.html'
};
}
}
/**
* Phases of the moon custom lovelace UI card
*/
class MoonPhasesCard extends HTMLElement {
set hass(hass) {
if (!this.content) {
// Get current date
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
// Get current moon phase w/moon entity
const entities = this.config.entities;
const moonState = hass.states[entities[0]].state;
const phase = getMoonPhase(moonState);
const sunState = hass.states[entities[1]].state;
console.log(`Current phase - ${phase} 🌙`);
// Render card
let card = document.createElement('ha-card');
this.content = document.createElement('div');
this.content.style.padding = '0 16px 16px';
card.appendChild(this.content);
this.appendChild(card);
// Styles
let style = document.createElement("style");
style.textContent = `
ha-card {
color: #FAFAFA;
}
.header {
display: flex;
justify-content: space-between;
padding: 1em 0;
font-weight: bold;
}
.title, .date {
font-size: 1em;
}
.content {
display: flex;
align-items: center;
justify-content: center;
padding-top: 1em;
font-weight: bold;
}
.content a {
color: #FAFAFA;
text-decoration: none;
}
.img, .name {
padding: 0 1em;
}
`;
card.appendChild(style);
// Toggle background based on sun above/below horizon
const backgroundColor = (sunState === 'above_horizon' ? '#87CEFA' : '#39006c');
card.style['background-color'] = backgroundColor;
// DOM
this.content.innerHTML = `
<div class="header">
<div class="title">CURRENT PHASE</div>
<div class="date">${month}/${day}/${year}</div>
</div>
<div class="content">
<img src="/hacsfiles/moon-phases/images/${phase.name}.png" class="img">
<span class="name"><a href="${phase.link}" target="_blank">${phase.name}</a></span>
</div>
`;
}
}
setConfig(config) {
if (!config.entities) {
throw new Error('You need to define an entity');
}
if (!config.entities[0].includes('moon')) {
throw new Error('Moon entity must be included first');
}
this.config = config;
}
}
customElements.define('moon-phases', MoonPhasesCard);