diff --git a/custom_components/samsung_soundbar/__init__.py b/custom_components/samsung_soundbar/__init__.py index 31c5c48..09d9a4f 100644 --- a/custom_components/samsung_soundbar/__init__.py +++ b/custom_components/samsung_soundbar/__init__.py @@ -6,19 +6,14 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from pysmartthings import SmartThings from .api_extension.SoundbarDevice import SoundbarDevice -from .const import ( - CONF_ENTRY_API_KEY, - CONF_ENTRY_DEVICE_ID, - CONF_ENTRY_DEVICE_NAME, - CONF_ENTRY_MAX_VOLUME, - SUPPORTED_DOMAINS, - DOMAIN, -) +from .const import (CONF_ENTRY_API_KEY, CONF_ENTRY_DEVICE_ID, + CONF_ENTRY_DEVICE_NAME, CONF_ENTRY_MAX_VOLUME, DOMAIN, + SUPPORTED_DOMAINS) from .models import DeviceConfig, SoundbarConfig _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: @@ -47,15 +42,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) session = async_get_clientsession(hass) soundbar_device = SoundbarDevice( - smart_things_device, - session, - entry.data.get(CONF_ENTRY_MAX_VOLUME), - entry.data.get(CONF_ENTRY_DEVICE_NAME), - ) + smart_things_device, + session, + entry.data.get(CONF_ENTRY_MAX_VOLUME), + entry.data.get(CONF_ENTRY_DEVICE_NAME), + ) await soundbar_device.update() domain_config.devices[entry.data.get(CONF_ENTRY_DEVICE_ID)] = DeviceConfig( - entry.data, - soundbar_device + entry.data, soundbar_device ) _LOGGER.info(f"[{DOMAIN}] after initializing Soundbar device") diff --git a/custom_components/samsung_soundbar/manifest.json b/custom_components/samsung_soundbar/manifest.json index aa8db7f..2772074 100644 --- a/custom_components/samsung_soundbar/manifest.json +++ b/custom_components/samsung_soundbar/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "issue_tracker": "https://github.com/samuelspagl/ha_samsung_soundbar/issues", "requirements": ["pysmartthings"], - "version": "0.1.0" + "version": "0.2.0" } diff --git a/custom_components/samsung_soundbar/sensor.py b/custom_components/samsung_soundbar/sensor.py new file mode 100644 index 0000000..68fea44 --- /dev/null +++ b/custom_components/samsung_soundbar/sensor.py @@ -0,0 +1,48 @@ +import logging + +from homeassistant.components.sensor import (SensorDeviceClass, SensorEntity, + SensorStateClass) +from homeassistant.helpers.entity import DeviceInfo + +from .api_extension.SoundbarDevice import SoundbarDevice +from .const import CONF_ENTRY_DEVICE_ID, DOMAIN +from .models import DeviceConfig + +_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.volume_level * 100