Add turn on and off function to lg_soundbar (#127022)

* Update media_player.py

Added support to toggle soundbars on and off

* Update homeassistant/components/lg_soundbar/media_player.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Fix complexity

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
pull/127354/head
skynet01 2024-10-02 12:04:14 -07:00 committed by GitHub
parent a3b1a30d06
commit a83d38a5fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 35 additions and 8 deletions

View File

@ -2,6 +2,8 @@
from __future__ import annotations
from typing import Any
import temescal
from homeassistant.components.media_player import (
@ -43,6 +45,8 @@ class LGDevice(MediaPlayerEntity):
_attr_supported_features = (
MediaPlayerEntityFeature.VOLUME_SET
| MediaPlayerEntityFeature.VOLUME_MUTE
| MediaPlayerEntityFeature.TURN_ON
| MediaPlayerEntityFeature.TURN_OFF
| MediaPlayerEntityFeature.SELECT_SOURCE
| MediaPlayerEntityFeature.SELECT_SOUND_MODE
)
@ -93,14 +97,7 @@ class LGDevice(MediaPlayerEntity):
"""Handle responses from the speakers."""
data = response.get("data") or {}
if response["msg"] == "EQ_VIEW_INFO":
if "i_bass" in data:
self._bass = data["i_bass"]
if "i_treble" in data:
self._treble = data["i_treble"]
if "ai_eq_list" in data:
self._equalisers = data["ai_eq_list"]
if "i_curr_eq" in data:
self._equaliser = data["i_curr_eq"]
self._update_equalisers(data)
elif response["msg"] == "SPK_LIST_VIEW_INFO":
if "i_vol" in data:
self._volume = data["i_vol"]
@ -112,6 +109,11 @@ class LGDevice(MediaPlayerEntity):
self._mute = data["b_mute"]
if "i_curr_func" in data:
self._function = data["i_curr_func"]
if "b_powerstatus" in data:
if data["b_powerstatus"]:
self._attr_state = MediaPlayerState.ON
else:
self._attr_state = MediaPlayerState.OFF
elif response["msg"] == "FUNC_VIEW_INFO":
if "i_curr_func" in data:
self._function = data["i_curr_func"]
@ -137,6 +139,17 @@ class LGDevice(MediaPlayerEntity):
self.schedule_update_ha_state()
def _update_equalisers(self, data: dict[str, Any]) -> None:
"""Update the equalisers."""
if "i_bass" in data:
self._bass = data["i_bass"]
if "i_treble" in data:
self._treble = data["i_treble"]
if "ai_eq_list" in data:
self._equalisers = data["ai_eq_list"]
if "i_curr_eq" in data:
self._equaliser = data["i_curr_eq"]
def update(self) -> None:
"""Trigger updates from the device."""
self._device.get_eq()
@ -204,3 +217,17 @@ class LGDevice(MediaPlayerEntity):
def select_sound_mode(self, sound_mode: str) -> None:
"""Set Sound Mode for Receiver.."""
self._device.set_eq(temescal.equalisers.index(sound_mode))
def turn_on(self) -> None:
"""Turn the media player on."""
self._set_power(True)
def turn_off(self) -> None:
"""Turn the media player off."""
self._set_power(False)
def _set_power(self, status: bool) -> None:
"""Set the media player state."""
self._device.send_packet(
{"cmd": "set", "data": {"b_powerkey": status}, "msg": "SPK_LIST_VIEW_INFO"}
)