class SpaceWeatherPredictionCard extends HTMLElement { constructor() { super(); this.attachShadow({mode: 'open'}); this._selectedDay = 'today'; } setConfig(config) { this._config = config; } set hass(hass) { this._hass = hass; this.render(); } render() { if (!this.shadowRoot) return; this.shadowRoot.innerHTML = `
Space Weather Predictions
${this._getAttribute(`sensor.space_weather_prediction_r_minorprob_${this._selectedDay}`, 'timestamp').split('T')[0]}
R1-R2
${Math.round(parseFloat(this._getStateValue(`sensor.space_weather_prediction_r_minorprob_${this._selectedDay}`)))}%
R3-R5
${Math.round(parseFloat(this._getStateValue(`sensor.space_weather_prediction_r_majorprob_${this._selectedDay}`)))}%
S1 or Greater
${Math.round(parseFloat(this._getStateValue(`sensor.space_weather_prediction_s_prob_${this._selectedDay}`)))}%
G Scale
G${this._getStateValue(`sensor.space_weather_prediction_g_scale_${this._selectedDay}`)}
`; this._attachClickListeners(); } _getStateValue(entityId) { const state = this._hass.states[entityId]; return state ? state.state : ''; } _getAttribute(entityId, attribute) { const state = this._hass.states[entityId]; return state ? state.attributes[attribute] : ''; } _getNumericState(entityId) { const stateValue = this._getStateValue(entityId); return stateValue.substring(1); } getCardSize() { return 5; } _attachClickListeners() { const scaleItems = this.shadowRoot.querySelectorAll('.prediction-item'); scaleItems.forEach(item => { item.addEventListener('click', () => { const entityId = item.dataset.entityId; this._handleClick(entityId); }); }); const dayButtons = this.shadowRoot.querySelectorAll('.day-button'); dayButtons.forEach(button => { button.addEventListener('click', () => { this._selectedDay = button.dataset.day; this.render(); }); }); } _handleClick(entityId) { const event = new Event('hass-more-info', {composed: true}); event.detail = {entityId}; this.dispatchEvent(event); } } customElements.define('space-weather-prediction', SpaceWeatherPredictionCard);