Skip to content

Commit

Permalink
Adds option for external override of alarm time
Browse files Browse the repository at this point in the history
  • Loading branch information
chaptergy committed Feb 14, 2023
1 parent 687b5ba commit 79ee314
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 49 deletions.
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ resources:
## Options
| Name | Type | Requirement | Description |
| --------------- | ------ | ------------ | -------------------------------------------- |
| type | string | **Required** | `custom:lightalarm-card` |
| name | string | Optional | Card title |
| time_entity | string | **Required** | `input_datetime` entity to select alarm time |
| mode_entity | string | **Required** | `input_select` entity to select alarm mode |
| duration_entity | string | **Required** | `input_number` entity to set fade duration |
| Name | Type | Requirement | Description |
| -------------------- | ------ | ------------ | -------------------------------------------- |
| type | string | **Required** | `custom:lightalarm-card` |
| name | string | Optional | Card title |
| time_entity | string | **Required** | `input_datetime` entity to select alarm time |
| override_time_entity | string | optional | `sensor` entity to override the alarm time |
| mode_entity | string | **Required** | `input_select` entity to select alarm mode |
| duration_entity | string | **Required** | `input_number` entity to set fade duration |

## Force Native Timepicker

Expand Down Expand Up @@ -167,7 +168,7 @@ sequence:
- 91
- 36
brightness: 255
transition: "{{ duration | float | multiply(60) }}"
transition: '{{ duration | float | multiply(60) }}'
- event: lightalarm_triggered
- condition: state
entity_id: input_select.lightalarm_mode
Expand All @@ -183,3 +184,30 @@ This script then switches on the lap to full brightness, while the transition pr
Then a custom event `lightalarm_triggered` is fired, so we could create more automations listening for this event, like the bathroom heater switching on. You can remove this line if you do not plan on triggering anything else.

Now the last thing to check is if the mode was set to `Once Only` and if it was, set it to `Off`. A condition in a script exits the script if the condition fails, and the following things are only executed if it succeeds.

As a final touch you can also set your phones alarm time to override the manual alarm time. You can create a sensor which has the automatic alarm time, which is either your phones next alarm if available, otherwise the manual alarm time.

```yml
template:
- trigger:
- platform: time_pattern
minutes: 0
- platform: state
entity_id:
- sensor.app_next_alarm
- input_datetime.lightalarm_time
- platform: homeassistant
event: start
sensor:
- name: Automatic Lightalarm Time
unique_id: "automatic_lightalarm_time"
state: >-
{% set nextAlarm = states("sensor.app_next_alarm") | as_datetime %}
{% if nextAlarm != None and nextAlarm - now() < timedelta(hours=16) %}
{{ (nextAlarm | as_local).strftime("%H:%M:%S") }}
{% else %}
{{ states("input_datetime.lightalarm_time") }}
{% endif %}
```

You can use this entity as the override entity in your card, and make sure to update the other template sensor (`lightalarm_time_start`) to use this value instead of the manual value.
97 changes: 63 additions & 34 deletions dist/lightalarm-card.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lightalarm-card",
"version": "3.1.1",
"version": "4.0.0",
"description": "Lovelace Lightalarm Card",
"keywords": [
"home-assistant",
Expand Down
2 changes: 1 addition & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const CARD_VERSION = '3.1.1';
export const CARD_VERSION = '4.0.0';
31 changes: 28 additions & 3 deletions src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export class LightalarmCardEditor extends LitElement implements LovelaceCardEdit
return '';
}

get _override_time_entity(): string {
if (this._config) {
return this._config.override_time_entity || '';
}

return '';
}

