From f0d45a46dba11c2374bf4efc12a36b424d0b91d6 Mon Sep 17 00:00:00 2001 From: MrBartusek Date: Mon, 9 Aug 2021 17:49:08 +0200 Subject: [PATCH] Add fallback event type when one is not provided --- src/data.js | 18 +- src/integrations/meteoalarm-integration.js | 18 +- src/meteoalarm-card.js | 25 +- src/translations/de.json | 20 +- src/translations/en.json | 18 +- src/translations/es.json | 18 +- src/translations/et.json | 18 +- src/translations/fr.json | 18 +- src/translations/it.json | 18 +- src/translations/nl.json | 18 +- src/translations/pl.json | 18 +- src/translations/schema/schema.json | 472 ++++++++++++--------- 12 files changed, 435 insertions(+), 244 deletions(-) diff --git a/src/data.js b/src/data.js index baf98b5..981805e 100644 --- a/src/data.js +++ b/src/data.js @@ -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; } @@ -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') ]; } @@ -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) diff --git a/src/integrations/meteoalarm-integration.js b/src/integrations/meteoalarm-integration.js index c6a5f96..8ae64ac 100644 --- a/src/integrations/meteoalarm-integration.js +++ b/src/integrations/meteoalarm-integration.js @@ -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') }; } } diff --git a/src/meteoalarm-card.js b/src/meteoalarm-card.js index 8605bdf..bd48815 100644 --- a/src/meteoalarm-card.js +++ b/src/meteoalarm-card.js @@ -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( @@ -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); @@ -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)); + } } } diff --git a/src/translations/de.json b/src/translations/de.json index 8e51ea8..c4ad433 100644 --- a/src/translations/de.json +++ b/src/translations/de.json @@ -11,7 +11,7 @@ "automatic_failed": null, "entity_invalid": null }, - "editor": { + "editor": { "entity": null, "integration": null, "required": null, @@ -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 + } } } diff --git a/src/translations/en.json b/src/translations/en.json index 589faee..725c24f 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -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" + } } } diff --git a/src/translations/es.json b/src/translations/es.json index ff5d392..d60734c 100644 --- a/src/translations/es.json +++ b/src/translations/es.json @@ -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 + } } } diff --git a/src/translations/et.json b/src/translations/et.json index 79fe1c7..949954c 100644 --- a/src/translations/et.json +++ b/src/translations/et.json @@ -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 + } } } diff --git a/src/translations/fr.json b/src/translations/fr.json index 05631fe..1e9648d 100644 --- a/src/translations/fr.json +++ b/src/translations/fr.json @@ -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 + } } } diff --git a/src/translations/it.json b/src/translations/it.json index 21236d6..12b849d 100644 --- a/src/translations/it.json +++ b/src/translations/it.json @@ -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 + } } } diff --git a/src/translations/nl.json b/src/translations/nl.json index 74631d3..8857988 100644 --- a/src/translations/nl.json +++ b/src/translations/nl.json @@ -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 + } } } diff --git a/src/translations/pl.json b/src/translations/pl.json index a6fdff9..c823959 100644 --- a/src/translations/pl.json +++ b/src/translations/pl.json @@ -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" + } } } diff --git a/src/translations/schema/schema.json b/src/translations/schema/schema.json index 7dd1bde..5eccb22 100644 --- a/src/translations/schema/schema.json +++ b/src/translations/schema/schema.json @@ -1,209 +1,269 @@ { - "$schema": "http://json-schema.org/draft-07/schema", - "type": "object", - "required": [ - "$schema", - "common", - "error", - "editor", - "events", - "messages" - ], - "properties": { - "$schema": { - "$id": "#/properties/%24schema", - "type": "string" - }, - "common": { - "$id": "#/properties/common", - "type": "object", - "required": [ - "name", - "description", - "not_available" - ], - "properties": { - "name": { - "$id": "#/properties/common/properties/name", + "$schema": "http://json-schema.org/draft-07/schema", + "type": "object", + "required": [ + "$schema", + "common", + "error", + "editor", + "events", + "messages" + ], + "properties": { + "$schema": { + "$id": "#/properties/%24schema", + "type": "string" + }, + "common": { + "$id": "#/properties/common", + "type": "object", + "required": [ + "name", + "description", + "not_available" + ], + "properties": { + "name": { + "$id": "#/properties/common/properties/name", + "type": ["string", "null"] + }, + "description": { + "$id": "#/properties/common/properties/description", + "type": ["string", "null"] + }, + "not_available": { + "$id": "#/properties/common/properties/not_available", + "type": ["string", "null"] + } + }, + "additionalProperties": false + }, + "error": { + "$id": "#/properties/error", + "type": "object", + "required": [ + "missing_entity", + "invalid_integration", + "automatic_failed", + "entity_invalid" + ], + "properties": { + "missing_entity": { + "$id": "#/properties/error/properties/missing_entity", + "type": ["string", "null"] + }, + "invalid_integration": { + "$id": "#/properties/error/properties/invalid_integration", + "type": ["string", "null"] + }, + "automatic_failed": { + "$id": "#/properties/error/properties/automatic_failed", + "type": ["string", "null"] + }, + "entity_invalid": { + "$id": "#/properties/error/properties/entity_invalid", + "type": ["string", "null"] + } + }, + "additionalProperties": false + }, + "editor": { + "$id": "#/properties/editor", + "type": "object", + "required": [ + "entity", + "integration", + "required", + "automatic", + "recommended", + "override_headline" + ], + "properties": { + "entity": { + "$id": "#/properties/editor/properties/entity", + "type": ["string", "null"] + }, + "integration": { + "$id": "#/properties/editor/properties/integration", + "type": ["string", "null"] + }, + "required": { + "$id": "#/properties/editor/properties/required", + "type": ["string", "null"] + }, + "automatic": { + "$id": "#/properties/editor/properties/automatic", + "type": ["string", "null"] + }, + "recommended": { + "$id": "#/properties/editor/properties/recommended", + "type": ["string", "null"] + }, + "override_headline": { + "$id": "#/properties/editor/properties/override_headline", "type": ["string", "null"] - }, - "description": { - "$id": "#/properties/common/properties/description", - "type": ["string", "null"] - }, - "not_available": { - "$id": "#/properties/common/properties/not_available", - "type": ["string", "null"] - } - }, - "additionalProperties": false - }, - "error": { - "$id": "#/properties/error", - "type": "object", - "required": [ - "missing_entity", - "invalid_integration", - "automatic_failed", - "entity_invalid" - ], - "properties": { - "missing_entity": { - "$id": "#/properties/error/properties/missing_entity", - "type": ["string", "null"] - }, - "invalid_integration": { - "$id": "#/properties/error/properties/invalid_integration", - "type": ["string", "null"] - }, - "automatic_failed": { - "$id": "#/properties/error/properties/automatic_failed", - "type": ["string", "null"] - }, - "entity_invalid": { - "$id": "#/properties/error/properties/entity_invalid", - "type": ["string", "null"] - } - }, - "additionalProperties": false - }, - "editor": { - "$id": "#/properties/editor", - "type": "object", - "required": [ - "entity", - "integration", - "required", - "automatic", - "recommended", - "override_headline" - ], - "properties": { - "entity": { - "$id": "#/properties/editor/properties/entity", - "type": ["string", "null"] - }, - "integration": { - "$id": "#/properties/editor/properties/integration", - "type": ["string", "null"] - }, - "required": { - "$id": "#/properties/editor/properties/required", - "type": ["string", "null"] - }, - "automatic": { - "$id": "#/properties/editor/properties/automatic", - "type": ["string", "null"] - }, - "recommended": { - "$id": "#/properties/editor/properties/recommended", - "type": ["string", "null"] - }, - "override_headline": { - "$id": "#/properties/editor/properties/override_headline", - "type": ["string", "null"] - } - }, - "additionalProperties": false - }, - "events": { - "$id": "#/properties/events", - "type": "object", - "required": [ - "no_warnings", - "wind", - "snow_ice", - "thunderstorms", - "fog", - "hight_temperature", - "low_temperature", - "coastal_event", - "forest_fire", - "avalanches", - "rain", - "flood", - "rain_flood" - ], - "properties": { - "no_warnings": { - "$id": "#/properties/events/properties/no_warnings", - "type": ["string", "null"] - }, - "wind": { - "$id": "#/properties/events/properties/wind", - "type": ["string", "null"] - }, - "snow_ice": { - "$id": "#/properties/events/properties/snow_ice", - "type": ["string", "null"] - }, - "thunderstorms": { - "$id": "#/properties/events/properties/thunderstorms", - "type": ["string", "null"] - }, - "fog": { - "$id": "#/properties/events/properties/fog", - "type": ["string", "null"] - }, - "hight_temperature": { - "$id": "#/properties/events/properties/hight_temperature", - "type": ["string", "null"] - }, - "low_temperature": { - "$id": "#/properties/events/properties/low_temperature", - "type": ["string", "null"] - }, - "coastal_event": { - "$id": "#/properties/events/properties/coastal_event", - "type": ["string", "null"] - }, - "forest_fire": { - "$id": "#/properties/events/properties/forest_fire", - "type": ["string", "null"] - }, - "avalanches": { - "$id": "#/properties/events/properties/avalanches", - "type": ["string", "null"] - }, - "rain": { - "$id": "#/properties/events/properties/rain", - "type": ["string", "null"] - }, - "flood": { - "$id": "#/properties/events/properties/flood", - "type": ["string", "null"] - }, - "rain_flood": { - "$id": "#/properties/events/properties/rain_flood", - "type": ["string", "null"] - } - }, - "additionalProperties": false - }, - "messages": { - "$id": "#/properties/messages", - "type": "object", - "required": [ - "yellow", - "orange", - "red" - ], - "properties": { - "yellow": { - "$id": "#/properties/messages/properties/yellow", - "type": ["string", "null"] - }, - "orange": { - "$id": "#/properties/messages/properties/orange", - "type": ["string", "null"] - }, - "red": { - "$id": "#/properties/messages/properties/red", - "type": ["string", "null"] - } - }, - "additionalProperties": false - } - }, - "additionalProperties": false + } + }, + "additionalProperties": false + }, + "events": { + "$id": "#/properties/events", + "type": "object", + "required": [ + "no_warnings", + "wind", + "snow_ice", + "thunderstorms", + "fog", + "hight_temperature", + "low_temperature", + "coastal_event", + "forest_fire", + "avalanches", + "rain", + "flood", + "rain_flood" + ], + "properties": { + "no_warnings": { + "$id": "#/properties/events/properties/no_warnings", + "type": ["string", "null"] + }, + "wind": { + "$id": "#/properties/events/properties/wind", + "type": ["string", "null"] + }, + "snow_ice": { + "$id": "#/properties/events/properties/snow_ice", + "type": ["string", "null"] + }, + "thunderstorms": { + "$id": "#/properties/events/properties/thunderstorms", + "type": ["string", "null"] + }, + "fog": { + "$id": "#/properties/events/properties/fog", + "type": ["string", "null"] + }, + "hight_temperature": { + "$id": "#/properties/events/properties/hight_temperature", + "type": ["string", "null"] + }, + "low_temperature": { + "$id": "#/properties/events/properties/low_temperature", + "type": ["string", "null"] + }, + "coastal_event": { + "$id": "#/properties/events/properties/coastal_event", + "type": ["string", "null"] + }, + "forest_fire": { + "$id": "#/properties/events/properties/forest_fire", + "type": ["string", "null"] + }, + "avalanches": { + "$id": "#/properties/events/properties/avalanches", + "type": ["string", "null"] + }, + "rain": { + "$id": "#/properties/events/properties/rain", + "type": ["string", "null"] + }, + "flood": { + "$id": "#/properties/events/properties/flood", + "type": ["string", "null"] + }, + "rain_flood": { + "$id": "#/properties/events/properties/rain_flood", + "type": ["string", "null"] + } + }, + "additionalProperties": false + }, + "messages": { + "$id": "#/properties/messages", + "type": "object", + "required": [ + "yellow", + "orange", + "red" + ], + "properties": { + "yellow": { + "$id": "#/properties/messages/properties/yellow", + "type": "object", + "required": [ + "event", + "generic", + "color" + ], + "properties": { + "event": { + "$id": "#/properties/error/properties/messages/event", + "type": ["string", "null"] + }, + "generic": { + "$id": "#/properties/error/properties/messages/generic", + "type": ["string", "null"] + }, + "color": { + "$id": "#/properties/error/properties/messages/color", + "type": ["string", "null"] + }, + "additionalProperties": false + } + }, + "orange": { + "$id": "#/properties/messages/properties/orange", + "type": "object", + "required": [ + "event", + "generic", + "color" + ], + "properties": { + "event": { + "$id": "#/properties/error/properties/messages/event", + "type": ["string", "null"] + }, + "generic": { + "$id": "#/properties/error/properties/messages/generic", + "type": ["string", "null"] + }, + "color": { + "$id": "#/properties/error/properties/messages/color", + "type": ["string", "null"] + }, + "additionalProperties": false + } + }, + "red": { + "$id": "#/properties/messages/properties/red", + "type": "object", + "required": [ + "event", + "generic", + "color" + ], + "properties": { + "event": { + "$id": "#/properties/error/properties/messages/event", + "type": ["string", "null"] + }, + "generic": { + "$id": "#/properties/error/properties/messages/generic", + "type": ["string", "null"] + }, + "color": { + "$id": "#/properties/error/properties/messages/color", + "type": ["string", "null"] + }, + "additionalProperties": false + } + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false } \ No newline at end of file