Skip to content

Commit

Permalink
Add configuration property to specify a time zone
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Kissling committed Oct 3, 2023
1 parent c86d19b commit df36830
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ hide_clock: false
hide_date: false
hourly_forecast: false
use_browser_time: false
time_zone: null
```

### Options
Expand All @@ -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).
Expand Down
14 changes: 9 additions & 5 deletions src/clock-weather-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,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;
}
Expand Down Expand Up @@ -393,7 +393,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
};
}

Expand Down Expand Up @@ -520,12 +521,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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit df36830

Please sign in to comment.