From bc35ea9dad2c794da1c08a8896225372b2294ad7 Mon Sep 17 00:00:00 2001 From: Patrick Kissling Date: Tue, 3 Oct 2023 14:33:11 +0200 Subject: [PATCH] Add configuration property to specify a time zone --- README.md | 3 ++- src/clock-weather-card.ts | 14 +++++++++----- src/types.ts | 2 ++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b3b4bd34..5bb07216 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ hide_clock: false hide_date: false hourly_forecast: false use_browser_time: false +time_zone: null ``` ### Options @@ -135,7 +136,7 @@ use_browser_time: false | hide_date | boolean | **Optional** | Hides the date from the today section | `false` | | hourly_forecast | boolean | **Optional** | Displays an hourly forecast instead of daily | `false` | | use_browser_time | boolean | **Optional** | Uses the time from your browser to indicate the current time. If not provided, uses the [time_zone](https://www.home-assistant.io/blog/2015/05/09/utc-time-zone-awareness/#setting-up-your-time-zone) configured in HA | `false` | - +| time_zone | string | **Optional** | Uses the given [time zone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) to indicate the current date and time. If not provided, uses the time zone configured in HA | `null` | ## Footnotes [^1]: Theme used: [lovelace-ios-themes](https://github.com/basnijholt/lovelace-ios-themes). diff --git a/src/clock-weather-card.ts b/src/clock-weather-card.ts index b0cc9ec6..88e705e1 100644 --- a/src/clock-weather-card.ts +++ b/src/clock-weather-card.ts @@ -125,7 +125,7 @@ export class ClockWeatherCard extends LitElement { const oldHass = changedProps.get('hass') as HomeAssistant | undefined; if (oldHass) { const oldSun = oldHass.states[this.config.sun_entity]; - const newSun = this.hass?.states[this.config.sun_entity]; + const newSun = this.hass.states[this.config.sun_entity]; if (oldSun !== newSun) { return true; } @@ -390,7 +390,8 @@ export class ClockWeatherCard extends LitElement { hide_clock: config.hide_clock ?? false, hide_date: config.hide_date ?? false, date_pattern: config.date_pattern ?? 'D', - use_browser_time: config.use_browser_time ?? false + use_browser_time: config.use_browser_time ?? false, + time_zone: config.time_zone ?? undefined }; } @@ -517,12 +518,15 @@ export class ClockWeatherCard extends LitElement { private toZonedDate(date: DateTime): DateTime { const localizedDate = date.setLocale(this.getLocale()); if (this.config.use_browser_time) return localizedDate; - const timeZone = this.hass?.config?.time_zone; - const withTimeZone = localizedDate.setZone(timeZone); + const timezone = this.config.time_zone ?? this.hass.config?.time_zone; + if (!timezone) { + return localizedDate; + } + const withTimeZone = localizedDate.setZone(timezone); if (withTimeZone.isValid) { return withTimeZone; } - console.error(`clock-weather-card - Time Zone [${timeZone}] not supported. Falling back to browser time.`); + console.error(`clock-weather-card - Time Zone "${timezone}" not supported. Falling back to browser time.`); return localizedDate; } diff --git a/src/types.ts b/src/types.ts index 436dea56..536fe6e6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -26,6 +26,7 @@ export interface ClockWeatherCardConfig extends LovelaceCardConfig { hide_clock?: boolean; hide_date?: boolean; use_browser_time?: boolean; + time_zone?: string; } export interface MergedClockWeatherCardConfig extends LovelaceCardConfig { @@ -45,6 +46,7 @@ export interface MergedClockWeatherCardConfig extends LovelaceCardConfig { hide_clock: boolean; hide_date: boolean; use_browser_time: boolean; + time_zone?: string; } export const enum WeatherEntityFeature {