Use POWER device class in eliqonline (#83810)

This commit is contained in:
epenet 2022-12-12 11:46:46 +01:00 committed by GitHub
parent 3b117998fb
commit 431df618c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,10 +10,11 @@ import voluptuous as vol
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_NAME, POWER_WATT
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_NAME, UnitOfPower
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
@ -26,12 +27,8 @@ CONF_CHANNEL_ID = "channel_id"
DEFAULT_NAME = "ELIQ Online"
ICON = "mdi:gauge"
SCAN_INTERVAL = timedelta(seconds=60)
UNIT_OF_MEASUREMENT = POWER_WATT
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_ACCESS_TOKEN): cv.string,
@ -68,41 +65,22 @@ async def async_setup_platform(
class EliqSensor(SensorEntity):
"""Implementation of an ELIQ Online sensor."""
_attr_device_class = SensorDeviceClass.POWER
_attr_native_unit_of_measurement = UnitOfPower.WATT
_attr_state_class = SensorStateClass.MEASUREMENT
def __init__(self, api, channel_id, name):
"""Initialize the sensor."""
self._name = name
self._state = None
self._attr_name = name
self._api = api
self._channel_id = channel_id
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def icon(self):
"""Return icon."""
return ICON
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return UNIT_OF_MEASUREMENT
@property
def native_value(self):
"""Return the state of the device."""
return self._state
async def async_update(self) -> None:
"""Get the latest data."""
try:
response = await self._api.get_data_now(channelid=self._channel_id)
self._state = int(response["power"])
_LOGGER.debug("Updated power from server %d W", self._state)
self._attr_native_value = int(response["power"])
_LOGGER.debug("Updated power from server %d W", self.native_value)
except KeyError:
_LOGGER.warning("Invalid response from ELIQ Online API")
except (OSError, asyncio.TimeoutError) as error: