Skip to content

Commit

Permalink
improve workday sensor support
Browse files Browse the repository at this point in the history
  • Loading branch information
niels committed Oct 22, 2020
1 parent 87e11e8 commit b7a48f6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const DeadEntityIcon = "help-circle-outline";
export const FieldTemperature = 'temperature';
export const UnitPercent = '%';
export const CreateTimeScheme = 'make_scheme';
export const WorkdaySensor = 'binary_sensor.workday_sensor';

export const DayOptions = [
{ id: 1, name: localize('days_short.mon') },
Expand Down
22 changes: 17 additions & 5 deletions src/custom-elements/scheduler-entity-row.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { LitElement, html, customElement, css, property, internalProperty, PropertyValues } from 'lit-element';
import { ImportedEntry, Dictionary, EntityElement, HassAction, ActionElement, CardConfig, ScheduleEntity } from '../types';
import { parseTimestamp, weekday, MinutesPerHour, daysToArray, ETimeEvent, relativeTime } from '../date-time';
import { parseTimestamp, weekday, MinutesPerHour, daysToArray, ETimeEvent, relativeTime, EDayType } from '../date-time';
import { PrettyPrintName, capitalize, PrettyPrintIcon } from '../helpers';
import { HomeAssistant, computeEntity, } from 'custom-card-helpers';
import { importEntry } from '../interface';
import { DefaultEntityIcon, DeadEntityIcon, DeadEntityName, DefaultActionIcon } from '../const';
import { DefaultEntityIcon, DeadEntityIcon, DeadEntityName, DefaultActionIcon, WorkdaySensor } from '../const';
import { formatAction } from '../formatAction';
import { importAction, findActionIndex, actionConfig, findAction } from '../action';
import { localize } from '../localize/localize';
Expand Down Expand Up @@ -96,9 +96,21 @@ export class ScheduleEntityRow extends LitElement {
ts.setHours(hours);
ts.setMinutes(minutes);

let days = daysToArray(entry.days);
//TBD adjust days for weekday integration!
while (ts.valueOf() <= now.valueOf() || !days.includes(weekday(ts))) {
const workdayEntity = this.hass!.states[WorkdaySensor];

function blockByWorkdayEntity(ts: Date) {
if (!workdayEntity) return false;
const start_of_day = new Date();
start_of_day.setHours(0, 0, 0, 0);
let days_diff = Math.floor((ts.valueOf() - start_of_day.valueOf()) / (24 * 3600 * 1000));
if (days_diff != 0) return false;
if (entry.days.type == EDayType.Workday) return workdayEntity.state != "on";
else if (entry.days.type == EDayType.Weekend) return workdayEntity.state == "on";
return false;
}

let days = daysToArray(entry.days, workdayEntity);
while (ts.valueOf() <= now.valueOf() || !days.includes(weekday(ts)) || blockByWorkdayEntity(ts)) {
ts.setDate(ts.getDate() + 1);
}
return ts;
Expand Down
4 changes: 2 additions & 2 deletions src/custom-elements/scheduler-timepicker-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { localize } from '../localize/localize';
import { CardConfig, ActionElement, EntityElement, EVariableType, LevelVariableConfig, LevelVariable, ListVariable, Entry, ListVariableConfig } from '../types';
import { PrettyPrintIcon, PrettyPrintName, capitalize } from '../helpers';
import { EDayType, daysToArray, ETimeEvent, sortDaylist } from '../date-time';
import { DayTypeOptions, DayOptions, CreateTimeScheme, DefaultTimeStep, FieldTemperature, DefaultActionIcon } from '../const';
import { DayTypeOptions, DayOptions, CreateTimeScheme, DefaultTimeStep, FieldTemperature, DefaultActionIcon, WorkdaySensor } from '../const';


import './time-picker';
Expand Down Expand Up @@ -279,7 +279,7 @@ export class SchedulerTimepickerCard extends LitElement {
const input = (ev.target as HTMLInputElement).value;
if (Object.values(EDayType).includes(input as EDayType)) {
if (input == EDayType.Custom && !daysCfg.custom_days)
Object.assign(daysCfg, { custom_days: daysToArray(daysCfg) });
Object.assign(daysCfg, { custom_days: daysToArray(daysCfg, this.hass!.states[WorkdaySensor]) });
Object.assign(daysCfg, { type: input });
} else {
Object.assign(daysCfg, { custom_days: [...input] });
Expand Down
23 changes: 17 additions & 6 deletions src/date-time.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { localize } from './localize/localize';
import { HassEntity } from 'home-assistant-js-websocket';

export const MinutesPerHour = 60;
export const HoursPerDay = 24;
Expand Down Expand Up @@ -109,12 +110,22 @@ export function parseTimestamp(input: string | Date): number {
return value;
}

export function daysToArray(dayCfg: Days) {
if (dayCfg.type == EDayType.Daily) return [1, 2, 3, 4, 5, 6, 7];
else if (dayCfg.type == EDayType.Workday) return [1, 2, 3, 4, 5];
else if (dayCfg.type == EDayType.Weekend) return [6, 7];
else if (dayCfg.type == EDayType.Custom) return dayCfg.custom_days as number[];
else return [];
export function daysToArray(dayCfg: Days, workday_sensor?: HassEntity) {
if (!workday_sensor) {
if (dayCfg.type == EDayType.Daily) return [1, 2, 3, 4, 5, 6, 7];
else if (dayCfg.type == EDayType.Workday) return [1, 2, 3, 4, 5];
else if (dayCfg.type == EDayType.Weekend) return [6, 7];
else if (dayCfg.type == EDayType.Custom) return dayCfg.custom_days as number[];
else return [];
}
else {
if (dayCfg.type == EDayType.Daily) return [1, 2, 3, 4, 5, 6, 7];
else if (dayCfg.type == EDayType.Custom) return dayCfg.custom_days as number[];
const days_of_the_week = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
if (dayCfg.type == EDayType.Workday) return (workday_sensor.attributes.workdays.map(day => days_of_the_week.findIndex(e => e == day) + 1));
else if (dayCfg.type == EDayType.Weekend) return days_of_the_week.map((e, index) => workday_sensor.attributes.workdays.includes(e) ? null : index + 1).filter(e => e);
else return [];
}
}

export function stringToTimeEvent(input: string): ETimeEvent {
Expand Down
2 changes: 1 addition & 1 deletion src/scheduler-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export class SchedulerCard extends LitElement {

if (!this.entity) {
const actions = scheduleEntity.attributes.actions
.map(e => importAction(e))
.map(importAction)
.map(e => actionConfig(omit(e, ['entity']) as HassAction));

const entity: EntityElement = {
Expand Down

0 comments on commit b7a48f6

Please sign in to comment.