mirror of
https://github.com/home-assistant/core.git
synced 2025-04-30 04:07:51 +00:00
Use POWER device class in eliqonline (#83810)
This commit is contained in:
parent
3b117998fb
commit
431df618c3
@ -10,10 +10,11 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorStateClass,
|
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.core import HomeAssistant
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
@ -26,12 +27,8 @@ CONF_CHANNEL_ID = "channel_id"
|
|||||||
|
|
||||||
DEFAULT_NAME = "ELIQ Online"
|
DEFAULT_NAME = "ELIQ Online"
|
||||||
|
|
||||||
ICON = "mdi:gauge"
|
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(seconds=60)
|
SCAN_INTERVAL = timedelta(seconds=60)
|
||||||
|
|
||||||
UNIT_OF_MEASUREMENT = POWER_WATT
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_ACCESS_TOKEN): cv.string,
|
vol.Required(CONF_ACCESS_TOKEN): cv.string,
|
||||||
@ -68,41 +65,22 @@ async def async_setup_platform(
|
|||||||
class EliqSensor(SensorEntity):
|
class EliqSensor(SensorEntity):
|
||||||
"""Implementation of an ELIQ Online sensor."""
|
"""Implementation of an ELIQ Online sensor."""
|
||||||
|
|
||||||
|
_attr_device_class = SensorDeviceClass.POWER
|
||||||
|
_attr_native_unit_of_measurement = UnitOfPower.WATT
|
||||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||||
|
|
||||||
def __init__(self, api, channel_id, name):
|
def __init__(self, api, channel_id, name):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._name = name
|
self._attr_name = name
|
||||||
self._state = None
|
|
||||||
self._api = api
|
self._api = api
|
||||||
self._channel_id = channel_id
|
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:
|
async def async_update(self) -> None:
|
||||||
"""Get the latest data."""
|
"""Get the latest data."""
|
||||||
try:
|
try:
|
||||||
response = await self._api.get_data_now(channelid=self._channel_id)
|
response = await self._api.get_data_now(channelid=self._channel_id)
|
||||||
self._state = int(response["power"])
|
self._attr_native_value = int(response["power"])
|
||||||
_LOGGER.debug("Updated power from server %d W", self._state)
|
_LOGGER.debug("Updated power from server %d W", self.native_value)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
_LOGGER.warning("Invalid response from ELIQ Online API")
|
_LOGGER.warning("Invalid response from ELIQ Online API")
|
||||||
except (OSError, asyncio.TimeoutError) as error:
|
except (OSError, asyncio.TimeoutError) as error:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user