Fix broken select entity translation labels.

This commit is contained in:
Dermot Duffy
2023-03-04 13:51:58 -08:00
parent 1255d3269a
commit 3cfebabd1b
5 changed files with 105 additions and 15 deletions
+1
View File
@@ -6,6 +6,7 @@ export const entitySchema = z.object({
entity_id: z.string(),
hidden_by: z.string().nullable(),
platform: z.string(),
translation_key: z.string().nullable(),
unique_id: z.string().optional(),
});
export type Entity = z.infer<typeof entitySchema>;
+49
View File
@@ -0,0 +1,49 @@
import { computeDomain, HomeAssistant } from 'custom-card-helpers';
import { HassEntity } from 'home-assistant-js-websocket';
import { Entity } from './entity-registry/types';
/**
* Get the translation of an entity state. Inspired by:
* https://github.com/home-assistant/frontend/blob/dev/src/common/entity/compute_state_display.ts#L204-L218
*
* This may no longer be necessary to custom implement if `custom-card-helpers`
* is updated to reflect how the Home Assistant frontend now [as of 2023-03-04]
* computes state display (e.g. supports usage of `translation_key`).
*
* https://github.com/custom-cards/custom-card-helpers/blob/master/src/compute-state-display.ts
*
*/
export const getEntityStateTranslation = (
hass: HomeAssistant,
entityID: string,
options?: {
entity?: Entity;
state?: string;
},
): string | null => {
const stateObj: HassEntity | undefined = hass.states[entityID];
const state = options?.state ? options.state : stateObj ? stateObj.state : null;
if (!state) {
return null;
}
const domain = computeDomain(entityID);
const attributes = stateObj ? stateObj.attributes : null;
return (
// Return the translation_key translation.
(options?.entity?.translation_key &&
hass.localize(
`component.${options.entity.platform}.entity.${domain}` +
`.${options.entity.translation_key}.state.${state}`,
)) ||
// Return device class translation
(attributes?.device_class &&
hass.localize(`component.${domain}.state.${attributes.device_class}.${state}`)) ||
// Return default translation
hass.localize(`component.${domain}.state._.${state}`) ||
// We don't know! Return the raw state.
state
);
};