Fix scaling on sensor

Previously the sensor was displaying the scaled amount applied to the slider / number (between 0 and 1). Now it is displaying the raw value.
This commit is contained in:
Samuel Spagl 2024-01-02 21:18:34 +01:00 committed by GitHub
parent cf80dfdb07
commit b5794f3c1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 7 deletions

View File

@ -1,16 +1,14 @@
import logging import logging
from homeassistant.components.sensor import (SensorDeviceClass, SensorEntity, from homeassistant.components.sensor import SensorEntity, SensorDeviceClass, SensorStateClass
SensorStateClass)
from homeassistant.helpers.entity import DeviceInfo
from .models import DeviceConfig
from .api_extension.SoundbarDevice import SoundbarDevice from .api_extension.SoundbarDevice import SoundbarDevice
from .const import CONF_ENTRY_DEVICE_ID, DOMAIN from .const import CONF_ENTRY_DEVICE_ID, DOMAIN
from .models import DeviceConfig from homeassistant.helpers.entity import DeviceInfo
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
domain_data = hass.data[DOMAIN] domain_data = hass.data[DOMAIN]
entities = [] entities = []
@ -19,11 +17,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
device = device_config.device device = device_config.device
if device.device_id == config_entry.data.get(CONF_ENTRY_DEVICE_ID): 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) async_add_entities(entities)
return True return True
class VolumeSensor(SensorEntity): class VolumeSensor(SensorEntity):
def __init__(self, device: SoundbarDevice, append_unique_id: str): def __init__(self, device: SoundbarDevice, append_unique_id: str):
self.entity_id = f"sensor.{device.device_name}_{append_unique_id}" 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. 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