2024-08-17 22:48:00 -06:00
|
|
|
class SpaceWeatherPredictionCard extends HTMLElement {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.attachShadow({mode: 'open'});
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
.prediction-container {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.prediction-item {
|
|
|
|
text-align: center;
|
|
|
|
margin-bottom: 16px;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
.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
|
|
|
}
|
|
|
|
|
|
|
|
.scale-value {
|
|
|
|
font-size: 24px;
|
|
|
|
font-weight: bold;
|
|
|
|
margin-bottom: 4px;
|
|
|
|
padding: 8px;
|
|
|
|
border-radius: 4px;
|
|
|
|
}
|
|
|
|
|
|
|
|
a {
|
|
|
|
text-decoration: none;
|
|
|
|
color: inherit;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<ha-card>
|
|
|
|
<div class="card-header">Space Weather Predictions</div>
|
2024-08-18 14:49:30 -06:00
|
|
|
<div class="card-subheader">
|
|
|
|
${this._getStateValue('sensor.space_weather_prediction_date_stamp')}
|
|
|
|
</div>
|
2024-08-17 22:48:00 -06:00
|
|
|
<div class="card-content">
|
|
|
|
<div class="prediction-container">
|
2024-08-18 14:49:30 -06:00
|
|
|
<div class="prediction-item" data-entity-id="sensor.space_weather_prediction_r_minorprob">
|
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">
|
|
|
|
${Math.round(parseFloat(this._getStateValue('sensor.space_weather_prediction_r_minorprob')))}%
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-08-18 14:49:30 -06:00
|
|
|
<div class="prediction-item" data-entity-id="sensor.space_weather_prediction_r_majorprob">
|
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">
|
|
|
|
${Math.round(parseFloat(this._getStateValue('sensor.space_weather_prediction_r_majorprob')))}%
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-08-18 14:49:30 -06:00
|
|
|
<div class="prediction-item" data-entity-id="sensor.space_weather_prediction_s_prob">
|
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">
|
|
|
|
${Math.round(parseFloat(this._getStateValue('sensor.space_weather_prediction_s_prob')))}%
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<!-- <div class="prediction-item">
|
|
|
|
<div class="prediction-label">S Probability</div>
|
|
|
|
<div class="prediction-value">
|
2024-08-18 14:49:30 -06:00
|
|
|
${this._getStateValue('sensor.space_weather_prediction_s_scale')}
|
2024-08-17 22:48:00 -06:00
|
|
|
</div>
|
|
|
|
</div> -->
|
2024-08-18 14:49:30 -06:00
|
|
|
<div class="prediction-item" data-entity-id="sensor.space_weather_prediction_g_scale">
|
2024-08-17 22:48:00 -06:00
|
|
|
<div class="prediction-label">G Scale</div>
|
|
|
|
<div class="prediction-value scale-value noaa_scale_bg_${this._getStateValue('sensor.space_weather_prediction_g_scale')}">
|
|
|
|
G${this._getStateValue('sensor.space_weather_prediction_g_scale')}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_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
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define('space-weather-prediction-card', SpaceWeatherPredictionCard);
|