Skip to content

Commit b02252b

Browse files
authored
Merge pull request #15 from maxwroc/vNext
Release v0.4.0
2 parents 4457757 + 716b8f0 commit b02252b

File tree

10 files changed

+266
-139
lines changed

10 files changed

+266
-139
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/.vscode/
22
/node_modules/
33
/dist/
4-
/src/custom-elements/styles.ts
4+
/src/custom-elements/*-styles.ts

README.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ The aim of this card is to show all the data provided by github integration. You
77

88
![image](https://user-images.githubusercontent.com/8268674/95763370-d904b180-0ca6-11eb-9951-56c8200ee025.png)
99

10+
This component can be used as entity as well
11+
12+
![image](https://user-images.githubusercontent.com/8268674/96303544-7be46500-0ff2-11eb-9a86-16af9c52f1d0.png)
1013

1114
## Configuration
1215

1316
### Card
1417
| Name | Type | Default | Since | Description |
1518
|:-----|:-----|:-----|:-----|:-----|
1619
| title | String | | v0.1.0 | Card header/title text
17-
| entities | [Entity](#entity)[] | **(required)** | v0.1.0 | Collection of entities to display
20+
| entities | [Entity](#entity)[] \| String | **(required)** | v0.1.0 | Collection of entities to display. You can provide simple list of entity_id strings.
1821

1922
[+ Entity Properties](#Entity-Properties) - applied to all entities
2023

@@ -52,6 +55,8 @@ E.g. `"Card version {latest_release_tag}"` becomes `"Card version v1.5.0"`
5255

5356
## Configuration examples
5457

58+
### Card
59+
5560
```yaml
5661
type: 'custom:github-flexi-card'
5762
title: Github projects
@@ -84,6 +89,35 @@ entities:
8489
- name: open_issues
8590
```
8691
92+
### Entity
93+
94+
Note: different type has to be used `custom:github-entity`
95+
96+
![image](https://user-images.githubusercontent.com/8268674/96303544-7be46500-0ff2-11eb-9a86-16af9c52f1d0.png)
97+
98+
```yaml
99+
type: entities
100+
title: Displayed as entity
101+
show_header_toggle: false
102+
entities:
103+
- sensor.home_assistant_v2_db
104+
- type: 'custom:github-entity'
105+
entity: sensor.battery_state_card
106+
secondary_info: 'Released {latest_release_tag}'
107+
url: true
108+
attribute_urls: true
109+
attributes:
110+
- name: views
111+
- name: stargazers
112+
- name: open_issues
113+
- name: clones
114+
- name: forks
115+
- name: open_pull_requests
116+
- sensor.hassio_online
117+
- sensor.last_boot
118+
- sensor.processor_use
119+
```
120+
87121
### Card-level entity properties
88122

89123
Card-level entity properties are useful when you want to have same settings for all of the entities.
@@ -104,9 +138,9 @@ attributes:
104138
- name: forks
105139
- name: open_pull_requests
106140
entities:
107-
- entity: sensor.battery_state_card
108-
- entity: sensor.hideseek_mod
109-
- entity: sensor.urleditorpro
141+
- sensor.battery_state_card
142+
- sensor.hideseek_mod
143+
- sensor.urleditorpro
110144
```
111145

112146
## How to install?

build/prebuild.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,19 @@ const minimizeCss = content => {
3131
}
3232

3333
const compileCss = async () => {
34-
const cssFile = "src/custom-elements/styles.css";
35-
const cssCode = await readFile(cssFile);
36-
37-
return await writeFile(cssFile.replace(".css", ".ts"), 'import { css } from "../lit-element"; const styles = css`' + minimizeCss(cssCode) + '`; export default styles;');
34+
const dir = "src/custom-elements";
35+
const files = fs.readdirSync(dir);
36+
return Promise.all(files.map(async file => {
37+
if (!file.endsWith(".css")) {
38+
return;
39+
}
40+
41+
const sourceFilePath = `${dir}/${file}`;
42+
const targetFilePath = sourceFilePath.replace(".css", "-styles.ts");
43+
44+
const cssCode = await readFile(sourceFilePath);
45+
await writeFile(targetFilePath, 'import { css } from "../lit-element"; const styles = css`' + minimizeCss(cssCode) + '`; export default styles;');
46+
}));
3847
};
3948

4049
// Updates version printed in console window

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "github-flexi-card",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "Home assistant github card",
55
"main": "dist/github-flexi-card.js",
66
"scripts": {

src/custom-elements/card.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
.truncate {
3+
white-space: nowrap;
4+
text-overflow: ellipsis;
5+
overflow: hidden;
6+
}

src/custom-elements/card.ts

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,120 @@
11
import { HomeAssistant } from "../ha-types";
22
import { html, LitElement } from "../lit-element";
3-
import { ICardConfig } from "../types";
3+
import { ICardConfig, IEntityConfig } from "../types";
44
import { GithubEntity } from "./entity";
5-
import styles from "./styles";
5+
import styles from "./card-styles";
66

77
export class GithubFlexiCard extends LitElement {
88

99
private cardTitle: string = "";
1010

1111
private entities: GithubEntity[] = [];
1212

13+
private cardSize = 0;
14+
1315
/**
1416
* CSS for the card
1517
*/
1618
static get styles() {
1719
return styles;
1820
}
1921

