import { cwcLocale , cwcMoonPhaseIcons } from "./ha-cwc-consts" ; import { HomeAssistant } from "custom-card-helpers/dist" ; import { IconsConfig } from "./types" ; import { hacsImagePathExist , manImagePathExist , numberFormat_0dec , numberFormat_1dec } from "./ha-card-weather-conditions" ; export function pad ( n , width , z = undefined ) { z = z || '0' ; n = n + '' ; return n . length >= width ? n : new Array ( width - n . length + 1 ) . join ( z ) + n ; } param imageSrc export function imageExist ( imageSrc ) { return new Promise < boolean > ( ( resolve ) => { setTimeout ( ( ) => { let img = new Image ( ) ; img . onload = ( ) => { resolve ( true ) ; } ; img . onerror = ( ) => { resolve ( false ) ; } ; img . src = imageSrc ; } , 100 ) ; } ) ; } param term param terms export const translate = ( term , terms ) => { return terms [ Object . keys ( terms ) . find ( key => key . toLowerCase ( ) === term . toLowerCase ( ) ) ] ? terms [ Object . keys ( terms ) . find ( key => key . toLowerCase ( ) === term . toLowerCase ( ) ) ] : term ; } ; param condition param iconsConfig param sunState export const getWeatherIcon = ( condition , iconsConfig , sunState ) => { let isNight = sunState && sunState == "below_horizon" ; let iconName = isNight ? iconsConfig . iconsNight [ condition ] : iconsConfig . iconsDay [ condition ] ; if ( iconsConfig . path == null ) { console . info ( "Image path not found. (hacsImagePathExist=" + hacsImagePathExist + ")(manImagePathExist=" + manImagePathExist ) ; } if ( undefined === iconName ) console . info ( "Icons issue. States: icons_model=" + iconsConfig . icons_model + " - isDay=" + ( ! isNight ) + " - condition: " + condition + "." ) ; return `${ iconsConfig . path }/${ iconsConfig . iconType }/${ iconName }.svg` ; } ; param condition param iconsConfig param sunState export const getWeatherBg = ( condition , iconsConfig , sunState ) => { let isNight = sunState && sunState == "below_horizon" ; let iconName = isNight ? iconsConfig . iconsNight [ condition ] : iconsConfig . iconsDay [ condition ] ; if ( iconsConfig . path == null ) { console . info ( "Image path not found. (hacsImagePathExist=" + hacsImagePathExist + ")(manImagePathExist=" + manImagePathExist ) ; } if ( undefined === iconName ) console . info ( "Icons issue. States: icons_model=" + iconsConfig . icons_model + " - isDay=" + ( ! isNight ) + " - condition: " + condition + "." ) ; return `${ iconsConfig . path }/background/${ iconName }` ; } ; param measure param hass export const getUnit = ( hass , measure ) => { const lengthUnit = hass . config . unit_system . length ; switch ( measure ) { case "air_pressure" : return lengthUnit === "km" ? "hPa" : "inHg" ; case "length" : return lengthUnit ; case "precipitation" : return lengthUnit === "km" ? "mm" : "in" ; default : return hass . config . unit_system [ measure ] || "" ; } } ; export const getWindDirections = ( wd , cwcLocWindDirections ) => { if ( wd < 0 || wd > 360 ) { console . log ( "Enter a degree between 0 and 360 degrees." ) ; return null ; } if ( wd >= 0 && wd <= 11.25 ) return cwcLocWindDirections [ 'N' ] ; if ( wd > 348.75 && wd <= 360 ) return cwcLocWindDirections [ 'N' ] ; if ( wd > 11.25 && wd <= 33.75 ) return cwcLocWindDirections [ 'NNE' ] ; if ( wd > 33.75 && wd <= 56.25 ) return cwcLocWindDirections [ 'NE' ] ; if ( wd > 56.25 && wd <= 78.75 ) return cwcLocWindDirections [ 'ENE' ] ; if ( wd > 78.75 && wd <= 101.25 ) return cwcLocWindDirections [ 'E' ] ; if ( wd > 101.25 && wd <= 123.75 ) return cwcLocWindDirections [ 'ESE' ] ; if ( wd > 123.75 && wd <= 146.25 ) return cwcLocWindDirections [ 'SE' ] ; if ( wd > 146.25 && wd <= 168.75 ) return cwcLocWindDirections [ 'SSE' ] ; if ( wd > 168.75 && wd <= 191.25 ) return cwcLocWindDirections [ 'S' ] ; if ( wd > 191.25 && wd <= 213.75 ) return cwcLocWindDirections [ 'SSW' ] ; if ( wd > 213.75 && wd <= 236.25 ) return cwcLocWindDirections [ 'SW' ] ; if ( wd > 236.25 && wd <= 258.75 ) return cwcLocWindDirections [ 'WSW' ] ; if ( wd > 258.75 && wd <= 281.25 ) return cwcLocWindDirections [ 'W' ] ; if ( wd > 281.25 && wd <= 303.75 ) return cwcLocWindDirections [ 'WNW' ] ; if ( wd > 303.75 && wd <= 326.25 ) return cwcLocWindDirections [ 'NW' ] ; if ( wd > 326.25 && wd <= 348.75 ) return cwcLocWindDirections [ 'NNW' ] ; return null ; } ; export function getMoonIcon ( phase ) { return ( cwcMoonPhaseIcons [ phase . toLowerCase ( ) ] ) ; } export function loadJSON ( full_path_file ) { return new Promise < string > ( ( resolve ) => { setTimeout ( ( ) => { let xobj = new XMLHttpRequest ( ) ; xobj . overrideMimeType ( "application/json" ) ; xobj . open ( 'GET' , full_path_file , true ) ; xobj . onreadystatechange = ( ) => { if ( xobj . readyState === 4 && xobj . status === 200 ) { resolve ( xobj . responseText ) ; } else if ( xobj . status !== 200 ) { let err = "ERROR during json file retrieve: '" + full_path_file + "', readyState: " + xobj . readyState + ", status: " + xobj . status + ", statusText: " + xobj . statusText + ", responseText: " + xobj . responseText ; console . info ( err ) ; } } ; xobj . send ( null ) ; } , 100 ) ; } ) ; } export function numFormat ( stringNumber , fractionDigits = 1 ) { switch ( fractionDigits ) { case 0 : return numberFormat_0dec . format ( parseFloat ( stringNumber ) ) ; break ; case 1 : return numberFormat_1dec . format ( parseFloat ( stringNumber ) ) ; break ; } }