Skip to content

Commit

Permalink
Add icons to select, sensor and switches, change some logging stateme…
Browse files Browse the repository at this point in the history
…nts (#16)

#### Added

- Icons for the individual entities

#### Changed

- Updated the GitHub actions workflows
- Change "magic numbers" to MediaPlayerEntityFeature object. For more information see https://developers.home-assistant.io/blog/2023/12/28/support-feature-magic-numbers-deprecation
- `source` now returns the value `wifi` when the `media_app_name` is *AirPlay* or *Spotify*
- removed some unnecessary logging statements, and changed others to debug
  • Loading branch information
samuelspagl authored Mar 12, 2024
1 parent dd61dec commit 5e24680
Show file tree
Hide file tree
Showing 12 changed files with 1,232 additions and 822 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ jobs:
contents: write
steps:
- name: "Checkout the repository"
uses: "actions/checkout@v3.5.3"
uses: "actions/checkout@v4.1.0"

- name: "Adjust version number"
shell: "bash"
run: |
yq -i -o json '.version="${{ github.event.release.tag_name }}"' \
"${{ github.workspace }}/custom_components/samsung_soundbar/manifest.json"
- name: "ZIP the integration directory"
shell: "bash"
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## [0.3.0] Icons and Chore

### Added

- Icons for the individual entities

### Changed

- Updated the GitHub actions workflows
- Change "magic numbers" to `MediaPlayerEntityFeature` object
For more information see https://developers.home-assistant.io/blog/2023/12/28/support-feature-magic-numbers-deprecation
- the `source` now returns the value `wifi` when the `media_app_name` is `AirPlay` or `Spotify`
- removed some unnecessary logging statements, and changed others to `debug`

## [0.2.1] Chore: Format repository - 2024-02-08

### Changed
Expand Down
1,929 changes: 1,148 additions & 781 deletions Pipfile.lock

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions custom_components/samsung_soundbar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up component from a config entry, config_entry contains data from config entry database."""
# store shell object

_LOGGER.info(f"[{DOMAIN}] Starting to setup ConfigEntry {entry.data}")
_LOGGER.info(f"[{DOMAIN}] Starting to setup a ConfigEntry")
_LOGGER.debug(f"[{DOMAIN}] Setting up ConfigEntry with the following data: {entry.data}")
if not DOMAIN in hass.data:
_LOGGER.info(f"[{DOMAIN}] Domain not found in hass.data setting default")
_LOGGER.debug(f"[{DOMAIN}] Domain not found in hass.data setting default")
hass.data[DOMAIN] = SoundbarConfig(
SmartThings(
async_get_clientsession(hass), entry.data.get(CONF_ENTRY_API_KEY)
Expand All @@ -31,10 +32,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)

domain_config: SoundbarConfig = hass.data[DOMAIN]
_LOGGER.info(f"[{DOMAIN}] Retrieved Domain Config: {domain_config}")
_LOGGER.debug(f"[{DOMAIN}] Retrieved Domain Config: {domain_config}")

if not entry.data.get(CONF_ENTRY_DEVICE_ID) in domain_config.devices:
_LOGGER.info(
_LOGGER.info(f"[{DOMAIN}] Setting up new Soundbar device")
_LOGGER.debug(
f"[{DOMAIN}] DeviceId: {entry.data.get(CONF_ENTRY_DEVICE_ID)} not found in domain_config, setting up new device."
)
smart_things_device = await domain_config.api.device(
Expand All @@ -51,7 +53,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
domain_config.devices[entry.data.get(CONF_ENTRY_DEVICE_ID)] = DeviceConfig(
entry.data, soundbar_device
)
_LOGGER.info(f"[{DOMAIN}] after initializing Soundbar device")
_LOGGER.info(f"[{DOMAIN}] Successfully initialized new Soundbar device")

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ async def set_woofer(self, level: int):

@property
def input_source(self):
if self.media_app_name in ("AirPlay", "Spotify"):
return "wifi"
return self.device.status.input_source

@property
Expand Down
7 changes: 2 additions & 5 deletions custom_components/samsung_soundbar/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,20 @@ async def validate_input(api, device_id: str):

class ExampleConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_user(self, user_input=None):
_LOGGER.error(f"Example Flow starts with user_input {user_input}")
if user_input is not None:
_LOGGER.error(f"User Input is not filled")
try:
session = async_get_clientsession(self.hass)
api = pysmartthings.SmartThings(
session, user_input.get(CONF_ENTRY_API_KEY)
)
_LOGGER.error(f"Validating Input {user_input}")
device = await validate_input(api, user_input.get(CONF_ENTRY_DEVICE_ID))

_LOGGER.error(
_LOGGER.debug(
f"Successfully validated Input, Creating entry with title {DOMAIN} and data {user_input}"
)
return self.async_create_entry(title=DOMAIN, data=user_input)
except Exception as excp:
_LOGGER.error(f"Example Flow triggered an exception {excp}")
_LOGGER.error(f"The ConfigFlow triggered an exception {excp}")
return self.async_abort(reason="fetch_failed")

return self.async_show_form(
Expand Down
2 changes: 1 addition & 1 deletion custom_components/samsung_soundbar/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/samuelspagl/ha_samsung_soundbar/issues",
"requirements": ["pysmartthings"],
"version": "0.2.0"
"version": "0.3.0"
}
26 changes: 12 additions & 14 deletions custom_components/samsung_soundbar/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

from homeassistant.components.media_player import (DEVICE_CLASS_SPEAKER,
MediaPlayerEntity)
from homeassistant.components.media_player.const import (
SUPPORT_PAUSE, SUPPORT_PLAY, SUPPORT_SELECT_SOUND_MODE,
SUPPORT_SELECT_SOURCE, SUPPORT_STOP, SUPPORT_TURN_OFF, SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET, SUPPORT_VOLUME_STEP)
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

Expand All @@ -21,16 +19,16 @@
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_STOP
| 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.STOP
| MediaPlayerEntityFeature.SELECT_SOUND_MODE
)


Expand Down
33 changes: 21 additions & 12 deletions custom_components/samsung_soundbar/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
device = device_config.device
if device.device_id == config_entry.data.get(CONF_ENTRY_DEVICE_ID):
entities.append(
EqPresetSelectEntity(
device,
"eq_preset",
)
EqPresetSelectEntity(device, "eq_preset", "mdi:tune-vertical")
)
entities.append(
SoundModeSelectEntity(
device,
"sound_mode_preset",
)
SoundModeSelectEntity(device, "sound_mode_preset", "mdi:surround-sound")
)
entities.append(
InputSelectEntity(
device,
"input_preset",
)
InputSelectEntity(device, "input_preset", "mdi:video-input-hdmi")
)
async_add_entities(entities)
return True
Expand All @@ -48,11 +39,13 @@ def __init__(
self,
device: SoundbarDevice,
append_unique_id: str,
icon_string: str,
):
self.entity_id = f"number.{device.device_name}_{append_unique_id}"
self.entity_description = SelectEntityDescription(
key=append_unique_id,
)
self.__base_icon = icon_string
self.__device = device
self._attr_unique_id = f"{device.device_id}_sw_{append_unique_id}"
self._attr_device_info = DeviceInfo(
Expand All @@ -72,6 +65,10 @@ def __init__(
def name(self):
return self.__append_unique_id

@property
def icon(self) -> str | None:
return self.__base_icon

# ------ STATE FUNCTIONS --------

@property
Expand All @@ -90,11 +87,13 @@ def __init__(
self,
device: SoundbarDevice,
append_unique_id: str,
icon_string: str,
):
self.entity_id = f"number.{device.device_name}_{append_unique_id}"
self.entity_description = SelectEntityDescription(
key=append_unique_id,
)
self.__base_icon = icon_string
self.__device = device
self._attr_unique_id = f"{device.device_id}_sw_{append_unique_id}"
self._attr_device_info = DeviceInfo(
Expand All @@ -114,6 +113,10 @@ def __init__(
def name(self):
return self.__append_unique_id

@property
def icon(self) -> str | None:
return self.__base_icon

# ------ STATE FUNCTIONS --------

@property
Expand All @@ -132,11 +135,13 @@ def __init__(
self,
device: SoundbarDevice,
append_unique_id: str,
icon_string: str,
):
self.entity_id = f"number.{device.device_name}_{append_unique_id}"
self.entity_description = SelectEntityDescription(
key=append_unique_id,
)
self.__base_icon = icon_string
self.__device = device
self._attr_unique_id = f"{device.device_id}_sw_{append_unique_id}"
self._attr_device_info = DeviceInfo(
Expand All @@ -156,6 +161,10 @@ def __init__(
def name(self):
return self.__append_unique_id

@property
def icon(self) -> str | None:
return self.__base_icon

# ------ STATE FUNCTIONS --------

@property
Expand Down
9 changes: 7 additions & 2 deletions custom_components/samsung_soundbar/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
device = device_config.device

if device.device_id == config_entry.data.get(CONF_ENTRY_DEVICE_ID):
entities.append(VolumeSensor(device, "volume_level"))
entities.append(VolumeSensor(device, "volume_level", "mdi:volume-high"))
async_add_entities(entities)
return True


class VolumeSensor(SensorEntity):
def __init__(self, device: SoundbarDevice, append_unique_id: str):
def __init__(self, device: SoundbarDevice, append_unique_id: str, icon_string: str):
self.entity_id = f"sensor.{device.device_name}_{append_unique_id}"
self.__device = device
self._attr_unique_id = f"{device.device_id}_sw_{append_unique_id}"
self.__base_icon = icon_string
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self.__device.device_id)},
name=self.__device.device_name,
Expand All @@ -40,6 +41,10 @@ def __init__(self, device: SoundbarDevice, append_unique_id: str):

_attr_device_class = SensorDeviceClass.VOLUME

@property
def icon(self) -> str | None:
return self.__base_icon

def update(self) -> None:
"""Fetch new state data for the sensor.
Expand Down
9 changes: 9 additions & 0 deletions custom_components/samsung_soundbar/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
lambda: device.night_mode,
device.set_night_mode,
device.set_night_mode,
"mdi:weather-night",
)
)
entities.append(
Expand All @@ -34,6 +35,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
lambda: device.bass_mode,
device.set_bass_mode,
device.set_bass_mode,
"mdi:speaker-wireless",
)
)
entities.append(
Expand All @@ -43,6 +45,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
lambda: device.voice_amplifier,
device.set_voice_amplifier,
device.set_voice_amplifier,
"mdi:account-voice",
)
)
async_add_entities(entities)
Expand All @@ -57,12 +60,14 @@ def __init__(
state_function,
on_function,
off_function,
icon_string: str = "mdi:toggle-switch-variant",
):
self.entity_id = f"switch.{device.device_name}_{append_unique_id}"

self.__device = device
self._name = f"{self.__device.device_name} {append_unique_id}"
self._attr_unique_id = f"{device.device_id}_sw_{append_unique_id}"
self.__base_icon = icon_string
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self.__device.device_id)},
name=self.__device.device_name,
Expand All @@ -85,6 +90,10 @@ def name(self):
def update(self):
self.__state = self.__state_function()

@property
def icon(self) -> str | None:
return self.__base_icon

# ------ STATE FUNCTIONS --------
@property
def state(self):
Expand Down
3 changes: 2 additions & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "Samsung Soundbar",
"filename": "samsung_soundbar.zip",
"render_readme": true,
"zip_release": true
"zip_release": true,
"homeassistant": "2024.1.0"
}

0 comments on commit 5e24680

Please sign in to comment.