Skip to content

Commit

Permalink
Add fallback event type when one is not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBartusek committed Aug 9, 2021
1 parent 9aa6761 commit f0d45a4
Show file tree
Hide file tree
Showing 12 changed files with 435 additions and 244 deletions.
18 changes: 13 additions & 5 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ class Event

class Level
{
constructor(name, color, translationKey)
constructor(name, severities, color, translationKey)
{
this.name = name;
this.severities = severities;
this.color = color;
this.translationKey = translationKey;
}
Expand All @@ -37,16 +38,18 @@ export default class Data
new Event('Extreme high temperature', 'thermometer', 'events.hight_temperature'),
new Event('Extreme low temperature', 'snowflake', 'events.low_temperature'),
new Event('Wind', 'windsock', 'events.wind'),
new Event('Fog', 'weather-fog', 'events.fog')
new Event('Fog', 'weather-fog', 'events.fog'),

new Event('Unknown Event', 'alert-circle-outline', null),
];
}

static get levels()
{
return [
new Level('Yellow', '#ff9800', 'messages.yellow'),
new Level('Orange', '#EE5A24', 'messages.orange'),
new Level('Red', '#db4437', 'messages.red')
new Level('Yellow', [ 'Moderate' ], '#ff9800', 'messages.yellow'),
new Level('Orange', [ 'Severe', 'Minor' ], '#EE5A24', 'messages.orange'),
new Level('Red', [ 'High', 'Extreme'], '#db4437', 'messages.red')
];
}

Expand All @@ -55,6 +58,11 @@ export default class Data
return this.events.find((e) => e.name === name);
}

static getLevelBySeverity(severity)
{
return this.levels.find((e) => e.severities.includes(severity));
}

// Find event with the highest id
// Usefull for integrations that provides more than one warning
static filterEvents(events)
Expand Down
18 changes: 15 additions & 3 deletions src/integrations/meteoalarm-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,28 @@ export class MeteoAlarmIntegration
const {
event,
headline,
severity,
awareness_type: awarenessType,
awareness_level: awarenessLevel,
} = entity.attributes;

if(awarenessType == undefined || awarenessLevel == undefined) return;
let type = null;
let level = null;

if(awarenessType != undefined)
{
type = this.eventTypes[Number(awarenessType.split(';')[0]) - 1];
}

if(awarenessLevel != undefined)
{
level = Data.levels[Number(awarenessLevel.split(';')[0]) - 2];
}

return {
headline: event || headline,
awarenessLevel: Data.levels[Number(awarenessLevel.split(';')[0]) - 2],
awarenessType: this.eventTypes[Number(awarenessType.split(';')[0]) - 1]
awarenessLevel: level || Data.getLevelBySeverity(severity),
awarenessType: type || Data.getEventByName('Unknown Event')
};
}
}
25 changes: 20 additions & 5 deletions src/meteoalarm-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class MeteoalarmCard extends LitElement
...this.integration.getResult(entity),
};

// Handle entity parsing errors
if(result.awarenessLevel == undefined || result.awarenessType == undefined)
{
throw Error(
Expand All @@ -201,9 +202,9 @@ class MeteoalarmCard extends LitElement
);
}

// Generate Headlines
if(result.headline === undefined || this.overrideHeadline)
{
// If headline is not issued, generate default one
result.headline = this.generateHeadline(result.awarenessType, result.awarenessLevel);
}
result.headlineNarrow = this.generateHeadline(result.awarenessType, result.awarenessLevel, true);
Expand All @@ -214,14 +215,28 @@ class MeteoalarmCard extends LitElement

generateHeadline(awarenessType, awarenessLevel, narrow = false)
{
if(narrow)
if(awarenessType.name == 'Unknown Event')
{
const awareness = localize(awarenessType.translationKey);
return awareness.charAt(0).toUpperCase() + awareness.slice(1);
if(narrow)
{
return localize(awarenessLevel.translationKey).color;
}
else
{
return localize(awarenessLevel.translationKey).generic;
}
}
else
{
return localize(awarenessLevel.translationKey).replace('{0}', localize(awarenessType.translationKey));
if(narrow)
{
const awareness = localize(awarenessType.translationKey);
return awareness.charAt(0).toUpperCase() + awareness.slice(1);
}
else
{
return localize(awarenessLevel.translationKey).event.replace('{0}', localize(awarenessType.translationKey));
}
}
}

