Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Add Support for number domain #64

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![GitHub Release][releases-shield]][releases]
[![hacs_badge](https://img.shields.io/badge/HACS-default-orange.svg?style=for-the-badge)](https://github.com/custom-components/hacs)

A button card with integrated slider for `automation, light, switch, fan, cover, input_boolean, input_number, media_player, climate, lock` entities.
A button card with integrated slider for `automation, light, switch, fan, cover, input_boolean, input_number, media_player, number, climate, lock` entities.

![Preview][preview]
![Preview 2][preview-2]
Expand Down Expand Up @@ -71,7 +71,7 @@ Slider Button Card supports Lovelace's Visual Editor.
| Name | Type | Requirement | Description | Default |
| ----------------- | ------- | ------------ | ------------------------------------------- | ------------------- |
| type | string | **Required** | `custom:slider-button-card` |
| entity | string | **Required** | HA entity ID from domain `automation, light, switch, fan, cover, input_boolean, input_number, media_player, climate, lock` | |
| entity | string | **Required** | HA entity ID from domain `automation, light, switch, fan, cover, input_boolean, input_number, media_player, number climate, lock` | |
| name | string | **Optional** | Name | `entity.friendly_name` |
| show_attribute | boolean | **Optional** | Show attribute | `false` (except for `media_player` entities) |
| show_name | boolean | **Optional** | Show name | `true` |
Expand Down Expand Up @@ -713,8 +713,9 @@ Mixed `group` entities are not supported, if you want to control multiple
## Known issues
When you discover any bugs please open an [issue](https://github.com/custom-cards/slider-button-card/issues).

### Input Numbers
### Input Number & Number entities
- If the `input_number.entity.min value` is not cleanly divisible by the `input_number.entity.step value`, then the slider card is off by an amount. If your `input_number` has `min = 5`, `max = 25`, `step = 5` then it will work just fine. But if the `step` is 2, then it will be off. This also has the side effect of changing the `input_number` to an "out of bounds" value when modified via this card. Using `step = 1` avoids this problem.
- The same limitation applies to `number` entities.

## Languages

Expand Down
7 changes: 2 additions & 5 deletions src/controllers/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export abstract class Controller {

moveSlider(event: any, {left, top, width, height}): number {
let percentage = this.calcMovementPercentage(event, {left, top, width, height});
//percentage = this.applyStep(percentage);
percentage = this.applyStep(percentage);
percentage = normalize(percentage, 0, 100);
if (!this.isValuePercentage) {
percentage = percentageToValue(percentage, this.min, this.max);
Expand Down Expand Up @@ -299,10 +299,7 @@ export abstract class Controller {
}

applyStep(value: number): number {
this.log("applyStep value", value);
let rounded = Math.round(value / this.step) * this.step;
this.log("applyStep round", rounded);
return rounded;
return Math.round(value / this.step) * this.step;
}

log(name = '', value: string | number | object = ''): void {
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/get-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { LightController } from './light-controller';
import { LockController } from './lock-controller';
import { MediaController } from './media-controller';
import { SwitchController } from './switch-controller';
import { NumberController } from './number-controller';

export class ControllerFactory {
static getInstance(config: SliderButtonCardConfig): Controller {
Expand All @@ -24,6 +25,7 @@ export class ControllerFactory {
[Domain.INPUT_BOOLEAN]: InputBooleanController,
[Domain.INPUT_NUMBER]: InputNumberController,
[Domain.MEDIA_PLAYER]: MediaController,
[Domain.NUMBER]: NumberController,
[Domain.CLIMATE]: ClimateController,
[Domain.LOCK]: LockController,
};
Expand Down
2 changes: 0 additions & 2 deletions src/controllers/input-number-controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Controller } from './controller';
import { normalize, percentageToValue, toPercentage } from '../utils';
import { SliderConfig } from '../types';

export class InputNumberController extends Controller {
_targetValue;
Expand Down
39 changes: 39 additions & 0 deletions src/controllers/number-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Controller } from './controller';

export class NumberController extends Controller {
_targetValue;
_invert = false;

get _value(): number {
return this.stateObj.state;
}

set _value(value) {
this._hass.callService('number', 'set_value', {
// eslint-disable-next-line @typescript-eslint/camelcase
entity_id: this.stateObj.entity_id,
value: value,
});
}

get _min(): number {
return this.stateObj.attributes.min;
}

get _max(): number {
return this.stateObj.attributes.max;
}

get isValuePercentage(): boolean {
return false;
}

get _step(): number {
return this.stateObj.attributes.step;
}

get label(): string {
return this.stateObj.attributes.unit_of_measurement ? `${this.targetValue} ${this.stateObj.attributes.unit_of_measurement}` : `${this.targetValue}`;
}

}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export enum Domain {
INPUT_BOOLEAN = 'input_boolean',
INPUT_NUMBER = 'input_number',
MEDIA_PLAYER = 'media_player',
NUMBER = 'number',
CLIMATE = 'climate',
LOCK = 'lock',
AUTOMATION = 'automation',
Expand Down