Skip to content

Commit

Permalink
Merge pull request #61 from rohankapoorcom/lizsugar_input_number
Browse files Browse the repository at this point in the history
[feat] Add Support for input_number domain
  • Loading branch information
rohankapoorcom authored Aug 16, 2023
2 parents 5310c83 + 8e92000 commit c17f74e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
4 changes: 2 additions & 2 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, media_player, climate, lock` entities.
A button card with integrated slider for `automation, light, switch, fan, cover, input_boolean, input_number, media_player, 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, media_player, climate, lock` | |
| entity | string | **Required** | HA entity ID from domain `automation, light, switch, fan, cover, input_boolean, input_number, media_player, 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
7 changes: 5 additions & 2 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,7 +299,10 @@ export abstract class Controller {
}

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

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 @@ -6,6 +6,7 @@ import { Controller } from './controller';
import { CoverController } from './cover-controller';
import { FanController } from './fan-controller';
import { InputBooleanController } from './input-boolean-controller';
import { InputNumberController } from './input-number-controller';
import { LightController } from './light-controller';
import { LockController } from './lock-controller';
import { MediaController } from './media-controller';
Expand All @@ -21,6 +22,7 @@ export class ControllerFactory {
[Domain.AUTOMATION]: AutomationController,
[Domain.COVER]: CoverController,
[Domain.INPUT_BOOLEAN]: InputBooleanController,
[Domain.INPUT_NUMBER]: InputNumberController,
[Domain.MEDIA_PLAYER]: MediaController,
[Domain.CLIMATE]: ClimateController,
[Domain.LOCK]: LockController,
Expand Down
41 changes: 41 additions & 0 deletions src/controllers/input-number-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Controller } from './controller';
import { normalize, percentageToValue, toPercentage } from '../utils';
import { SliderConfig } from '../types';

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

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

set _value(value) {
this._hass.callService('input_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}`;
}

}
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export enum Domain {
FAN = 'fan',
COVER = 'cover',
INPUT_BOOLEAN = 'input_boolean',
INPUT_NUMBER = 'input_number',
MEDIA_PLAYER = 'media_player',
CLIMATE = 'climate',
LOCK = 'lock',
Expand Down Expand Up @@ -175,6 +176,15 @@ export const SliderConfigDefaultDomain: Map<string, SliderConfig> = new Map([
force_square: false,
show_attribute: false,
}],
[Domain.INPUT_NUMBER, {
direction: SliderDirections.LEFT_RIGHT,
background: SliderBackground.SOLID,
use_state_color: false,
use_percentage_bg_opacity: false,
show_track: false,
toggle_on_click: false,
force_square: false,
}],
[Domain.MEDIA_PLAYER, {
direction: SliderDirections.LEFT_RIGHT,
background: SliderBackground.TRIANGLE,
Expand Down

0 comments on commit c17f74e

Please sign in to comment.