mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Updated ELIQ Online sensor to async API (#19248)
* Updated ELIQ Online sensor to async API * Remove use of STATE_UNKNOWN
This commit is contained in:
parent
f95bd9c78f
commit
004179775c
@ -10,11 +10,12 @@ import logging
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import (CONF_ACCESS_TOKEN, CONF_NAME, STATE_UNKNOWN)
|
from homeassistant.const import (CONF_ACCESS_TOKEN, CONF_NAME)
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
REQUIREMENTS = ['eliqonline==1.0.14']
|
REQUIREMENTS = ['eliqonline==1.2.2']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -35,24 +36,27 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities,
|
||||||
|
discovery_info=None):
|
||||||
"""Set up the ELIQ Online sensor."""
|
"""Set up the ELIQ Online sensor."""
|
||||||
import eliqonline
|
import eliqonline
|
||||||
|
|
||||||
access_token = config.get(CONF_ACCESS_TOKEN)
|
access_token = config.get(CONF_ACCESS_TOKEN)
|
||||||
name = config.get(CONF_NAME, DEFAULT_NAME)
|
name = config.get(CONF_NAME, DEFAULT_NAME)
|
||||||
channel_id = config.get(CONF_CHANNEL_ID)
|
channel_id = config.get(CONF_CHANNEL_ID)
|
||||||
|
session = async_get_clientsession(hass)
|
||||||
|
|
||||||
api = eliqonline.API(access_token)
|
api = eliqonline.API(session=session,
|
||||||
|
access_token=access_token)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
_LOGGER.debug("Probing for access to ELIQ Online API")
|
_LOGGER.debug("Probing for access to ELIQ Online API")
|
||||||
api.get_data_now(channelid=channel_id)
|
await api.get_data_now(channelid=channel_id)
|
||||||
except OSError as error:
|
except OSError as error:
|
||||||
_LOGGER.error("Could not access the ELIQ Online API: %s", error)
|
_LOGGER.error("Could not access the ELIQ Online API: %s", error)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
add_entities([EliqSensor(api, channel_id, name)], True)
|
async_add_entities([EliqSensor(api, channel_id, name)], True)
|
||||||
|
|
||||||
|
|
||||||
class EliqSensor(Entity):
|
class EliqSensor(Entity):
|
||||||
@ -61,7 +65,7 @@ class EliqSensor(Entity):
|
|||||||
def __init__(self, api, channel_id, name):
|
def __init__(self, api, channel_id, name):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._state = STATE_UNKNOWN
|
self._state = None
|
||||||
self._api = api
|
self._api = api
|
||||||
self._channel_id = channel_id
|
self._channel_id = channel_id
|
||||||
|
|
||||||
@ -85,12 +89,14 @@ class EliqSensor(Entity):
|
|||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
def update(self):
|
async def async_update(self):
|
||||||
"""Get the latest data."""
|
"""Get the latest data."""
|
||||||
try:
|
try:
|
||||||
response = 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._state = int(response["power"])
|
||||||
_LOGGER.debug("Updated power from server %d W", self._state)
|
_LOGGER.debug("Updated power from server %d W", self._state)
|
||||||
|
except KeyError:
|
||||||
|
_LOGGER.warning("Invalid response from ELIQ Online API")
|
||||||
except OSError as error:
|
except OSError as error:
|
||||||
_LOGGER.warning("Could not connect to the ELIQ Online API: %s",
|
_LOGGER.warning("Could not connect to the ELIQ Online API: %s",
|
||||||
error)
|
error)
|
||||||
|
@ -331,7 +331,7 @@ edp_redy==0.0.3
|
|||||||
einder==0.3.1
|
einder==0.3.1
|
||||||
|
|
||||||
# homeassistant.components.sensor.eliqonline
|
# homeassistant.components.sensor.eliqonline
|
||||||
eliqonline==1.0.14
|
eliqonline==1.2.2
|
||||||
|
|
||||||
# homeassistant.components.elkm1
|
# homeassistant.components.elkm1
|
||||||
elkm1-lib==0.7.13
|
elkm1-lib==0.7.13
|
||||||
|
Loading…
x
Reference in New Issue
Block a user