Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexible translation keys for logbook binary sensors #22257

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 16 additions & 98 deletions src/data/logbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,114 +206,32 @@ export const localizeStateMessage = (
const isOff = state === BINARY_STATE_OFF;
const device_class = stateObj.attributes.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`, {
if (device_class && (isOn || isOff)) {
return (
localize(
`${LOGBOOK_LOCALIZE_PATH}.${isOn ? "detected_device_classes" : "cleared_device_classes"}.${device_class}`,
{
device_class: autoCaseNoun(
localize(
`component.binary_sensor.entity_component.${device_class}.name`
),
) || device_class,
hass.language
),
});
}
if (isOff) {
return localize(`${LOGBOOK_LOCALIZE_PATH}.cleared_device_class`, {
}
) ||
// 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"}`,
{
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;
Expand Down
62 changes: 61 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,67 @@
"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_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%]"
}
}
},
"entity": {
Expand Down
Loading