Skip to content

Commit

Permalink
added speed factor configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
reptilex committed Apr 1, 2022
1 parent ef0570b commit e06ab4c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ One to force W (Watt) instead of kW, set it to 1 to use it:
show_w_not_kw: 1
```
One to set a different speed for the moving dots, normal speed factor is 0.04 so stay near that number at first, 0.2 is really fast:
```yml
speed_factor: 0.03
```
One for the threshold from which W is converted to kW (the example below will change W into kilowatt from 5000 W onwards):
```yml
threshold_in_k: 5
Expand Down
3 changes: 2 additions & 1 deletion src/TeslaStyleSolarPowerCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class TeslaStyleSolarPowerCard extends LitElement {
if (this.config.battery_icon == null) this.config.battery_icon = 'mdi:battery-medium';
if (this.config.appliance1_icon == null) this.config.appliance1_icon = 'mdi:car-sports';
if (this.config.appliance2_icon == null) this.config.appliance2_icon = 'mdi:air-filter';
if (this.config.speed_factor == null) this.config.speed_factor = 0.04;

this.createSolarCardElements();
if (!this.config.energy_flow_diagramm) {
Expand Down Expand Up @@ -149,7 +150,7 @@ export class TeslaStyleSolarPowerCard extends LitElement {
this.hass.states[solarSensor.entity].state,
this.hass.states[solarSensor.entity].attributes.unit_of_measurement
);
solarSensor.setSpeed();
solarSensor.setSpeed(this.config.speed_factor);
} catch (err) {
this.error += " Configured '" + solarSensor.entity + "' entity was not found. ";
}
Expand Down
12 changes: 10 additions & 2 deletions src/models/SensorElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,17 @@ export class SensorElement {
}
}

public setSpeed(): void {
public setSpeed(factor: number | undefined): void {
this.speed = 0;
if (Math.abs(this.value) === 0) return;
this.speed = (SensorElement.SPEEDFACTOR * this.value) / 1000;

let speedFactor: number;
if (factor === undefined || factor > 1 || factor <= 0) {
speedFactor = SensorElement.SPEEDFACTOR;
} else {
speedFactor = factor;
}

this.speed = (speedFactor * this.value) / 1000;
}
}
1 change: 1 addition & 0 deletions src/models/TeslaStyleSolarPowerCardConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface TeslaStyleSolarPowerCardConfig extends LovelaceCardConfig {
show_w_not_kw?: any;
hide_inactive_lines?: boolean;
threshold_in_k?: number;
speed_factor?: number;
energy_flow_diagramm?: boolean;
energy_flow_diagramm_lines_factor?: number;
change_house_bubble_color_with_flow?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions tesla-style-solar-power-card.js

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions test/SensorElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@ describe('SensorElements test', () => {
const selement = new SensorElement('test_entity', 'solar_consumption');
selement.value = 1;
selement.unitOfMeasurement = 'W';
selement.setSpeed();
selement.setSpeed(0.04);
expect(selement.speed).to.equal(0.00004);
});

it('should setSpeed with kW', () => {
const selement = new SensorElement('test_entity', 'solar_consumption');
selement.value = 1;
selement.unitOfMeasurement = 'KW';
selement.setSpeed();
selement.setSpeed(0.04);
expect(selement.speed).to.equal(0.00004);
});

it('should setSpeed with kW with factor 0.05', () => {
const selement = new SensorElement('test_entity', 'solar_consumption');
selement.value = 1;
selement.unitOfMeasurement = 'KW';
selement.setSpeed(0.05);
expect(selement.speed).to.equal(0.00005);
});

it('should setValueAndUnitOfMeasurement from kW rounded to 1 decimals', () => {
const selement = new SensorElement('test_entity', 'solar_consumption');
selement.setValueAndUnitOfMeasurement('1.1111', 'kW');
Expand Down

0 comments on commit e06ab4c

Please sign in to comment.