Skip to content

Commit

Permalink
Add overriding keys
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMachowski committed Nov 5, 2023
1 parent 755b219 commit c801c62
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 16 deletions.
61 changes: 61 additions & 0 deletions changing_buttons.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
## Overriding key actions

Example config:
```yaml
type: custom:lg-remote-control
av_receiver_family: anthemav
entity: media_player.lg_webos_smart_tv
is_smart_tv: 'true'
colors:
buttons: red
text: blue
background: blue
projectorentity: ''
mac: '00:11:22:33:44:66'
keys:
LEFT:
service: light.toggle
data:
entity_id: light.tv
VOLUME_UP:
service: light.toggle
data:
entity_id: light.tv
```
available keys:
- `"1"`
- `"2"`
- `"3"`
- `"4"`
- `"5"`
- `"6"`
- `"7"`
- `"8"`
- `"9"`
- `"0"`
- `"UP"`
- `"LEFT"`
- `"ENTER"`
- `"RIGHT"`
- `"BACK"`
- `"DOWN"`
- `"EXIT"`
- `"RED"`
- `"GREEN"`
- `"YELLOW"`
- `"BLUE"`
- `"HOME"`
- `"CHANNELUP"`
- `"MUTE"`
- `"INFO"`
- `"CHANNELDOWN"`
- `"PLAY"`
- `"PAUSE"`
- `"STOP"`
- `"REWIND"`
- `"RECORD"`
- `"FAST_FOWARD"`
- `"POWER"`
- `"VOLUME_UP"`
- `"VOLUME_DOWN"`
1 change: 0 additions & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const CARD_VERSION = "v@LG_REMOTE_CONTROL_CARD_VERSION_PLACEHOLDER@";

export const CARD_TAG_NAME = "lg-remote-control";
export const EDITOR_CARD_TAG_NAME = "lg-remote-control-editor";
47 changes: 32 additions & 15 deletions src/lg-remote-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class LgRemoteControl extends LitElement {
${stateObj.state === 'off' ? html`
<button class="btn ripple" @click=${() => this._media_player_turn_on(mac)}><ha-icon icon="mdi:power" style="color: ${textColor};"/></button>
` : html`
<button class="btn ripple" @click=${() => this._media_player_service("turn_off")}><ha-icon icon="mdi:power" style="color: red;"/></button>
<button class="btn ripple" @click=${() => this._media_player_service("POWER", "turn_off")}><ha-icon icon="mdi:power" style="color: red;"/></button>
`}
<button class="btn-flat flat-high ripple" @click=${() => this._show_keypad = !this._show_keypad}>123</button>
</div>
Expand Down Expand Up @@ -261,12 +261,12 @@ class LgRemoteControl extends LitElement {
<!-- ################################# MEDIA CONTROL ################################# -->
<div class="grid-container-media-control" >
<button class="btn-flat flat-low ripple" @click=${() => this._command("media.controls/play")}><ha-icon icon="mdi:play"/></button>
<button class="btn-flat flat-low ripple" @click=${() => this._command("media.controls/pause")}><ha-icon icon="mdi:pause"/></button>
<button class="btn-flat flat-low ripple" @click=${() => this._command("media.controls/stop")}><ha-icon icon="mdi:stop"/></button>
<button class="btn-flat flat-low ripple" @click=${() => this._command("media.controls/rewind")}><ha-icon icon="mdi:skip-backward"/></button>
<button class="btn-flat flat-low ripple" style="color: red;" @click=${() => this._command("media.controls/Record")}><ha-icon icon="mdi:record"/></button>
<button class="btn-flat flat-low ripple" @click=${() => this._command("media.controls/fastForward")}><ha-icon icon="mdi:skip-forward"/></button>
<button class="btn-flat flat-low ripple" @click=${() => this._command("PLAY", "media.controls/play")}><ha-icon icon="mdi:play"/></button>
<button class="btn-flat flat-low ripple" @click=${() => this._command("PAUSE", "media.controls/pause")}><ha-icon icon="mdi:pause"/></button>
<button class="btn-flat flat-low ripple" @click=${() => this._command("STOP", "media.controls/stop")}><ha-icon icon="mdi:stop"/></button>
<button class="btn-flat flat-low ripple" @click=${() => this._command("REWIND", "media.controls/rewind")}><ha-icon icon="mdi:skip-backward"/></button>
<button class="btn-flat flat-low ripple" style="color: red;" @click=${() => this._command("RECORD", "media.controls/Record")}><ha-icon icon="mdi:record"/></button>
<button class="btn-flat flat-low ripple" @click=${() => this._command("FAST_FOWARD", "media.controls/fastForward")}><ha-icon icon="mdi:skip-forward"/></button>
</div>
<!-- ################################# MEDIA CONTROL END ################################# -->
</div>
Expand Down Expand Up @@ -297,30 +297,31 @@ class LgRemoteControl extends LitElement {
}

_button(button) {
this.hass.callService("webostv", "button", {
this.callServiceFromConfig(button, "webostv.button", {
entity_id: this.config.entity,
button: button
});
})
}

_command(command) {
this.hass.callService("webostv", "command", {
_command(button, command) {
this.callServiceFromConfig(button, "webostv.command", {
entity_id: this.config.entity,
command: command
});
}

_media_player_turn_on(mac) {
if (this.config.mac) {
this.hass.callService("wake_on_lan", "send_magic_packet", {
mac: mac
});
} else {
this._media_player_service("turn_on");
this._media_player_service("POWER", "turn_on");
}
}

_media_player_service(service) {
this.hass.callService("media_player", service, {
_media_player_service(button, service) {
this.callServiceFromConfig(button, `media_player.${service}`, {
entity_id: this.config.entity,
});
}
Expand All @@ -335,7 +336,7 @@ class LgRemoteControl extends LitElement {

// Funzione per aggiornare e chiamare il servizio
const updateValue = (service) => {
this.hass.callService("media_player", service, {
this.callServiceFromConfig(service.toUpperCase(), `media_player.${service}`, {
entity_id: this.output_entity,
});
};
Expand Down Expand Up @@ -477,6 +478,22 @@ class LgRemoteControl extends LitElement {
return 15;
}

callServiceFromConfig(key: string, service: string, serviceData: Record<string, any>) {
let serviceToUse = service;
let serviceDataToUse = serviceData;
if(this.config.keys && key in this.config.keys) {
const keyConfig = this.config.keys[key];
serviceToUse = keyConfig["service"];
serviceDataToUse = keyConfig["data"];
}
this.hass.callService(
serviceToUse.split(".")[0],
serviceToUse.split(".")[1],
serviceDataToUse
);

}

static getIcon(iconName) {
return Object.keys(LgRemoteControl.iconMapping).includes(iconName)
? LgRemoteControl.iconMapping[iconName]
Expand Down

0 comments on commit c801c62

Please sign in to comment.