2024-08-17 22:48:00 -06:00
|
|
|
class SpaceWeatherPredictionCard extends HTMLElement {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.attachShadow({mode: 'open'});
|
2024-08-19 14:03:43 -06:00
|
|
|
this._selectedDay = 'today';
|
2024-08-17 22:48:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
setConfig(config) {
|
|
|
|
this._config = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
set hass(hass) {
|
|
|
|
this._hass = hass;
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (!this.shadowRoot) return;
|
|
|
|
|
|
|
|
this.shadowRoot.innerHTML = `
|
|
|
|
<style>
|
|
|
|
/* TODO: unify this with the other card */
|
2024-08-19 14:03:43 -06:00
|
|
|
|
2024-08-18 15:38:47 -06:00
|
|
|
.prediction-container {
|
|
|
|
display: grid;
|
|
|
|
grid-template-columns: repeat(4, 1fr);
|
|
|
|
gap: 16px;
|
|
|
|
align-items: center;
|
|
|
|
justify-items: center;
|
|
|
|
}
|
2024-08-17 22:48:00 -06:00
|
|
|
|
|
|
|
.prediction-item {
|
|
|
|
text-align: center;
|
2024-08-18 14:49:30 -06:00
|
|
|
cursor: pointer;
|
2024-08-17 22:48:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
.prediction-label {
|
|
|
|
font-weight: bold;
|
|
|
|
margin-bottom: 8px;
|
2024-08-19 18:29:53 -06:00
|
|
|
min-height: 40px;
|
2024-08-17 22:48:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
.prediction-value {
|
|
|
|
font-size: 24px;
|
|
|
|
font-weight: bold;
|
|
|
|
margin-bottom: 4px;
|
|
|
|
padding: 8px;
|
|
|
|
border-radius: 4px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.noaa_scale_bg_5 {
|
|
|
|
background-color: #C80000;
|
|
|
|
}
|
|
|
|
.noaa_scale_bg_4 {
|
|
|
|
background-color: #FF0000;
|
|
|
|
}
|
|
|
|
.noaa_scale_bg_3 {
|
|
|
|
background-color: #FF9600;
|
|
|
|
}
|
|
|
|
.noaa_scale_bg_2 {
|
|
|
|
background-color: #FFC800;
|
|
|
|
}
|
|
|
|
.noaa_scale_bg_1 {
|
|
|
|
background-color: #F6EB14;
|
|
|
|
}
|
|
|
|
.noaa_scale_bg_0 {
|
|
|
|
background-color: #92D050;
|
|
|
|
}
|
|
|
|
|
|
|
|
.card-header {
|
|
|
|
font-size: 20px;
|
|
|
|
font-weight: bold;
|
2024-08-18 14:49:30 -06:00
|
|
|
padding: 0 16px 2px 16px;
|
|
|
|
text-align: center;
|
|
|
|
}
|
2024-08-19 14:03:43 -06:00
|
|
|
|
2024-08-18 14:49:30 -06:00
|
|
|
.card-subheader {
|
|
|
|
padding: 0 16px 16px 16px;
|
2024-08-17 22:48:00 -06:00
|
|
|
margin-bottom: 16px;
|
|
|
|
text-align: center;
|
2024-08-18 14:49:30 -06:00
|
|
|
font-style: italic;
|
|
|
|
font-size: 15px;
|
2024-08-17 22:48:00 -06:00
|
|
|
}
|
2024-08-19 14:03:43 -06:00
|
|
|
|
2024-08-17 22:48:00 -06:00
|
|
|
.scale-value {
|
|
|
|
font-size: 24px;
|
|
|
|
font-weight: bold;
|
|
|
|
margin-bottom: 4px;
|
|
|
|
padding: 8px;
|
|
|
|
border-radius: 4px;
|
|
|
|
}
|
2024-08-19 14:03:43 -06:00
|
|
|
|
2024-08-17 22:48:00 -06:00
|
|
|
a {
|
|
|
|
text-decoration: none;
|
|
|
|
color: inherit;
|
|
|
|
}
|
2024-08-19 14:03:43 -06:00
|
|
|
|
|
|
|
.button-container {
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
margin-top: 16px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.day-button {
|
|
|
|
margin: 0 8px;
|
|
|
|
padding: 8px 16px;
|
|
|
|
border: none;
|
|
|
|
border-radius: 4px;
|
|
|
|
background-color: #e5e5e5;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
.day-button.selected {
|
|
|
|
background-color: #3788d8;
|
|
|
|
color: #fff;
|
|
|
|
}
|
2024-08-17 22:48:00 -06:00
|
|
|
</style>
|
|
|
|
|
|
|
|
<ha-card>
|
|
|
|
<div class="card-header">Space Weather Predictions</div>
|
2024-08-18 14:49:30 -06:00
|
|
|
<div class="card-subheader">
|
2024-08-19 17:26:44 -06:00
|
|
|
${this._getAttribute(`sensor.space_weather_prediction_r_minorprob_${this._selectedDay}`, 'timestamp').split('T')[0]}
|
2024-08-18 14:49:30 -06:00
|
|
|
</div>
|
2024-08-17 22:48:00 -06:00
|
|
|
<div class="card-content">
|
|
|
|
<div class="prediction-container">
|
2024-08-19 14:03:43 -06:00
|
|
|
<div class="prediction-item" data-entity-id="sensor.space_weather_prediction_r_minorprob_${this._selectedDay}">
|
2024-08-17 22:48:00 -06:00
|
|
|
<div class="prediction-label">R1-R2</div>
|
|
|
|
<!-- TODO: what happens when "Scale" in JSON is not null? -->
|
|
|
|
<!-- TODO: what happens when "Text" in JSON is not null? -->
|
|
|
|
<div class="prediction-value">
|
2024-08-19 14:03:43 -06:00
|
|
|
${Math.round(parseFloat(this._getStateValue(`sensor.space_weather_prediction_r_minorprob_${this._selectedDay}`)))}%
|
2024-08-17 22:48:00 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-08-19 14:03:43 -06:00
|
|
|
<div class="prediction-item" data-entity-id="sensor.space_weather_prediction_r_majorprob_${this._selectedDay}">
|
2024-08-17 22:48:00 -06:00
|
|
|
<div class="prediction-label">R3-R5</div>
|
|
|
|
<!-- TODO: what happens when "Scale" in JSON is not null? -->
|
|
|
|
<!-- TODO: what happens when "Text" in JSON is not null? -->
|
|
|
|
<div class="prediction-value">
|
2024-08-19 14:03:43 -06:00
|
|
|
${Math.round(parseFloat(this._getStateValue(`sensor.space_weather_prediction_r_majorprob_${this._selectedDay}`)))}%
|
2024-08-17 22:48:00 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-08-19 14:03:43 -06:00
|
|
|
<div class="prediction-item" data-entity-id="sensor.space_weather_prediction_s_prob_${this._selectedDay}">
|
2024-08-17 22:48:00 -06:00
|
|
|
<div class="prediction-label">S1 or Greater</div>
|
|
|
|
<!-- TODO: what happens when "Scale" in JSON is not null? -->
|
|
|
|
<!-- TODO: what happens when "Text" in JSON is not null? -->
|
|
|
|
<div class="prediction-value">
|
2024-08-19 14:03:43 -06:00
|
|
|
${Math.round(parseFloat(this._getStateValue(`sensor.space_weather_prediction_s_prob_${this._selectedDay}`)))}%
|
2024-08-17 22:48:00 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<!-- <div class="prediction-item">
|
|
|
|
<div class="prediction-label">S Probability</div>
|
|
|
|
<div class="prediction-value">
|
2024-08-19 14:03:43 -06:00
|
|
|
${this._getStateValue('sensor.space_weather_prediction_s_scale_today')}
|
2024-08-17 22:48:00 -06:00
|
|
|
</div>
|
|
|
|
</div> -->
|
2024-08-19 14:03:43 -06:00
|
|
|
<div class="prediction-item" data-entity-id="sensor.space_weather_prediction_g_scale_${this._selectedDay}">
|
2024-08-17 22:48:00 -06:00
|
|
|
<div class="prediction-label">G Scale</div>
|
2024-08-19 14:03:43 -06:00
|
|
|
<div class="prediction-value scale-value noaa_scale_bg_${this._getStateValue(`sensor.space_weather_prediction_g_scale_${this._selectedDay}`)}">
|
|
|
|
G${this._getStateValue(`sensor.space_weather_prediction_g_scale_${this._selectedDay}`)}
|
2024-08-17 22:48:00 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-08-19 14:03:43 -06:00
|
|
|
<div class="button-container">
|
|
|
|
<button class="day-button ${this._selectedDay === 'today' ? 'selected' : ''}" data-day="today">Today</button>
|
|
|
|
<button class="day-button ${this._selectedDay === '1day' ? 'selected' : ''}" data-day="1day">1 Day</button>
|
|
|
|
<button class="day-button ${this._selectedDay === '2day' ? 'selected' : ''}" data-day="2day">2 Day</button>
|
|
|
|
</div>
|
2024-08-17 22:48:00 -06:00
|
|
|
</div>
|
|
|
|
</ha-card>
|
|
|
|
`;
|
2024-08-18 14:49:30 -06:00
|
|
|
this._attachClickListeners();
|
2024-08-17 22:48:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
_getStateValue(entityId) {
|
|
|
|
const state = this._hass.states[entityId];
|
|
|
|
return state ? state.state : '';
|
|
|
|
}
|
|
|
|
|
2024-08-18 14:49:30 -06:00
|
|
|
_getAttribute(entityId, attribute) {
|
|
|
|
const state = this._hass.states[entityId];
|
|
|
|
return state ? state.attributes[attribute] : '';
|
|
|
|
}
|
|
|
|
|
2024-08-17 22:48:00 -06:00
|
|
|
_getNumericState(entityId) {
|
|
|
|
const stateValue = this._getStateValue(entityId);
|
|
|
|
return stateValue.substring(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
getCardSize() {
|
|
|
|
return 5;
|
|
|
|
}
|
2024-08-18 14:49:30 -06:00
|
|
|
|
|
|
|
_attachClickListeners() {
|
|
|
|
const scaleItems = this.shadowRoot.querySelectorAll('.prediction-item');
|
|
|
|
scaleItems.forEach(item => {
|
|
|
|
item.addEventListener('click', () => {
|
|
|
|
const entityId = item.dataset.entityId;
|
|
|
|
this._handleClick(entityId);
|
|
|
|
});
|
|
|
|
});
|
2024-08-19 14:03:43 -06:00
|
|
|
|
|
|
|
const dayButtons = this.shadowRoot.querySelectorAll('.day-button');
|
|
|
|
dayButtons.forEach(button => {
|
|
|
|
button.addEventListener('click', () => {
|
2024-08-19 18:29:53 -06:00
|
|
|
this._selectedDay = button.dataset.day;
|
2024-08-19 14:03:43 -06:00
|
|
|
this.render();
|
|
|
|
});
|
|
|
|
});
|
2024-08-18 14:49:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleClick(entityId) {
|
|
|
|
const event = new Event('hass-more-info', {composed: true});
|
|
|
|
event.detail = {entityId};
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
}
|
2024-08-17 22:48:00 -06:00
|
|
|
}
|
|
|
|
|
2024-08-19 14:03:43 -06:00
|
|
|
customElements.define('space-weather-prediction', SpaceWeatherPredictionCard);
|