Skip to content

Commit

Permalink
Adjust to changes in HA 2025.5
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMachowski committed May 1, 2024
1 parent 59c6777 commit 299ff2b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 46 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ media_player:
## Getting API key and device id
Make sure your device is connected to yout SmartThings account.
Make sure your device is connected to your SmartThings account.
Obtain an API key from https://account.smartthings.com/tokens
Obtain an API key by following [these steps](https://www.home-assistant.io/integrations/smartthings/#personal-access-token-pat).
Once you're signed in on that page, go to https://api.smartthings.com/v1/devices to see a list of your devices and their device IDs.
Expand Down
49 changes: 25 additions & 24 deletions custom_components/smartthings_soundbar/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json

import requests
from homeassistant.const import (STATE_IDLE, STATE_OFF, STATE_ON, STATE_PAUSED, STATE_PLAYING)
from homeassistant.const import (STATE_OFF, STATE_ON, STATE_PAUSED, STATE_PLAYING)

API_BASEURL = "https://api.smartthings.com/v1"
API_DEVICES = API_BASEURL + "/devices/"
Expand All @@ -22,57 +22,58 @@
class SoundbarApi:

@staticmethod
def device_update(self):
API_KEY = self._api_key
def device_update(entity):
API_KEY = entity._api_key
REQUEST_HEADERS = {"Authorization": "Bearer " + API_KEY}
DEVICE_ID = self._device_id
DEVICE_ID = entity._device_id
API_DEVICE = API_DEVICES + DEVICE_ID
API_DEVICE_STATUS = API_DEVICE + "/states"
API_COMMAND = API_DEVICE + "/commands"
cmdurl = requests.post(API_COMMAND, data=COMMAND_REFRESH, headers=REQUEST_HEADERS)
resp = requests.get(API_DEVICE_STATUS, headers=REQUEST_HEADERS)
data = resp.json()

device_volume = data['main']['volume']['value']
device_volume = min(int(device_volume) / self._max_volume, 1)
device_volume = min(int(device_volume) / entity._max_volume, 1)
switch_state = data['main']['switch']['value']
playback_state = data['main']['playbackStatus']['value']
device_source = data['main']['inputSource']['value']
device_all_sources = json.loads(data['main']['supportedInputSources']['value'])
device_muted = data['main']['mute']['value'] != "unmuted"

if switch_state == "on":
if device_source in CONTROLLABLE_SOURCES:
if device_source.lower() in CONTROLLABLE_SOURCES:
if playback_state == "playing":
self._state = STATE_PLAYING
entity._state = STATE_PLAYING
elif playback_state == "paused":
self._state = STATE_PAUSED
entity._state = STATE_PAUSED
else:
self._state = STATE_ON
entity._state = STATE_ON
else:
self._state = STATE_ON
entity._state = STATE_ON
else:
self._state = STATE_OFF
self._volume = device_volume
self._source_list = device_all_sources if type(device_all_sources) is list else device_all_sources["value"]
self._muted = device_muted
self._source = device_source
if self._state in [STATE_PLAYING, STATE_PAUSED] and 'trackDescription' in data['main']:
self._media_title = data['main']['trackDescription']['value']
entity._state = STATE_OFF
entity._volume = device_volume
entity._source_list = device_all_sources if type(device_all_sources) is list else device_all_sources["value"]
entity._muted = device_muted
entity._source = device_source
if entity._state in [STATE_PLAYING, STATE_PAUSED] and 'trackDescription' in data['main']:
entity._media_title = data['main']['trackDescription']['value']
else:
self._media_title = None
entity._media_title = None

@staticmethod
def send_command(self, argument, cmdtype):
API_KEY = self._api_key
def send_command(entity, argument, cmdtype):
API_KEY = entity._api_key
REQUEST_HEADERS = {"Authorization": "Bearer " + API_KEY}
DEVICE_ID = self._device_id
DEVICE_ID = entity._device_id
API_DEVICES = API_BASEURL + "/devices/"
API_DEVICE = API_DEVICES + DEVICE_ID
API_COMMAND = API_DEVICE + "/commands"

if cmdtype == "setvolume": # sets volume
API_COMMAND_DATA = "{'commands':[{'component': 'main','capability': 'audioVolume','command': 'setVolume','arguments': "
volume = int(argument * self._max_volume)
volume = int(argument * entity._max_volume)
API_COMMAND_ARG = "[{}]}}]}}".format(volume)
API_FULL = API_COMMAND_DATA + API_COMMAND_ARG
cmdurl = requests.post(API_COMMAND, data=API_FULL, headers=REQUEST_HEADERS)
Expand All @@ -84,7 +85,7 @@ def send_command(self, argument, cmdtype):
API_COMMAND_DATA = "{'commands':[{'component': 'main','capability': 'audioVolume','command': 'volumeDown'}]}"
cmdurl = requests.post(API_COMMAND, data=API_COMMAND_DATA, headers=REQUEST_HEADERS)
elif cmdtype == "audiomute": # mutes audio
if self._muted == False:
if entity._muted == False:
cmdurl = requests.post(API_COMMAND, data=COMMAND_MUTE, headers=REQUEST_HEADERS)
else:
cmdurl = requests.post(API_COMMAND, data=COMMAND_UNMUTE, headers=REQUEST_HEADERS)
Expand Down Expand Up @@ -118,4 +119,4 @@ def send_command(self, argument, cmdtype):
]
}}"""
cmdurl = requests.post(API_COMMAND, data=API_COMMAND_DATA, headers=REQUEST_HEADERS)
self.async_schedule_update_ha_state()
entity.schedule_update_ha_state()
33 changes: 13 additions & 20 deletions custom_components/smartthings_soundbar/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,10 @@

from homeassistant.components.media_player import (
MediaPlayerEntity,
MediaPlayerEntityFeature,
PLATFORM_SCHEMA,
DEVICE_CLASS_SPEAKER,
)
from homeassistant.components.media_player.const import (
SUPPORT_PAUSE,
SUPPORT_PLAY,
SUPPORT_SELECT_SOURCE,
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_STEP,
SUPPORT_VOLUME_SET,
SUPPORT_SELECT_SOUND_MODE
)
from homeassistant.const import (
CONF_NAME, CONF_API_KEY, CONF_DEVICE_ID
)
Expand All @@ -30,15 +20,15 @@
CONF_MAX_VOLUME = "max_volume"

SUPPORT_SMARTTHINGS_SOUNDBAR = (
SUPPORT_PAUSE
| SUPPORT_VOLUME_STEP
| SUPPORT_VOLUME_MUTE
| SUPPORT_VOLUME_SET
| SUPPORT_SELECT_SOURCE
| SUPPORT_TURN_OFF
| SUPPORT_TURN_ON
| SUPPORT_PLAY
| SUPPORT_SELECT_SOUND_MODE
MediaPlayerEntityFeature.PAUSE
| MediaPlayerEntityFeature.VOLUME_STEP
| MediaPlayerEntityFeature.VOLUME_MUTE
| MediaPlayerEntityFeature.VOLUME_SET
| MediaPlayerEntityFeature.SELECT_SOURCE
| MediaPlayerEntityFeature.TURN_OFF
| MediaPlayerEntityFeature.TURN_ON
| MediaPlayerEntityFeature.PLAY
| MediaPlayerEntityFeature.SELECT_SOUND_MODE
)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
Expand Down Expand Up @@ -77,6 +67,9 @@ def __init__(self, name, api_key, device_id, max_volume):
def update(self):
SoundbarApi.device_update(self)

def unique_id(self) -> str | None:
return f"SmartThings_Soundbar_{self._device_id}"

def turn_off(self):
arg = ""
cmdtype = "switch_off"
Expand Down

0 comments on commit 299ff2b

Please sign in to comment.