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 authored and pkissling committed Oct 7, 2023
1 parent b2736e9 commit ed5020b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ hide_clock: false
hide_date: false
hourly_forecast: false
use_browser_time: true
time_zone: null
```

### Options
Expand All @@ -129,6 +130,7 @@ use_browser_time: true
| 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, falls back to the [`time_zone`](https://www.home-assistant.io/blog/2015/05/09/utc-time-zone-awareness/#setting-up-your-time-zone) configured in HA | `true` |
| 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

Expand Down
7 changes: 4 additions & 3 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 @@ -394,7 +394,8 @@ export class ClockWeatherCard extends LitElement {
hide_clock: config.hide_clock ?? false,
hide_date: config.hide_date ?? false,
date_pattern: config.date_pattern ?? 'P',
use_browser_time: config.use_browser_time ?? true
use_browser_time: config.use_browser_time ?? true,
time_zone: config.time_zone ?? undefined
}
}

Expand Down Expand Up @@ -549,7 +550,7 @@ export class ClockWeatherCard extends LitElement {

private toZonedDate (date: Date): Date {
if (this.config.use_browser_time) return date
const timeZone = this.hass?.config?.time_zone
const timeZone = this.config.time_zone ?? this.hass?.config?.time_zone
const withTimeZone = DateTime.fromJSDate(date).setZone(timeZone)
if (!withTimeZone.isValid) {
console.error(`clock-weather-card - Time Zone [${timeZone}] not supported. Falling back to browser time.`)
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 @@ -43,6 +44,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 ed5020b

Please sign in to comment.