✨🐛: add volume `sensor` entity, fix sound_mode/source in `media_player` entity
This merge-request adds new functionality as well as fixes a minor bug. For more information see #9. - New sensor entity added - Disabled extra_state_attributes to correctly display available sources/soundmodes in the media_player configuration. --------- Co-authored-by: samuelspagl <samuel@spagl-media.de>
This commit is contained in:
		
							parent
							
								
									78e825157e
								
							
						
					
					
						commit
						30ef090d9e
					
				|  | @ -2,6 +2,7 @@ | ||||||
| __pycache__/ | __pycache__/ | ||||||
| *.py[cod] | *.py[cod] | ||||||
| *$py.class | *$py.class | ||||||
|  | .DS_store | ||||||
| 
 | 
 | ||||||
| # C extensions | # C extensions | ||||||
| *.so | *.so | ||||||
|  |  | ||||||
							
								
								
									
										18
									
								
								CHANGELOG.md
								
								
								
								
							
							
						
						
									
										18
									
								
								CHANGELOG.md
								
								
								
								
							|  | @ -0,0 +1,18 @@ | ||||||
|  | # Changelog | ||||||
|  | 
 | ||||||
|  | ## [0.2.0] Add volume sensor - 2024-02-08 | ||||||
|  | 
 | ||||||
|  | ### Added | ||||||
|  | 
 | ||||||
|  | - add new sensor entity for the volume | ||||||
|  |    | ||||||
|  | ### Fix | ||||||
|  | 
 | ||||||
|  | - remove `extra_state_attributes` from `media_player` instance: | ||||||
|  |   The property caused some unwanted side-effects on some systems. | ||||||
|  | 
 | ||||||
|  | ## [0.1.0] 🎉 First Version | ||||||
|  | 
 | ||||||
|  | ### Added | ||||||
|  | 
 | ||||||
|  | - first version, gonna extend this Changelog sometime :D | ||||||
|  | @ -6,19 +6,14 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||||||
| from pysmartthings import SmartThings | from pysmartthings import SmartThings | ||||||
| 
 | 
 | ||||||
| from .api_extension.SoundbarDevice import SoundbarDevice | from .api_extension.SoundbarDevice import SoundbarDevice | ||||||
| from .const import ( | from .const import (CONF_ENTRY_API_KEY, CONF_ENTRY_DEVICE_ID, | ||||||
|     CONF_ENTRY_API_KEY, |                     CONF_ENTRY_DEVICE_NAME, CONF_ENTRY_MAX_VOLUME, DOMAIN, | ||||||
|     CONF_ENTRY_DEVICE_ID, |                     SUPPORTED_DOMAINS) | ||||||
|     CONF_ENTRY_DEVICE_NAME, |  | ||||||
|     CONF_ENTRY_MAX_VOLUME, |  | ||||||
|     SUPPORTED_DOMAINS, |  | ||||||
|     DOMAIN, |  | ||||||
| ) |  | ||||||
| from .models import DeviceConfig, SoundbarConfig | from .models import DeviceConfig, SoundbarConfig | ||||||
| 
 | 
 | ||||||
| _LOGGER = logging.getLogger(__name__) | _LOGGER = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
| PLATFORMS = ["media_player", "switch", "image", "number", "select"] | PLATFORMS = ["media_player", "switch", "image", "number", "select", "sensor"] | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||||||
|  | @ -54,8 +49,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||||||
|         ) |         ) | ||||||
|         await soundbar_device.update() |         await soundbar_device.update() | ||||||
|         domain_config.devices[entry.data.get(CONF_ENTRY_DEVICE_ID)] = DeviceConfig( |         domain_config.devices[entry.data.get(CONF_ENTRY_DEVICE_ID)] = DeviceConfig( | ||||||
|             entry.data, |             entry.data, soundbar_device | ||||||
|             soundbar_device |  | ||||||
|         ) |         ) | ||||||
|         _LOGGER.info(f"[{DOMAIN}] after initializing Soundbar device") |         _LOGGER.info(f"[{DOMAIN}] after initializing Soundbar device") | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -8,5 +8,5 @@ | ||||||
|   "iot_class": "cloud_polling", |   "iot_class": "cloud_polling", | ||||||
|   "issue_tracker": "https://github.com/samuelspagl/ha_samsung_soundbar/issues", |   "issue_tracker": "https://github.com/samuelspagl/ha_samsung_soundbar/issues", | ||||||
|   "requirements": ["pysmartthings"], |   "requirements": ["pysmartthings"], | ||||||
|   "version": "0.1.0" |   "version": "0.2.0" | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,5 +1,5 @@ | ||||||
| import logging | import logging | ||||||
| from typing import Mapping, Any | from typing import Any, Mapping | ||||||
| 
 | 
 | ||||||