22+
/**
23+
* List of properties which trigger update when changed
24+
*/
2025
static get properties() {
2126
return {
2227
cardTitle: { type: String },
2328
entities: { type: Array },
2429
};
2530
}
2631

32+
/**
33+
* Called whenever HS state is updated
34+
*/
2735
set hass(hass: HomeAssistant) {
2836
this.entities.forEach(entity => entity.hass = hass);
2937
}
3038

39+
/**
40+
* Called whenever card config is updated
41+
*/
3142
setConfig(cardConfig: ICardConfig) {
3243
this.cardTitle = cardConfig.title;
3344

34-
if (this.entities.length != cardConfig.entities.length) {
35-
this.entities = cardConfig.entities.map(entityConf => {
36-
const elem = document.createElement("github-entity") as GithubEntity;
37-
38-
// we have to make a copy as the original one is immutable
39-
const updatableConfig = { ...entityConf };
45+
this.cardSize = 0;
4046

41-
// if property is not defined take the card-level one
42-
updatableConfig.attributes = updatableConfig.attributes || cardConfig.attributes;
43-
updatableConfig.attribute_urls = updatableConfig.attribute_urls !== undefined ? updatableConfig.attribute_urls : cardConfig.attribute_urls;
44-
updatableConfig.icon = updatableConfig.icon || cardConfig.icon;
45-
updatableConfig.name = updatableConfig.name || cardConfig.name;
46-
updatableConfig.secondary_info = updatableConfig.secondary_info || cardConfig.secondary_info;
47-
updatableConfig.url = updatableConfig.url !== undefined ? updatableConfig.url : cardConfig.url;
47+
if (this.cardTitle) {
48+
this.cardSize++;
49+
}
4850

49-
elem.setConfig(updatableConfig);
51+
if (this.entities.length != cardConfig.entities.length) {
52+
this.entities = cardConfig.entities.map(e => getEntityConfig(e, cardConfig)).map(entityConf => {
53+
const elem = document.createElement("github-entity") as GithubEntity;
54+
elem.setConfig(entityConf);
55+
this.cardSize++;
5056
return elem;
5157
})
5258
}
5359
else {
54-
this.entities.forEach((entity, index) => entity.setConfig(cardConfig.entities[index]));
60+
this.entities.forEach((entity, index) => entity.setConfig(getEntityConfig(cardConfig.entities[index], cardConfig)));
5561
}
5662
}
5763

64+
/**
65+
* Gets the height of your card.
66+
*
67+
* Home Assistant uses this to automatically distribute all cards over
68+
* the available columns. One is equal 50px.
69+
*/
70+
getCardSize() {
71+
return this.cardSize;
72+
}
73+
74+
/**
75+
* Called when element rendering was triggered
76+
*/
5877
render() {
5978
return html`
6079
<ha-card>
61-
${this.getHeader()}
80+
${this.cardTitle && header(this.cardTitle)}
6281
<div class="card-content">
6382
${this.entities}
6483
</div>
6584
</ha-card>
6685
`;
6786
}
87+
}
6888

69-
private getHeader() {
70-
return this.cardTitle && html`
71-
<div class="card-header">
72-
<div class="truncate">
73-
${this.cardTitle}
74-
</div>
75-
</div>
76-
`;
77-
}
89+
/**
90+
* Header/title view
91+
*/
92+
const header = (title: string) => html`
93+
<div class="card-header">
94+
<div class="truncate">
95+
${title}
96+
</div>
97+
</div>
98+
`;
99+
100+
/**
101+
* Converts string entry to proper config obj and applies card-level settings
102+
*/
103+
const getEntityConfig = (configEntry: IEntityConfig | string, cardConfig: ICardConfig): IEntityConfig => {
104+
105+
const entityConfig = typeof configEntry != "string" ?
106+
// we have to make a copy as the original one is immutable
107+
{ ...configEntry } :
108+
// construct simple config entry
109+
{ entity: configEntry };
110+
111+
// if property is not defined take the card-level one
112+
entityConfig.attributes = entityConfig.attributes || cardConfig.attributes;
113+
entityConfig.attribute_urls = entityConfig.attribute_urls !== undefined ? entityConfig.attribute_urls : cardConfig.attribute_urls;
114+
entityConfig.icon = entityConfig.icon || cardConfig.icon;
115+
entityConfig.name = entityConfig.name || cardConfig.name;
116+
entityConfig.secondary_info = entityConfig.secondary_info || cardConfig.secondary_info;
117+
entityConfig.url = entityConfig.url !== undefined ? entityConfig.url : cardConfig.url;
118+
119+
return entityConfig;
78120
}

src/custom-elements/styles.css renamed to src/custom-elements/entity.css

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
2-
31
.truncate {
42
white-space: nowrap;
53
text-overflow: ellipsis;
64
overflow: hidden;
75
}
86

7+
.clickable {
8+
cursor: pointer;
9+
}
10+
911
.entity-spacing:first-child {
1012
margin-top: 0;
1113
}
@@ -62,8 +64,4 @@
6264
}
6365
.compact-view .state > * {
6466
margin: 0;
65-
}
66-
67-
.clickable {
68-
cursor: pointer;
6967
}

0 commit comments

Comments
 (0)