Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class DarkSkyWeatherCard extends HTMLElement {
  2.   set hass(hass) {
  3.     if (!this.content) {
  4.       const card = document.createElement('ha-card');
  5.       const link = document.createElement('link');
  6.       link.type = 'text/css';
  7.       link.rel = 'stylesheet';
  8.       link.href = '/local/lovelace/dark-sky-weather-card.css';
  9.       card.appendChild(link);
  10.       this.content = document.createElement('div');
  11.       this.content.className = 'card';
  12.       card.appendChild(this.content);
  13.       this.appendChild(card);
  14.     }
  15.  
  16.     const getUnit = function(measure) {
  17.       const lengthUnit = hass.config.unit_system.length;
  18.       switch (measure) {
  19.         case 'air_pressure':
  20.           return lengthUnit === 'km' ? 'hPa' : 'mbar';
  21.         case 'length':
  22.           return lengthUnit;
  23.         case 'precipitation':
  24.           return lengthUnit === 'km' ? 'mm' : 'in';
  25.         default:
  26.           return hass.config.unit_system[measure] || '';
  27.       }
  28.     };
  29.  
  30.     const transformDayNight = {
  31.       "below_horizon": "night",
  32.       "above_horizon": "day",
  33.     }
  34.     const sunLocation = transformDayNight[hass.states[this.config.entity_sun].state];
  35.     const weatherIcons = {
  36.       'clear-day': 'day',
  37.       'clear-night': 'night',
  38.       'rain': 'rainy-5',
  39.       'snow': 'snowy-6',
  40.       'sleet': 'rainy-6',
  41.       'wind': 'cloudy',
  42.       'fog': 'cloudy',
  43.       'cloudy': 'cloudy',
  44.       'partly-cloudy-day': 'cloudy-day-3',
  45.       'partly-cloudy-night': 'cloudy-night-3',
  46.       'hail': 'rainy-7',
  47.       'lightning': 'thunder',
  48.       'thunderstorm': 'thunder',
  49.       'windy-variant': `cloudy-${sunLocation}-3`,
  50.       'exceptional': '!!',
  51.     }
  52.  
  53.     const windDirections = [
  54.       'N',
  55.       'NNE',
  56.       'NE',
  57.       'ENE',
  58.       'E',
  59.       'ESE',
  60.       'SE',
  61.       'SSE',
  62.       'S',
  63.       'SSW',
  64.       'SW',
  65.       'WSW',
  66.       'W',
  67.       'WNW',
  68.       'NW',
  69.       'NNW',
  70.       'N'
  71.     ];
  72.  
  73.     var forecastDate1 = new Date();
  74.     forecastDate1.setDate(forecastDate1.getDate() + 1);
  75.     var forecastDate2 = new Date();
  76.     forecastDate2.setDate(forecastDate2.getDate() + 2);
  77.     var forecastDate3 = new Date();
  78.     forecastDate3.setDate(forecastDate3.getDate() + 3);
  79.     var forecastDate4 = new Date();
  80.     forecastDate4.setDate(forecastDate4.getDate() + 4);
  81.     var forecastDate5 = new Date();
  82.     forecastDate5.setDate(forecastDate5.getDate() + 5);
  83.  
  84.  
  85.     const currentConditions = hass.states[this.config.entity_current_conditions].state;
  86.     const humidity = hass.states[this.config.entity_humidity].state;
  87.     const pressure = Math.round(hass.states[this.config.entity_pressure].state);
  88.     const temperature = Math.round(hass.states[this.config.entity_temperature].state);
  89.     const visibility = hass.states[this.config.entity_visibility].state;
  90.     const windBearing = windDirections[(Math.round((hass.states[this.config.entity_wind_bearing].state / 360) * 16))];
  91.     const windSpeed = Math.round(hass.states[this.config.entity_wind_speed].state);
  92.     const sunrise = hass.states[this.config.entity_sunrise].state;
  93.     const sunset = hass.states[this.config.entity_sunset].state;
  94.     const forecast1 = {
  95.       date: forecastDate1,
  96.       condition: this.config.entity_forecast_icon_1,
  97.       temphigh: this.config.entity_forecast_high_temp_1,
  98.       templow: this.config.entity_forecast_low_temp_1,
  99.     };
  100.     const forecast2 = {
  101.       date: forecastDate2,
  102.       condition: this.config.entity_forecast_icon_2,
  103.       temphigh: this.config.entity_forecast_high_temp_2,
  104.       templow: this.config.entity_forecast_low_temp_2,
  105.     };
  106.     const forecast3 = {
  107.       date: forecastDate3,
  108.       condition: this.config.entity_forecast_icon_3,
  109.       temphigh: this.config.entity_forecast_high_temp_3,
  110.       templow: this.config.entity_forecast_low_temp_3,
  111.     };
  112.     const forecast4 = {
  113.       date: forecastDate4,
  114.       condition: this.config.entity_forecast_icon_4,
  115.       temphigh: this.config.entity_forecast_high_temp_4,
  116.       templow: this.config.entity_forecast_low_temp_4,
  117.     };
  118.     const forecast5 = {
  119.       date: forecastDate5,
  120.       condition: this.config.entity_forecast_icon_5,
  121.       temphigh: this.config.entity_forecast_high_temp_5,
  122.       templow: this.config.entity_forecast_low_temp_5,
  123.     };
  124.  
  125.     const forecast = [forecast1, forecast2, forecast3, forecast4, forecast5];
  126.  
  127.  
  128.     this.content.innerHTML = `
  129.       <span class="icon bigger" style="background: none, url(/local/icons/weather_icons/animated/${weatherIcons[currentConditions]}.svg) no-repeat; background-size: contain;">${currentConditions}</span>
  130.       <span class="temp">${temperature}</span><span class="tempc"> ${getUnit('temperature')}</span>
  131.       <span>
  132.         <ul class="variations right">
  133.             <li><span class="ha-icon"><ha-icon icon="mdi:water-percent"></ha-icon></span>${humidity}<span class="unit"> %</span></li>
  134.             <li><span class="ha-icon"><ha-icon icon="mdi:gauge"></ha-icon></span>${pressure}<span class="unit"> ${getUnit('air_pressure')}</span></li>
  135.             <li><span class="ha-icon"><ha-icon icon="mdi:weather-sunset-down"></ha-icon></span>${sunset}</li>
  136.         </ul>
  137.         <ul class="variations">
  138.             <li><span class="ha-icon"><ha-icon icon="mdi:weather-windy"></ha-icon></span>${windBearing} ${windSpeed}<span class="unit"> ${getUnit('length')}/h</span></li>
  139.             <li><span class="ha-icon"><ha-icon icon="mdi:weather-fog"></ha-icon></span>${visibility}<span class="unit"> ${getUnit('length')}</span></li>
  140.             <li><span class="ha-icon"><ha-icon icon="mdi:weather-sunset-up"></ha-icon></span>${sunrise}</li>
  141.         </ul>
  142.       </span>
  143.       <div class="forecast clear">
  144.           ${forecast.map(daily => `
  145.               <div class="day">
  146.                   <span class="dayname">${(new Intl.DateTimeFormat(this.config.language, {weekday: 'short'})).format(new Date(daily.date)).split('.')[0].toUpperCase()}</span>
  147.                   <br><i class="icon" style="background: none, url(/local/icons/weather_icons/animated/${weatherIcons[hass.states[daily.condition].state]}.svg) no-repeat; background-size: contain;"></i>
  148.                   <br><span class="highTemp">${Math.round(hass.states[daily.temphigh].state)}${getUnit('temperature')}</span>
  149.                   <br><span class="lowTemp">${Math.round(hass.states[daily.templow].state)}${getUnit('temperature')}</span>
  150.               </div>`).join('')}
  151.       </div>
  152.       <br><span class="summary">${hass.states[this.config.entity_daily_summary].state}</span></br>`;
  153.   }
  154.  
  155.   setConfig(config) {
  156.     if (!config.entity_current_conditions ||
  157.       !config.entity_humidity ||
  158.       !config.entity_pressure ||
  159.       !config.entity_temperature ||
  160.       !config.entity_visibility ||
  161.       !config.entity_wind_bearing ||
  162.       !config.entity_wind_speed) {
  163.       throw new Error('Please define entities');
  164.     }
  165.     this.config = config;
  166.   }
  167.  
  168.   // @TODO: This requires more intelligent logic
  169.   getCardSize() {
  170.     return 4;
  171.   }
  172. }
  173.  
  174. customElements.define('dark-sky-weather-card', DarkSkyWeatherCard);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement