Skip to content

Commit

Permalink
light profiles now pulled from lovelace variable
Browse files Browse the repository at this point in the history
this makes updating the card easier
  • Loading branch information
tcarlsen committed Jun 18, 2019
1 parent 4fe140b commit 2e9afe1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ resources:
url: /local/light-with-profiles.js
```
2. Sadly theres no way for a card to read config files so, if you like the card to be able to show wich profiles are active *(change color)* you will need to add your profiles inside the `light-with-profiles.js` aswell:
2. If you like the card to be able to show wich profiles are active *(change color)* you will need to add your profiles defined in `light_profiles.csv` in your `lovelace.yaml` like so:

```javascript
this.lightProfiles = {
bright: '0.457,0.408,254',
dimmed: '0.457,0.408,77',
nightlight: '0.509,0.411,1'
};
```yaml
light_profiles:
bright: '0.457,0.408,254'
dimmed: '0.457,0.408,77'
nightlight: '0.509,0.411,1'
```

3. Lastly add the custom card:
Expand Down Expand Up @@ -73,4 +72,3 @@ entities:
- entity: light.entreen
- entity: light.kokken
```

38 changes: 27 additions & 11 deletions light-with-profiles.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
LitElement,
html,
css
} from "https://unpkg.com/[email protected]/lit-element.js?module";
import { LitElement, html, css } from "https://unpkg.com/[email protected]/lit-element.js?module";

class LightWithProfiles extends LitElement {
static get properties() {
Expand All @@ -15,12 +11,7 @@ class LightWithProfiles extends LitElement {
constructor() {
super();

this.lightProfiles = {
// Add your profiles here to show what profile is active
// bright: '0.457,0.408,254',
// dimmed: '0.457,0.408,77',
// nightlight: '0.509,0.411,1'
};
this.lightProfiles = {};
}

render() {
Expand Down Expand Up @@ -90,6 +81,13 @@ class LightWithProfiles extends LitElement {
if (!config.entities) {
throw new Error("You need to define entities");
}

const ll = this.getLovelace();

if (ll.config && ll.config.light_profiles) {
this.lightProfiles = ll.config.light_profiles;
}

this.config = config;
}

Expand Down Expand Up @@ -134,6 +132,24 @@ class LightWithProfiles extends LitElement {
}
`;
}
// https://github.com/custom-cards/custom-card-helpers/blob/master/src/get-lovelace.ts
getLovelace() {
let root = document.querySelector('home-assistant');
root = root && root.shadowRoot;
root = root && root.querySelector('home-assistant-main');
root = root && root.shadowRoot;
root = root && root.querySelector('app-drawer-layout partial-panel-resolver');
root = root && root.shadowRoot || root;
root = root && root.querySelector('ha-panel-lovelace');
root = root && root.shadowRoot;
root = root && root.querySelector('hui-root');
if (root) {
const ll = root.lovelace;
ll.current_view = root.___curView;
return ll;
}
return null;
}
}

customElements.define('light-with-profiles', LightWithProfiles);

0 comments on commit 2e9afe1

Please sign in to comment.