Skip to content

Commit

Permalink
Consolidate code into function
Browse files Browse the repository at this point in the history
  • Loading branch information
rgc99 committed Jun 25, 2024
1 parent 701f46e commit ebed7b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 37 deletions.
42 changes: 5 additions & 37 deletions src/irrigation-unlimited-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {
IUTimeline,
} from "./iu";
import { styles } from "./styles";

import { HomeAssistant } from "./ha-types";
import type {
IrrigationUnlimitedCardConfig,
LovelaceCardEditor,
} from "./types";
import { CARD_VERSION } from "./const";
import { localize } from "./localize/localize";
import { date_to_str } from "./util";

/* eslint no-console: 0 */
console.info(
Expand Down Expand Up @@ -124,7 +124,6 @@ export class IrrigationUnlimitedCard extends LitElement {
let start: Date;
let duration: string | undefined;
let schedule_name: string;
let startStr = "";

if (isOn) {
start = new Date(stateObj.attributes.current_start);
Expand All @@ -139,14 +138,7 @@ export class IrrigationUnlimitedCard extends LitElement {
duration = stateObj.attributes.next_duration;
schedule_name = stateObj.attributes.next_name;
}
if (!isNaN(start.getTime())) {
startStr = start.toLocaleTimeString(undefined, {
weekday: "short",
hour: "numeric",
minute: "2-digit",
hour12: false,
});
}
const startStr = date_to_str(start);

const controllerClasses: Array<string> = ["iu-controller iu-object"];
if (isHidden) controllerClasses.push("iu-hidden");
Expand Down Expand Up @@ -246,7 +238,6 @@ export class IrrigationUnlimitedCard extends LitElement {
let schedule_index: number;
let schedule_name: string;
let adjustment: string;
let startStr = "";

if (isOn) {
start = new Date(stateObj.attributes.current_start);
Expand All @@ -268,14 +259,7 @@ export class IrrigationUnlimitedCard extends LitElement {
adjustment = stateObj.attributes.next_adjustment;
}
const isManual = schedule_index === 0;
if (!isNaN(start.getTime())) {
startStr = start.toLocaleTimeString(undefined, {
weekday: "short",
hour: "numeric",
minute: "2-digit",
hour12: false,
});
}
const startStr = date_to_str(start);
const classes: Array<string> = ["iu-zone-row iu-td"];
if (isOn) classes.push("iu-on");
if (isEnabled) classes.push("iu-enabled");
Expand Down Expand Up @@ -418,7 +402,6 @@ export class IrrigationUnlimitedCard extends LitElement {
let schedule_index: number;
let schedule_name: string;
let adjustment: string;
let startStr = "";

if (isOn || isPaused || isDelay) {
start = new Date(stateObj.attributes.current_start);
Expand All @@ -441,15 +424,7 @@ export class IrrigationUnlimitedCard extends LitElement {
}
const isManual = schedule_index === 0;
const isRunning = sequence.remaining !== "0:00:00";

if (start !== null && !isNaN(start.getTime())) {
startStr = start.toLocaleTimeString(undefined, {
weekday: "short",
hour: "numeric",
minute: "2-digit",
hour12: false,
});
}
const startStr = date_to_str(start);
const classes: Array<string> = ["iu-sequence-row iu-td"];
if (isOn) classes.push("iu-on");
if (isPaused) classes.push("iu-paused");
Expand Down Expand Up @@ -550,14 +525,7 @@ export class IrrigationUnlimitedCard extends LitElement {

if (suspended !== null) {
const start = new Date(suspended);
if (!isNaN(start.getTime())) {
startStr = start.toLocaleTimeString(undefined, {
weekday: "short",
hour: "numeric",
minute: "2-digit",
hour12: false,
});
}
startStr = date_to_str(start);
}
const classes: Array<string> = ["iu-sequence-zone-row iu-td"];
if (isOn) classes.push("iu-on");
Expand Down
12 changes: 12 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ export function secs_to_hms(value: number | undefined): string | undefined {
String(seconds).padStart(2, "0")
);
}

export function date_to_str(value: Date): string {
if (value !== null && !isNaN(value.getTime())) {
return value.toLocaleTimeString(undefined, {
weekday: "short",
hour: "numeric",
minute: "2-digit",
hour12: false,
});
}
return "";
}

0 comments on commit ebed7b1

Please sign in to comment.