Expand Down
20 changes: 16 additions & 4 deletions src/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"automatic_failed": null,
"entity_invalid": null
},
"editor": {
"editor": {
"entity": null,
"integration": null,
"required": null,
Expand All @@ -35,8 +35,20 @@
"rain_flood": "Regenüberflutung"
},
"messages": {
"yellow": "{0} der Warnstufe Gelb",
"orange": "{0} der Warnstufe Orange",
"red": "{0} der Warnstufe Rot"
"yellow": {
"event": "{0} der Warnstufe Gelb",
"generic": null,
"color": null
},
"orange": {
"event": "{0} der Warnstufe Orange",
"generic": null,
"color": null
},
"red": {
"event": "{0} der Warnstufe Rot",
"generic": null,
"color": null
}
}
}
18 changes: 15 additions & 3 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@
"rain_flood": "rain flood"
},
"messages": {
"yellow": "Yellow {0} warning",
"orange": "Orange {0} warning",
"red": "Red {0} warning"
"yellow": {
"event": "Yellow {0} warning",
"generic": "Yellow warning",
"color": "Yellow"
},
"orange": {
"event": "Orange {0} warning",
"generic": "Orange warning",
"color": "Orange"
},
"red": {
"event": "Red {0} warning",
"generic": "Red warning",
"color": "Red"
}
}
}
18 changes: 15 additions & 3 deletions src/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@
"rain_flood": "inundación de lluvia"
},
"messages": {
"yellow": "Alerta amarilla por {0}",
"orange": "Alerta naranja por {0}",
"red": "Alerta roja {0}"
"yellow": {
"event": "Alerta amarilla por {0}",
"generic": null,
"color": null
},
"orange": {
"event": "Alerta naranja por {0}",
"generic": null,
"color": null
},
"red": {
"event": "Alerta roja {0}",
"generic": null,
"color": null
}
}
}
18 changes: 15 additions & 3 deletions src/translations/et.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@
"rain_flood": "paduvihm"
},
"messages": {
"yellow": "Kollane hoiatus {0}",
"orange": "Oranž hoiatus {0}",
"red": "Punane hoiatus {0}"
"yellow": {
"event": "Kollane hoiatus {0}",
"generic": null,
"color": null
},
"orange": {
"event": "Oranž hoiatus {0}",
"generic": null,
"color": null
},
"red": {
"event": "Punane hoiatus {0}",
"generic": null,
"color": null
}
}
}
18 changes: 15 additions & 3 deletions src/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@
"rain_flood": "pluie/inondation"
},
"messages": {
"yellow": "Alerte jaune {0}",
"orange": "Alerte orange {0}",
"red": "Alerte rouge {0}"
"yellow": {
"event": "Alerte jaune {0}",
"generic": null,
"color": null
},
"orange": {
"event": "Alerte orange {0}",
"generic": null,
"color": null
},
"red": {
"event": "Alerte rouge {0}",
"generic": null,
"color": null
}
}
}
18 changes: 15 additions & 3 deletions src/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@
"rain_flood": "acquazzoni"
},
"messages": {
"yellow": "Allarme Giallo {0}",
"orange": "Allarme Arancione {0}",
"red": "Allarme Rosso {0}"
"yellow": {
"event": "Allarme Giallo {0}",
"generic": null,
"color": null
},
"orange": {
"event": "Allarme Arancione {0}",
"generic": null,
"color": null
},
"red": {
"event": "Allarme Rosso {0}",
"generic": null,
"color": null
}
}
}
18 changes: 15 additions & 3 deletions src/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@
"rain_flood": "wateroverlast"
},
"messages": {
"yellow": "Waarschuwing geel {0}",
"orange": "Waarschuwing oranje {0}",
"red": "Waarschuwing rood {0}"
"yellow": {
"event": "Waarschuwing geel {0}",
"generic": null,
"color": null
},
"orange": {
"event": "Waarschuwing oranje {0}",
"generic": null,
"color": null
},
"red": {
"event": "Waarschuwing rood {0}",
"generic": null,
"color": null
}
}
}
18 changes: 15 additions & 3 deletions src/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@
"rain_flood": "ulewy"
},
"messages": {
"yellow": "Żółty alert na {0}",
"orange": "Pomarańczowy alert na {0}",
"red": "Czerwony alert na {0}"
"yellow": {
"event": "Żółty alert na {0}",
"generic": "Żółty alert",
"color": "Żółty"
},
"orange": {
"event": "Pomarańczowy alert na {0}",
"generic": "Pomarańczowy alert",
"color": "Pomarańczowy"
},
"red": {
"event": "Czerwony alert na {0}",
"generic": "Czerwony alert",
"color": "Czerwony"
}
}
}
Loading

0 comments on commit f0d45a4

Please sign in to comment.