get _mode_entity(): string {
if (this._config) {
return this._config.mode_entity || '';
Expand Down Expand Up @@ -70,13 +78,16 @@ export class LightalarmCardEditor extends LitElement implements LovelaceCardEdit

// You can restrict on domain type
const time_entities = Object.keys(this.hass.states).filter(
eid => eid.substr(0, eid.indexOf('.')) === 'input_datetime',
eid => eid.substring(0, eid.indexOf('.')) === 'input_datetime',
);
const override_time_entities = Object.keys(this.hass.states).filter(
eid => eid.substring(0, eid.indexOf('.')) === 'sensor',
);
const mode_entities = Object.keys(this.hass.states).filter(
eid => eid.substr(0, eid.indexOf('.')) === 'input_select',
eid => eid.substring(0, eid.indexOf('.')) === 'input_select',
);
const duration_entities = Object.keys(this.hass.states).filter(
eid => eid.substr(0, eid.indexOf('.')) === 'input_number',
eid => eid.substring(0, eid.indexOf('.')) === 'input_number',
);

return html`
Expand Down Expand Up @@ -104,6 +115,20 @@ export class LightalarmCardEditor extends LitElement implements LovelaceCardEdit
})}
</ha-select>
<br />
<ha-select
label="${localize('config.override_time_entity')}"
@selected=${this._valueChanged}
.configValue=${'override_time_entity'}
.value=${this._override_time_entity}
class="padding-bottom full-width"
>
${override_time_entities.map(entity => {
return html`
<mwc-list-item .value="${entity}">${entity}</mwc-list-item>
`;
})}
</ha-select>
<br />
<ha-select
label="${localize('config.mode_entity')}"
@selected=${this._valueChanged}
Expand Down
31 changes: 29 additions & 2 deletions src/lightalarm-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,19 @@ export class LightalarmCard extends LitElement {
`;
}

const overrideTime = this.config.override_time_entity
? this.hass.states[this.config.override_time_entity]?.state
: undefined;
const hasOverride =
overrideTime &&
overrideTime !== 'unknown' &&
overrideTime !== 'unavailable' &&
timeStateObj.state !== overrideTime;

return html`
<ha-card .header=${this.config.name} tabindex="0">
<div class="lightalarm-wrapper" id="lightalarm-wrapper">
<div class="alarm-time-and-decorator-wrap">
<div class="alarm-time-and-decorator-wrap${hasOverride ? ' has-override' : ''}">
<svg viewBox="0 0 24 24" class="alarm-time-decorator">
<path
d="M12,20A7,7 0 0,1 5,13A7,7 0 0,1 12,6A7,7 0 0,1 19,13A7,7 0 0,1 12,20M12,4A9,9 0 0,0 3,13A9,9 0 0,0 12,22A9,9 0 0,0 21,13A9,9 0 0,0 12,4M7.88,3.39L6.6,1.86L2,5.71L3.29,7.24L7.88,3.39M22,5.72L17.4,1.86L16.11,3.39L20.71,7.25L22,5.72Z"
Expand All @@ -131,7 +140,7 @@ export class LightalarmCard extends LitElement {
<div class="alarm-time-wrap">
<span class="alarm-time-display">
${timeStateObj.state === 'unknown'
? '07:00'
? '--:--'
: ('0' + timeStateObj.attributes.hour).slice(-2) +
':' +
('0' + timeStateObj.attributes.minute).slice(-2)}
Expand Down Expand Up @@ -189,6 +198,12 @@ export class LightalarmCard extends LitElement {
/>
</div>
</div>
${hasOverride &&
html`
<div class="alarm-time-override">
<span>${overrideTime?.substring(0, 5)}</span>
</div>
`}
</div>
<div class="alarm-properties-wrap">
Expand Down Expand Up @@ -278,6 +293,12 @@ export class LightalarmCard extends LitElement {
opacity: 0.07;
}
.lightalarm-wrapper .alarm-time-override {
text-align: center;
position: relative;
top: 35%;
font-size: 1.3rem;
}
.lightalarm-wrapper .alarm-time-wrap {
position: relative;
font-size: 1.4rem;
Expand All @@ -287,6 +308,12 @@ export class LightalarmCard extends LitElement {
top: 50%;
margin-top: -0.2em;
}
.lightalarm-wrapper .has-override .alarm-time-wrap {
top: 64%;
font-size: 0.9rem;
text-decoration: line-through;
color: var(--secondary-text-color);
}
.lightalarm-wrapper .alarm-time-display {
box-sizing: border-box;
position: relative;
Expand Down
1 change: 1 addition & 0 deletions src/localize/languages/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"optional": "Optional",
"name": "Titel",
"time_entity": "Zeit-Entität",
"override_time_entity": "Zeit-Überschreibungs-Entität",
"mode_entity": "Modus-Entität",
"duration_entity": "Dauer-Entität",
"invalid_configuration": "Ungültige Konfiguration",
Expand Down
1 change: 1 addition & 0 deletions src/localize/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"optional": "Optional",
"name": "Title",
"time_entity": "Time Entity",
"override_time_entity": "Time Override Entity",
"mode_entity": "Mode Entity",
"duration_entity": "Duration Entity",
"invalid_configuration": "Invalid configuration",
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface LightalarmCardConfig {
type: string;
name?: string;
time_entity: string;
override_time_entity?: string;
mode_entity: string;
duration_entity: string;
}

0 comments on commit 79ee314

Please sign in to comment.