Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #17 from AitorDB/feature/add-12h-support
Browse files Browse the repository at this point in the history
  • Loading branch information
AitorDB authored May 17, 2021
2 parents b2922bd + 3aaa2e2 commit f36551e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ Note: If `Custom: Sun card` doesn't appear you will have to reload cleaning the
| language | `'es'`/`'en'` | Changes card language | Home assistant language or english if not supported |
| showAzimuth | `boolean` | Displays azimuth in the footer | `false` |
| showElevation | `boolean` | Displays elevation in the footer | `false` |
| timeFormat | `'12h'`/`'24h'` | Displayed time format | Locale based on Home assistant language |
| title | `string` | Card title | Doesn't display a title by default | |

## Known issues
- Home assistant seems to provide next events instead today's one

## TODO
- [ ] Adjust styles
- [ ] Fix issue regarding next events
- [ ] Add to HACS (https://github.com/hacs/default/pull/951)
27 changes: 21 additions & 6 deletions src/cardContent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { html, TemplateResult } from 'lit-element'
import { TSunCardConfig, TSunCardData, TSunCardTexts } from './types'
import { TSunCardConfig, TSunCardData, TSunCardTexts, TSunCardTime } from './types'

export class SunCardContent {
static generate (data: TSunCardData, localization: TSunCardTexts, config: TSunCardConfig): TemplateResult {
Expand All @@ -24,11 +24,12 @@ export class SunCardContent {
<div class="sun-card-header">
<div class="sun-card-text-container">
<span class="sun-card-text-subtitle">${localization.Sunrise}</span>
<span class="sun-card-rising-time sun-card-text-time">${data?.times.sunrise ?? ''}</span>
${data?.times.sunrise ? this.generateTime(data.times.sunrise) : ''}
</div>
<div class="sun-card-text-container">
<span class="sun-card-text-subtitle">${localization.Sunset}</span>
<span class="sun-card-setting-time sun-card-text-time">${data?.times.sunset ?? ''}</span>
${data?.times.sunset ? this.generateTime(data.times.dawn) : ''}
</div>
</div>
`
Expand Down Expand Up @@ -86,15 +87,15 @@ export class SunCardContent {
<div class="sun-card-footer-row">
<div class="sun-card-text-container">
<span class="sun-card-text-subtitle">${localization.Dawn}</span>
<span class="sun-card-dawn-time sun-card-text-time">${data?.times.dawn ?? ''}</span>
${data?.times.dawn ? this.generateTime(data.times.dawn) : ''}
</div>
<div class="sun-card-text-container">
<span class="sun-card-text-subtitle">${localization.Noon}</span>
<span class="sun-card-noon-time sun-card-text-time">${data?.times.noon ?? ''}</span>
${data?.times.noon ? this.generateTime(data.times.noon) : ''}
</div>
<div class="sun-card-text-container">
<span class="sun-card-text-subtitle">${localization.Dusk}</span>
<span class="sun-card-dusk-time sun-card-text-time">${data?.times.dusk ?? ''}</span>
${data?.times.dusk ? this.generateTime(data.times.dusk) : ''}
</div>
</div>
`
Expand Down Expand Up @@ -130,4 +131,18 @@ export class SunCardContent {
</div>
`
}

private static generateTime (time: TSunCardTime) {
if (time.period) {
return html`
<span class="sun-card-text-time">
${time.time} <span class="sun-card-text-time-period">${time.period}</span>
</span>
`
}

return html`
<span class="sun-card-text-time">${time.time}</span>
`
}
}
4 changes: 4 additions & 0 deletions src/cardStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ export default css`
font-size: 1.25rem;
font-weight: 500;
}
.sun-card-text-time-period {
font-size: 0.75rem;
}
`
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class Constants {
darkMode: true,
language: 'en',
showAzimuth: false,
showElevation: false
showElevation: false,
timeFormat: '24h'
}

static readonly EVENT_X_POSITIONS = {
Expand Down
18 changes: 16 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,21 @@ class SunCard extends LitElement {
}

parseTime (timeText: string) {
const regex = /\d{1,2}:\d{1,2}|[AMP]+/g
const date = new Date(timeText)
const regex = /([0-9]{2}:[0-9]{2})/
return date.toTimeString().match(regex)?.[1]
const { language, timeFormat } = this.getConfig()
const [time, period] = date.toLocaleTimeString(language, { hour12: timeFormat === '12h' }).match(regex) as [string, ('AM' | 'PM')?]
return { time, period }
}

processLastHass () {
if (!this.lastHass) {
return
}

this.config.darkMode = this.config.darkMode ?? this.lastHass.themes.darkMode
this.config.language = this.config.language ?? this.lastHass.locale?.language ?? this.lastHass.language
this.config.timeFormat = this.config.timeFormat ?? this.getTimeFormatByLanguage(this.config.language)

const times = {
dawn: this.parseTime(this.lastHass.states['sun.sun'].attributes.next_dawn),
Expand Down Expand Up @@ -129,6 +136,7 @@ class SunCard extends LitElement {
config.language = this.config.language ?? Constants.DEFAULT_CONFIG.language
config.showAzimuth = this.config.showAzimuth ?? Constants.DEFAULT_CONFIG.showAzimuth
config.showElevation = this.config.showElevation ?? Constants.DEFAULT_CONFIG.showElevation
config.timeFormat = this.config.timeFormat ?? Constants.DEFAULT_CONFIG.timeFormat
config.title = this.config.title

if (!Object.keys(Constants.LOCALIZATION_LANGUAGES).includes(config.language!)) {
Expand All @@ -138,6 +146,12 @@ class SunCard extends LitElement {
return config
}

getTimeFormatByLanguage (language: string) {
const date = new Date()
const time = date.toLocaleTimeString(language).toLocaleLowerCase()
return time.includes('pm') || time.includes('am') ? '12h' : '24h'
}

setConfig (config: TSunCardConfig) {
this.config = { ...config }
}
Expand Down
16 changes: 11 additions & 5 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ export type TSunCardConfig = {
language?: string
showAzimuth?: boolean
showElevation?: boolean
timeFormat?: '12h' | '24h'
title?: string
}

export type TSunCardTime = {
time: string,
period?: 'AM' | 'PM'
}

export type TSunCardData = {
azimuth: number
dawnProgressPercent: number
Expand All @@ -18,11 +24,11 @@ export type TSunCardData = {
y: number
}
times: {
dawn: string | null | undefined
dusk: string | null | undefined
noon: string | null | undefined
sunrise: string | null | undefined
sunset: string | null | undefined
dawn: TSunCardTime
dusk: TSunCardTime
noon: TSunCardTime
sunrise: TSunCardTime
sunset: TSunCardTime
}
}

Expand Down

0 comments on commit f36551e

Please sign in to comment.