Skip to content

Commit

Permalink
added inches as a possible unit of measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
t1gr0u committed May 8, 2023
1 parent 7d1c8d1 commit ae7d691
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ entity: sensor.rain_daily
| show_warning | boolean | **Optional** | Show what a warning looks like for the card | `false` |
| entity | string | **Required** | Home Assistant entity ID. | `none` |
| language | string | **Optional** | The 2 character that determines the language| `en` |
| is_imperial | boolean | **Optional** | Switch to inches (`in`) instead of `mm` | `false` |
| hourly_rate_entity| string | **Optional** | Home Assistant entity ID to hourly rate | `none` |
| tap_action | object | **Optional** | Action to take on tap | `action: more-info` |
| hold_action | object | **Optional** | Action to take on hold | `none` |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rain-gauge-card",
"version": "1.1.0",
"version": "1.2.0",
"description": "A Lovelace card that shows the rain gauge for Home Assistant",
"keywords": [
"home-assistant",
Expand Down
2 changes: 1 addition & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const CARD_VERSION = '1.1.0';
export const CARD_VERSION = '1.2.0';
11 changes: 11 additions & 0 deletions src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export class RainGaugeCardEditor extends ScopedRegistryHost(LitElement) implemen
return this._config?.entity || '';
}

get _is_imperial(): boolean {
return this._config?.is_imperial || false;
}

get _language(): string {
return this._config?.language || '';
}
Expand Down Expand Up @@ -99,6 +103,13 @@ export class RainGaugeCardEditor extends ScopedRegistryHost(LitElement) implemen
.configValue=${'name'}
@input=${this._valueChanged}
></mwc-textfield>
<mwc-formfield .label=${`Is imperial?`}>
<mwc-switch
.checked=${this._is_imperial !== false}
.configValue=${'is_imperial'}
@change=${this._valueChanged}
></mwc-switch>
</mwc-formfield>
<mwc-select
naturalMenuWidth
fixedMenuPosition
Expand Down
17 changes: 12 additions & 5 deletions src/rain-gauge-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,14 @@ export class RainGaugeCard extends LitElement {

const entityId = this.config.entity;
const entityState = entityId ? this.hass.states[entityId] : undefined;
const stateValue: number = entityState ? parseFloat(entityState.state) : 0;
let stateValue: number = entityState ? parseFloat(entityState.state) : 0;

let unitOfMeasurement = 'mm'
if (this.config.is_imperial) {
unitOfMeasurement = 'in'
const stateValueConverted = stateValue * 25.4
stateValue = Math.round((stateValueConverted + Number.EPSILON) * 100) / 100
}

// 180 min - 0 max
const maxLevel = 40
Expand Down Expand Up @@ -144,23 +151,23 @@ export class RainGaugeCard extends LitElement {
<div>
<p>
<span style="font-weight: bold;">${localize('common.total', '', '', this.config.language)}</span><br/>
${stateValue} mm
${stateValue} ${unitOfMeasurement}
</p>
</div>
<div>
${this._showHourlyRate(hourlyRateEntityState, hourlyRateStateValue)}
${this._showHourlyRate(hourlyRateEntityState, hourlyRateStateValue, unitOfMeasurement)}
</div>
</div>
</div>
</ha-card>
`;
}

private _showHourlyRate(hourlyRateEntityState: any | undefined, hourlyRateStateValue: number): TemplateResult | void {
private _showHourlyRate(hourlyRateEntityState: any | undefined, hourlyRateStateValue: number, unitOfMeasurement: string): TemplateResult | void {
if (hourlyRateEntityState === undefined) return
return html`<p>
<span style="font-weight: bold;">${localize('common.rate', '', '', this.config.language)}</span><br/>
${hourlyRateStateValue} mm/h
${hourlyRateStateValue} ${unitOfMeasurement}/h
</p>`
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface RainGaugeCardConfig extends LovelaceCardConfig {
test_gui?: boolean;
entity?: string;
language?: string;
is_imperial?: boolean;
hourly_rate_entity?: string;
tap_action?: ActionConfig;
hold_action?: ActionConfig;
Expand Down

0 comments on commit ae7d691

Please sign in to comment.