generated from custom-cards/boilerplate-card
-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix broken select entity translation labels.
- Loading branch information
1 parent
1255d32
commit 3cfebab
Showing
5 changed files
with
105 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
); | ||
}; |