mirror of
https://github.com/GeorgeSG/lovelace-time-picker-card.git
synced 2026-09-06 02:18:44 +00:00
Refactor constants
This commit is contained in:
@@ -2,3 +2,8 @@ import * as pkg from '../package.json';
|
||||
|
||||
export const CARD_VERSION = pkg.version;
|
||||
export const CARD_SIZE = 3;
|
||||
|
||||
export const ENTITY_DOMAIN = 'input_datetime';
|
||||
|
||||
export const DEFAULT_HOUR_STEP = 1;
|
||||
export const DEFAULT_MINUTE_STEP = 5;
|
||||
|
||||
+6
-6
@@ -1,16 +1,16 @@
|
||||
import { TimeUnit } from './time-unit';
|
||||
import { DEFAULT_HOUR_STEP } from '../const';
|
||||
import { HourMode } from '../types';
|
||||
import { TimeUnit } from './time-unit';
|
||||
|
||||
export class Hour extends TimeUnit {
|
||||
private static readonly DEFAULT_STEP = 1;
|
||||
private static readonly MAX = 24;
|
||||
private static readonly VALUE_LIMIT = 24;
|
||||
|
||||
constructor(value: number, step = Hour.DEFAULT_STEP, private hourMode: HourMode) {
|
||||
super(value, step, Hour.MAX);
|
||||
constructor(value: number, step = DEFAULT_HOUR_STEP, private hourMode: HourMode) {
|
||||
super(value, step, Hour.VALUE_LIMIT);
|
||||
}
|
||||
|
||||
get maxValue(): number {
|
||||
return this.hourMode || Hour.MAX;
|
||||
return this.hourMode || Hour.VALUE_LIMIT;
|
||||
}
|
||||
|
||||
togglePeriod(): void {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { DEFAULT_MINUTE_STEP } from '../const';
|
||||
import { TimeUnit } from './time-unit';
|
||||
|
||||
export class Minute extends TimeUnit {
|
||||
private static readonly DEFAULT_STEP = 5;
|
||||
private static readonly MAX = 60;
|
||||
private static readonly VALUE_LIMIT = 60;
|
||||
|
||||
maxValue = Minute.MAX;
|
||||
maxValue = Minute.VALUE_LIMIT;
|
||||
|
||||
constructor(value: number, step = Minute.DEFAULT_STEP) {
|
||||
super(value, step, Minute.MAX);
|
||||
constructor(value: number, step = DEFAULT_MINUTE_STEP) {
|
||||
super(value, step, Minute.VALUE_LIMIT);
|
||||
}
|
||||
|
||||
protected isValidString(valueStr: string): boolean {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { HomeAssistant, computeDomain } from 'custom-card-helpers';
|
||||
import { computeDomain, HomeAssistant } from 'custom-card-helpers';
|
||||
import { HassEntity } from 'home-assistant-js-websocket';
|
||||
import {
|
||||
css,
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
} from 'lit-element';
|
||||
import './components/time-period.component';
|
||||
import './components/time-unit.component';
|
||||
import { CARD_SIZE, CARD_VERSION } from './const';
|
||||
import { CARD_SIZE, CARD_VERSION, ENTITY_DOMAIN } from './const';
|
||||
import { Hour } from './models/hour';
|
||||
import { Minute } from './models/minute';
|
||||
import { Partial } from './partials';
|
||||
@@ -25,8 +25,6 @@ console.info(
|
||||
|
||||
@customElement('time-picker-card')
|
||||
export class TimePickerCard extends LitElement {
|
||||
private static readonly ENTITY_DOMAIN = 'input_datetime';
|
||||
|
||||
@property() private hass!: HomeAssistant;
|
||||
@property() private config!: TimePickerCardConfig;
|
||||
@property() private hour!: Hour;
|
||||
@@ -54,13 +52,13 @@ export class TimePickerCard extends LitElement {
|
||||
return Partial.error('Entity not found', this.config);
|
||||
}
|
||||
|
||||
if (computeDomain(this.entity.entity_id) !== TimePickerCard.ENTITY_DOMAIN) {
|
||||
return Partial.error(`You must set an ${TimePickerCard.ENTITY_DOMAIN} entity`, this.config);
|
||||
if (computeDomain(this.entity.entity_id) !== ENTITY_DOMAIN) {
|
||||
return Partial.error(`You must set an ${ENTITY_DOMAIN} entity`, this.config);
|
||||
}
|
||||
|
||||
if (!this.entity.attributes.has_time) {
|
||||
return Partial.error(
|
||||
`You must set an ${TimePickerCard.ENTITY_DOMAIN} entity that sets has_time: true`,
|
||||
`You must set an ${ENTITY_DOMAIN} entity that sets has_time: true`,
|
||||
this.config
|
||||
);
|
||||
}
|
||||
@@ -121,7 +119,7 @@ export class TimePickerCard extends LitElement {
|
||||
|
||||
const time = `${this.hour.value}:${this.minute.value}:00`;
|
||||
|
||||
return this.hass.callService(TimePickerCard.ENTITY_DOMAIN, 'set_datetime', {
|
||||
return this.hass.callService(ENTITY_DOMAIN, 'set_datetime', {
|
||||
entity_id: this.entity!.entity_id,
|
||||
time,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user