-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88c4957
commit 240a1e7
Showing
13 changed files
with
131 additions
and
24 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
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
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,23 @@ | ||
import { HomeAssistant } from "custom-card-helpers"; | ||
import { HomeAssistantEntity, RoomCardConfig, RoomCardEntity, RoomCardIcon } from "./types/room-card-types"; | ||
import { evalTemplate } from "./util"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
export const templateStyling = (stateObj: HomeAssistantEntity, config: RoomCardEntity | RoomCardConfig, hass: HomeAssistant) : Function => { | ||
const icon = (config.icon as RoomCardIcon); | ||
|
||
return icon?.template?.styles !== undefined ? evalTemplate(hass, stateObj, icon.template.styles) : null; | ||
} | ||
|
||
export const mapTemplate = (entity: RoomCardEntity, config: RoomCardConfig) => { | ||
if(entity !== undefined && entity.template) { | ||
const templatesWithMatchingName = config.templates.filter(template => template.name === entity.template); | ||
if(templatesWithMatchingName.length > 0) { | ||
const templateFromConfig = templatesWithMatchingName[0]; | ||
|
||
return { stateObj: entity.stateObj, ...entity, ...templateFromConfig.template }; | ||
} | ||
} | ||
|
||
return entity; | ||
} |
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,61 @@ | ||
import { createMock } from 'ts-auto-mock'; | ||
import { mapTemplate } from '../../src/template'; | ||
import { RoomCardConfig, RoomCardEntity } from '../../src/types/room-card-types'; | ||
|
||
describe('Testing template file function mapTemplate', () => { | ||
const entity = createMock<RoomCardEntity>(); | ||
entity.template = 'test_template'; | ||
|
||
test('Passing entity and config with templates should map properties', () => { | ||
|
||
const config: RoomCardConfig = { | ||
entityIds: [], | ||
type: '', | ||
templates: [{ | ||
name: 'test_template', | ||
template: { | ||
'show_icon': true, | ||
'show_state': true | ||
} | ||
}] | ||
} | ||
|
||
const expectedEntity: RoomCardEntity = { stateObj: entity.stateObj, show_icon: true, show_state: true }; | ||
expect(mapTemplate(entity, config)).toMatchObject(expectedEntity); | ||
}), | ||
test('Passing entity and config with complex templates should map properties', () => { | ||
|
||
const config: RoomCardConfig = { | ||
entityIds: [], | ||
type: '', | ||
templates: [{ | ||
name: 'test_template', | ||
template: { | ||
'show_icon': true, | ||
'show_state': true, | ||
'icon': { | ||
conditions: [{ | ||
condition: 'above', | ||
value: '1', | ||
icon: 'mdi:test' | ||
}] | ||
} | ||
} | ||
}] | ||
} | ||
|
||
const expectedEntity: RoomCardEntity = { | ||
stateObj: entity.stateObj, | ||
show_icon: true, | ||
show_state: true, | ||
icon: { | ||
conditions: [{ | ||
condition: 'above', | ||
value: '1', | ||
icon: 'mdi:test' | ||
}] | ||
} | ||
}; | ||
expect(mapTemplate(entity, config)).toMatchObject(expectedEntity); | ||
}) | ||
}); |
4 changes: 2 additions & 2 deletions
4
tests/entity/templateStyling.test.ts → tests/template/templateStyling.test.ts
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