| from homeassistant.components.media_player import ( | from homeassistant.components.media_player import ( | ||||||
|     DEVICE_CLASS_SPEAKER, |     DEVICE_CLASS_SPEAKER, | ||||||
|  | @ -20,7 +20,6 @@ from homeassistant.components.media_player.const import ( | ||||||
| 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 .models import DeviceConfig |  | ||||||
| from .api_extension.SoundbarDevice import SoundbarDevice | from .api_extension.SoundbarDevice import SoundbarDevice | ||||||
| from .const import ( | from .const import ( | ||||||
|     CONF_ENTRY_API_KEY, |     CONF_ENTRY_API_KEY, | ||||||
|  | @ -29,6 +28,7 @@ from .const import ( | ||||||
|     CONF_ENTRY_MAX_VOLUME, |     CONF_ENTRY_MAX_VOLUME, | ||||||
|     DOMAIN, |     DOMAIN, | ||||||
| ) | ) | ||||||
|  | from .models import DeviceConfig | ||||||
| 
 | 
 | ||||||
| _LOGGER = logging.getLogger(__name__) | _LOGGER = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
|  | @ -191,6 +191,8 @@ 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() | ||||||
| 
 | 
 | ||||||
|     @property |     # This property can be uncommented for some extra_attributes | ||||||
|     def extra_state_attributes(self) -> Mapping[str, Any] | None: |     # Still enabling this can cause side-effects. | ||||||
|         return self.device.retrieve_data |     # @property | ||||||
|  |     # def extra_state_attributes(self) -> Mapping[str, Any] | None: | ||||||
|  |     #     return {"device_information": self.device.retrieve_data} | ||||||
|  |  | ||||||
|  | @ -0,0 +1,49 @@ | ||||||
|  | import logging | ||||||
|  | 
 | ||||||
|  | from homeassistant.components.sensor import SensorEntity, SensorDeviceClass, SensorStateClass | ||||||
|  | 
 | ||||||
|  | from .models import DeviceConfig | ||||||
|  | from .api_extension.SoundbarDevice import SoundbarDevice | ||||||
|  | from .const import CONF_ENTRY_DEVICE_ID, DOMAIN | ||||||
|  | from homeassistant.helpers.entity import DeviceInfo | ||||||
|  | 
 | ||||||
|  | _LOGGER = logging.getLogger(__name__) | ||||||
|  | 
 | ||||||
|  | async def async_setup_entry(hass, config_entry, async_add_entities): | ||||||
|  |     domain_data = hass.data[DOMAIN] | ||||||
|  |     entities = [] | ||||||
|  |     for key in domain_data.devices: | ||||||
|  |         device_config: DeviceConfig = domain_data.devices[key] | ||||||
|  |         device = device_config.device | ||||||
|  | 
 | ||||||
|  |         if device.device_id == config_entry.data.get(CONF_ENTRY_DEVICE_ID): | ||||||
|  |             entities.append( | ||||||
|  |                 VolumeSensor(device, "volume_level") | ||||||
|  |             ) | ||||||
|  |     async_add_entities(entities) | ||||||
|  |     return True | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class VolumeSensor(SensorEntity): | ||||||
|  |     def __init__(self, device: SoundbarDevice, append_unique_id: 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._attr_device_info = DeviceInfo( | ||||||
|  |             identifiers={(DOMAIN, self.__device.device_id)}, | ||||||
|  |             name=self.__device.device_name, | ||||||
|  |             manufacturer=self.__device.manufacturer, | ||||||
|  |             model=self.__device.model, | ||||||
|  |             sw_version=self.__device.firmware_version, | ||||||
|  |         ) | ||||||
|  |         self.__append_unique_id = append_unique_id | ||||||
|  | 
 | ||||||
|  |         _attr_device_class = SensorDeviceClass.VOLUME | ||||||
|  | 
 | ||||||
|  |     def update(self) -> None: | ||||||
|  |         """Fetch new state data for the sensor. | ||||||
|  | 
 | ||||||
|  |         This is the only method that should fetch new data for Home Assistant. | ||||||
|  |         """ | ||||||
|  |         self._attr_native_value = self.__device.device.status.volume | ||||||
		Loading…
	
		Reference in New Issue