From 73c5d6630fbcc6dec63cbfbbd087fad36b8d0eda Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Wed, 6 Nov 2024 10:18:11 +0100 Subject: [PATCH] Revert "More flexible translation keys for logbook binary sensors (#22257)" This reverts commit df3e4576db3aeb191077044cea9c82ea4ed5d9ab. --- src/data/logbook.ts | 114 +++++++++++++++++++++++++++++++++------ src/translations/en.json | 62 +-------------------- 2 files changed, 99 insertions(+), 77 deletions(-) diff --git a/src/data/logbook.ts b/src/data/logbook.ts index 1762ffdd0401..712680ae9073 100644 --- a/src/data/logbook.ts +++ b/src/data/logbook.ts @@ -218,32 +218,114 @@ export const localizeStateMessage = ( const isOff = state === BINARY_STATE_OFF; const device_class = stateObj.attributes.device_class; - if (device_class && (isOn || isOff)) { - return ( - localize( - `${LOGBOOK_LOCALIZE_PATH}.${isOn ? "detected_device_classes" : "cleared_device_classes"}.${device_class}`, - { + switch (device_class) { + case "battery": + if (isOn) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_low`); + } + if (isOff) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_normal`); + } + break; + + case "connectivity": + if (isOn) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_connected`); + } + if (isOff) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_disconnected`); + } + break; + + case "door": + case "garage_door": + case "opening": + case "window": + if (isOn) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_opened`); + } + if (isOff) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_closed`); + } + break; + + case "lock": + if (isOn) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_unlocked`); + } + if (isOff) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_locked`); + } + break; + + case "plug": + if (isOn) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_plugged_in`); + } + if (isOff) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_unplugged`); + } + break; + + case "presence": + if (isOn) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_at_home`); + } + if (isOff) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_away`); + } + break; + + case "safety": + if (isOn) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_unsafe`); + } + if (isOff) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.was_safe`); + } + break; + + case "cold": + case "gas": + case "heat": + case "moisture": + case "motion": + case "occupancy": + case "power": + case "problem": + case "smoke": + case "sound": + case "vibration": + if (isOn) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.detected_device_class`, { device_class: autoCaseNoun( localize( `component.binary_sensor.entity_component.${device_class}.name` - ) || device_class, + ), hass.language ), - } - ) || - // If there's no key for a specific device class, fallback to generic string - localize( - `${LOGBOOK_LOCALIZE_PATH}.${isOn ? "detected_device_class" : "cleared_device_class"}`, - { + }); + } + if (isOff) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.cleared_device_class`, { device_class: autoCaseNoun( localize( `component.binary_sensor.entity_component.${device_class}.name` - ) || device_class, + ), hass.language ), - } - ) - ); + }); + } + break; + + case "tamper": + if (isOn) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.detected_tampering`); + } + if (isOff) { + return localize(`${LOGBOOK_LOCALIZE_PATH}.cleared_tampering`); + } + break; } break; diff --git a/src/translations/en.json b/src/translations/en.json index 9ea5723459ff..0c3cd4698d8f 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -486,67 +486,7 @@ "cleared_tampering": "cleared tampering", "detected_event": "{event_type} event detected", "detected_event_no_type": "detected an event", - "detected_unknown_event": "detected an unknown event", - "detected_device_classes": { - "battery": "[%key:ui::components::logbook::messages::was_low%]", - "battery_charging": "started charging", - "carbon_monoxide": "[%key:ui::components::logbook::messages::detected_device_class%]", - "cold": "[%key:ui::components::logbook::messages::detected_device_class%]", - "connectivity": "[%key:ui::components::logbook::messages::was_connected%]", - "door": "[%key:ui::components::logbook::messages::was_opened%]", - "garage_door": "[%key:ui::components::logbook::messages::was_opened%]", - "gas": "[%key:ui::components::logbook::messages::detected_device_class%]", - "heat": "[%key:ui::components::logbook::messages::detected_device_class%]", - "light": "[%key:ui::components::logbook::messages::detected_device_class%]", - "lock": "[%key:ui::components::logbook::messages::was_unlocked%]", - "moisture": "[%key:ui::components::logbook::messages::detected_device_class%]", - "motion": "[%key:ui::components::logbook::messages::detected_device_class%]", - "moving": "[%key:ui::components::logbook::messages::detected_device_class%]", - "occupancy": "[%key:ui::components::logbook::messages::detected_device_class%]", - "opening": "[%key:ui::components::logbook::messages::was_opened%]", - "plug": "[%key:ui::components::logbook::messages::was_plugged_in%]", - "power": "[%key:ui::components::logbook::messages::detected_device_class%]", - "presence": "[%key:ui::components::logbook::messages::was_at_home%]", - "problem": "[%key:ui::components::logbook::messages::detected_device_class%]", - "running": "[%key:ui::components::logbook::messages::detected_device_class%]", - "safety": "[%key:ui::components::logbook::messages::was_unsafe%]", - "smoke": "[%key:ui::components::logbook::messages::detected_device_class%]", - "sound": "[%key:ui::components::logbook::messages::detected_device_class%]", - "tamper": "[%key:ui::components::logbook::messages::detected_tampering%]", - "update": "[%key:ui::components::logbook::messages::detected_device_class%]", - "vibration": "[%key:ui::components::logbook::messages::detected_device_class%]", - "window": "[%key:ui::components::logbook::messages::was_opened%]" - }, - "cleared_device_classes": { - "battery": "[%key:ui::components::logbook::messages::was_normal%]", - "battery_charging": "stopped charging", - "carbon_monoxide": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "cold": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "connectivity": "[%key:ui::components::logbook::messages::was_disconnected%]", - "door": "[%key:ui::components::logbook::messages::was_closed%]", - "garage_door": "[%key:ui::components::logbook::messages::was_closed%]", - "gas": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "heat": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "light": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "lock": "[%key:ui::components::logbook::messages::was_locked%]", - "moisture": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "motion": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "moving": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "occupancy": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "opening": "[%key:ui::components::logbook::messages::was_closed%]", - "plug": "[%key:ui::components::logbook::messages::was_unplugged%]", - "power": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "presence": "[%key:ui::components::logbook::messages::was_away%]", - "problem": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "running": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "safety": "[%key:ui::components::logbook::messages::was_safe%]", - "smoke": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "sound": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "tamper": "[%key:ui::components::logbook::messages::cleared_tampering%]", - "update": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "vibration": "[%key:ui::components::logbook::messages::cleared_device_class%]", - "window": "[%key:ui::components::logbook::messages::was_closed%]" - } + "detected_unknown_event": "detected an unknown event" } }, "entity": {