Merge branch 'feature/add-volume-sensor' of https://github.com/samuelspagl/ha_samsung_soundbar into feature/add-volume-sensor

This commit is contained in:
samuelspagl 2024-01-15 14:28:39 +01:00
commit c61b2ba3f2
1 changed files with 8 additions and 7 deletions

View File

@ -1,16 +1,14 @@
import logging
from homeassistant.components.sensor import (SensorDeviceClass, SensorEntity,
SensorStateClass)
from homeassistant.helpers.entity import DeviceInfo
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 .models import DeviceConfig
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 = []
@ -19,11 +17,14 @@ 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")
)
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}"
@ -45,4 +46,4 @@ class VolumeSensor(SensorEntity):
This is the only method that should fetch new data for Home Assistant.
"""
self._attr_native_value = self.__device.volume_level * 100
self._attr_native_value = self.__device.device.status.volume