From 252d3855caf9bd0344263425cb74748de6f421b8 Mon Sep 17 00:00:00 2001 From: samuelspagl Date: Fri, 5 Apr 2024 14:27:33 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Add=20Service=20calls=20for=20cu?= =?UTF-8?q?stom=20capabilities?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api_extension/SoundbarDevice.py | 2 + .../samsung_soundbar/media_player.py | 55 ++++++++++++ .../samsung_soundbar/services.yaml | 88 +++++++++++++++++++ .../samsung_soundbar/translations/en.json | 12 ++- 4 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 custom_components/samsung_soundbar/services.yaml diff --git a/custom_components/samsung_soundbar/api_extension/SoundbarDevice.py b/custom_components/samsung_soundbar/api_extension/SoundbarDevice.py index d724a4e..b32c41b 100644 --- a/custom_components/samsung_soundbar/api_extension/SoundbarDevice.py +++ b/custom_components/samsung_soundbar/api_extension/SoundbarDevice.py @@ -60,6 +60,8 @@ class SoundbarDevice: async def update(self): await self.device.status.refresh() + await self._update_media() + if self.__enable_soundmode: await self._update_soundmode() if self.__enable_advanced_audio: diff --git a/custom_components/samsung_soundbar/media_player.py b/custom_components/samsung_soundbar/media_player.py index 41acca1..5f2388a 100644 --- a/custom_components/samsung_soundbar/media_player.py +++ b/custom_components/samsung_soundbar/media_player.py @@ -8,6 +8,9 @@ from homeassistant.components.media_player import ( from homeassistant.components.media_player.const import MediaPlayerEntityFeature from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.entity import DeviceInfo, generate_entity_id +from homeassistant.helpers import config_validation as cv, entity_platform, selector +import voluptuous as vol + from .api_extension.SoundbarDevice import SoundbarDevice from .const import ( @@ -40,9 +43,47 @@ SUPPORT_SMARTTHINGS_SOUNDBAR = ( ) +def addServices(): + platform = entity_platform.async_get_current_platform() + + platform.async_register_entity_service( + "select_soundmode", + cv.make_entity_service_schema({vol.Required("sound_mode"): str}), + SmartThingsSoundbarMediaPlayer.async_select_sound_mode.__name__, + ) + + platform.async_register_entity_service( + "set_woofer_level", + cv.make_entity_service_schema( + {vol.Required("level"): vol.All(int, vol.Range(min=-12, max=6))} + ), + SmartThingsSoundbarMediaPlayer.async_set_woofer_level.__name__, + ) + + platform.async_register_entity_service( + "set_night_mode", + cv.make_entity_service_schema({vol.Required("enabled"): bool}), + SmartThingsSoundbarMediaPlayer.async_set_night_mode.__name__, + ) + + platform.async_register_entity_service( + "set_bass_enhancer", + cv.make_entity_service_schema({vol.Required("enabled"): bool}), + SmartThingsSoundbarMediaPlayer.async_set_bass_mode.__name__, + ) + + platform.async_register_entity_service( + "set_voice_enhancer", + cv.make_entity_service_schema({vol.Required("enabled"): bool}), + SmartThingsSoundbarMediaPlayer.async_set_voice_mode.__name__, + ) + + async def async_setup_entry(hass, config_entry, async_add_entities): domain_data = hass.data[DOMAIN] + addServices() + entities = [] for key in domain_data.devices: device_config: DeviceConfig = domain_data.devices[key] @@ -188,6 +229,20 @@ class SmartThingsSoundbarMediaPlayer(MediaPlayerEntity): async def async_media_stop(self): await self.device.media_stop() + # ---------- SERVICE_UTILITY ------------ + + async def async_set_woofer_level(self, level: int): + await self.device.set_woofer(level) + + async def async_set_bass_mode(self, enabled: bool): + await self.device.set_bass_mode(enabled) + + async def async_set_voice_mode(self, enabled: bool): + await self.device.set_voice_amplifier(enabled) + + async def async_set_night_mode(self, enabled: bool): + await self.device.set_night_mode(enabled) + # This property can be uncommented for some extra_attributes # Still enabling this can cause side-effects. # @property diff --git a/custom_components/samsung_soundbar/services.yaml b/custom_components/samsung_soundbar/services.yaml new file mode 100644 index 0000000..718a66d --- /dev/null +++ b/custom_components/samsung_soundbar/services.yaml @@ -0,0 +1,88 @@ + +select_soundmode: + name: Select Soundmode + description: Some Soundbars support different "sound modes". If supported you can select them here. + target: + device: + integration: samsung_soundbar + fields: + sound_mode: + name: Sound Mode + description: Select the Soundmode you are interested in. + required: true + example: "adaptive sound" + # The default field value + default: "standard" + # Selector (https://www.home-assistant.io/docs/blueprint/selectors/) to control + # the input UI for this field + selector: + select: + translation_key: "soundmode" + options: + - "standard" + - "surround" + - "game" + - "adaptive sound" + +set_woofer_level: + name: Set Woofer level + description: Set the subwoofer level of your soundbar + target: + device: + integration: samsung_soundbar + fields: + level: + name: Volume level + required: true + example: 3 + default: 0 + selector: + number: + min: -12 + max: 6 + step: 1 + +set_night_mode: + name: Set NightMode + description: Activates / deactivates the Nightmode + target: + device: + integration: samsung_soundbar + fields: + enabled: + name: Enabled / Disabled + required: true + example: true + default: false + selector: + boolean: + +set_bass_enhancer: + name: Set bass enhancement + description: Activates / deactivates the bass enhancement + target: + device: + integration: samsung_soundbar + fields: + enabled: + name: Enabled / Disabled + required: true + example: true + default: false + selector: + boolean: + +set_voice_enhancer: + name: Set voice enhancement + description: Activates / deactivates the voice enhancement + target: + device: + integration: samsung_soundbar + fields: + enabled: + name: Enabled / Disabled + required: true + example: true + default: false + selector: + boolean: \ No newline at end of file diff --git a/custom_components/samsung_soundbar/translations/en.json b/custom_components/samsung_soundbar/translations/en.json index 82399ea..27bf560 100644 --- a/custom_components/samsung_soundbar/translations/en.json +++ b/custom_components/samsung_soundbar/translations/en.json @@ -33,5 +33,15 @@ "title": "Device Settings" } } - } + }, + "selector": { + "soundmode": { + "options": { + "standard": "Standard", + "surround": "Surround", + "game": "Gaming", + "adaptive sound": "Adaptive Sound" + } + } + } } \ No newline at end of file