Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility fix for Home Assistant 2022.4.0+ #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions dist/datetime/select_unit.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export declare type Unit = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';
export declare function selectUnit(from: Date | number, to?: Date | number, thresholds?: Partial<Thresholds>): {
value: number;
unit: Unit;
};
declare type Thresholds = Record<'second' | 'minute' | 'hour' | 'day', number>;
export declare const DEFAULT_THRESHOLDS: Thresholds;
export {};
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.m.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.m.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.modern.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.modern.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.umd.js.map

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
},
"homepage": "https://github.com/custom-cards/custom-card-helpers#readme",
"dependencies": {
"@formatjs/intl-utils": "^3.8.4",
"home-assistant-js-websocket": "^6.0.1",
"intl-messageformat": "^9.11.1",
"lit": "^2.1.1",
Expand Down
3 changes: 2 additions & 1 deletion src/datetime/relative_time.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//REF: https://github.com/home-assistant/frontend/blob/dev/src/common/datetime/relative_time.ts

import { selectUnit } from "@formatjs/intl-utils";
import { FrontendLocaleData } from "../types";
import { selectUnit } from "./select_unit";

const formatRelTimeMem =
(locale: FrontendLocaleData) =>
new Intl.RelativeTimeFormat(locale.language, { numeric: "auto" });


/**
* Calculate a string representing a date object as relative time from now.
*
Expand Down
94 changes: 94 additions & 0 deletions src/datetime/select_unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* selectUnit() taken from @formatjs/intl-units 3.8.4.
*
*/

export type Unit =
| 'second'
| 'minute'
| 'hour'
| 'day'
| 'week'
| 'month'
| 'quarter'
| 'year';

const MS_PER_SECOND = 1e3;
const SECS_PER_MIN = 60;
const SECS_PER_HOUR = SECS_PER_MIN * 60;
const SECS_PER_DAY = SECS_PER_HOUR * 24;
const SECS_PER_WEEK = SECS_PER_DAY * 7;

export function selectUnit(
from: Date | number,
to: Date | number = Date.now(),
thresholds: Partial<Thresholds> = {}
): {value: number; unit: Unit} {
const resolvedThresholds: Thresholds = {
...DEFAULT_THRESHOLDS,
...(thresholds || {}),
};
const secs = (+from - +to) / MS_PER_SECOND;
if (Math.abs(secs) < resolvedThresholds.second) {
return {
value: Math.round(secs),
unit: 'second',
};
}
const mins = secs / SECS_PER_MIN;
if (Math.abs(mins) < resolvedThresholds.minute) {
return {
value: Math.round(mins),
unit: 'minute',
};
}
const hours = secs / SECS_PER_HOUR;
if (Math.abs(hours) < resolvedThresholds.hour) {
return {
value: Math.round(hours),
unit: 'hour',
};
}

const days = secs / SECS_PER_DAY;
if (Math.abs(days) < resolvedThresholds.day) {
return {
value: Math.round(days),
unit: 'day',
};
}
const fromDate = new Date(from);
const toDate = new Date(to);

const years = fromDate.getFullYear() - toDate.getFullYear();
if (Math.round(Math.abs(years)) > 0) {
return {
value: Math.round(years),
unit: 'year',
};
}

const months = years * 12 + fromDate.getMonth() - toDate.getMonth();
if (Math.round(Math.abs(months)) > 0) {
return {
value: Math.round(months),
unit: 'month',
};
}
const weeks = secs / SECS_PER_WEEK;

return {
value: Math.round(weeks),
unit: 'week',
};
}

type Thresholds = Record<'second' | 'minute' | 'hour' | 'day', number>;

export const DEFAULT_THRESHOLDS: Thresholds = {
second: 45, // seconds to minute
minute: 45, // minutes to hour
hour: 22, // hour to day
day: 5, // day to week
};

3 changes: 2 additions & 1 deletion src/get-lovelace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export const getLovelace = () => {
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.querySelector('ha-drawer');
root = root && root.querySelector('partial-panel-resolver');
root = root && root.shadowRoot || root;
root = root && root.querySelector('ha-panel-lovelace');
root = root && root.shadowRoot;
Expand Down
Loading