This commit is contained in:
tjamieg 2024-12-28 23:00:27 +00:00 committed by GitHub
commit befebc6e6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 10 deletions

View File

@ -1,16 +1,20 @@
import logging import logging
from typing import Any, Mapping from typing import Any, Mapping
from homeassistant.components.media_player import ( from homeassistant.components.media_player import MediaPlayerEntity
DEVICE_CLASS_SPEAKER,
MediaPlayerEntity,
)
from homeassistant.components.media_player.const import MediaPlayerEntityFeature from homeassistant.components.media_player.const import MediaPlayerEntityFeature
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import DeviceInfo, generate_entity_id from homeassistant.helpers.entity import DeviceInfo, generate_entity_id
from homeassistant.helpers import config_validation as cv, entity_platform, selector from homeassistant.helpers import config_validation as cv, entity_platform, selector
import voluptuous as vol import voluptuous as vol
# Modification: Added try/except to handle fallback for MediaPlayerDeviceClass
try:
from homeassistant.components.media_player.const import MediaPlayerDeviceClass
HAS_MEDIAPLAYERDEVICECLASS = True
except ImportError:
HAS_MEDIAPLAYERDEVICECLASS = False
from .api_extension.SoundbarDevice import SoundbarDevice from .api_extension.SoundbarDevice import SoundbarDevice
from .api_extension.const import SpeakerIdentifier, RearSpeakerMode from .api_extension.const import SpeakerIdentifier, RearSpeakerMode
from .const import ( from .const import (
@ -105,8 +109,6 @@ def addServices():
) )
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
domain_data = hass.data[DOMAIN] domain_data = hass.data[DOMAIN]
@ -148,7 +150,12 @@ class SmartThingsSoundbarMediaPlayer(MediaPlayerEntity):
@property @property
def device_class(self): def device_class(self):
return DEVICE_CLASS_SPEAKER # Modification: Added fallback to DEVICE_CLASS_SPEAKER for backward compatibility
return (
MediaPlayerDeviceClass.SPEAKER
if HAS_MEDIAPLAYERDEVICECLASS
else "speaker"
)
@property @property
def supported_features(self): def supported_features(self):
@ -171,6 +178,7 @@ class SmartThingsSoundbarMediaPlayer(MediaPlayerEntity):
await self.device.switch_on() await self.device.switch_on()
# ---------- VOLUME ------------ # ---------- VOLUME ------------
@property @property
def volume_level(self): def volume_level(self):
return self.device.volume_level return self.device.volume_level
@ -218,6 +226,7 @@ class SmartThingsSoundbarMediaPlayer(MediaPlayerEntity):
await self.device.select_sound_mode(sound_mode) await self.device.select_sound_mode(sound_mode)
# ---------- MEDIA ------------ # ---------- MEDIA ------------
@property @property
def media_title(self): def media_title(self):
return self.device.media_title return self.device.media_title
@ -257,7 +266,7 @@ class SmartThingsSoundbarMediaPlayer(MediaPlayerEntity):
async def async_media_stop(self): async def async_media_stop(self):
await self.device.media_stop() await self.device.media_stop()
# ---------- SERVICE_UTILITY ------------ # ---------- SERVICE UTILITY ------------
async def async_set_woofer_level(self, level: int): async def async_set_woofer_level(self, level: int):
await self.device.set_woofer(level) await self.device.set_woofer(level)
@ -271,8 +280,6 @@ class SmartThingsSoundbarMediaPlayer(MediaPlayerEntity):
async def async_set_night_mode(self, enabled: bool): async def async_set_night_mode(self, enabled: bool):
await self.device.set_night_mode(enabled) await self.device.set_night_mode(enabled)
# ---------- SERVICE_UTILITY ------------
async def async_set_speaker_level(self, speaker_identifier: str, level: int): async def async_set_speaker_level(self, speaker_identifier: str, level: int):
await self.device.set_speaker_level( await self.device.set_speaker_level(
SpeakerIdentifier(speaker_identifier), level SpeakerIdentifier(speaker_identifier), level