Skip to content

Commit

Permalink
Updated ELIQ Online sensor to async API
Browse files Browse the repository at this point in the history
  • Loading branch information
molobrakos committed Dec 13, 2018
1 parent 2680bf8 commit e4e7bd2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions homeassistant/components/sensor/eliqonline.py
Expand Up @@ -13,8 +13,9 @@
from homeassistant.const import (CONF_ACCESS_TOKEN, CONF_NAME, STATE_UNKNOWN)
from homeassistant.helpers.entity import Entity
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__)

Expand All @@ -35,24 +36,27 @@
})


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."""
import eliqonline

access_token = config.get(CONF_ACCESS_TOKEN)
name = config.get(CONF_NAME, DEFAULT_NAME)
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:
_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:
_LOGGER.error("Could not access the ELIQ Online API: %s", error)
return False

add_entities([EliqSensor(api, channel_id, name)], True)
async_add_entities([EliqSensor(api, channel_id, name)], True)


class EliqSensor(Entity):
Expand Down Expand Up @@ -85,12 +89,14 @@ def state(self):
"""Return the state of the device."""
return self._state

def update(self):
async def async_update(self):
"""Get the latest data."""
try:
response = self._api.get_data_now(channelid=self._channel_id)
self._state = int(response.power)
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)
except KeyError:
_LOGGER.warning("Invalid response from ELIQ Online API")
except OSError as error:
_LOGGER.warning("Could not connect to the ELIQ Online API: %s",
error)
2 changes: 1 addition & 1 deletion requirements_all.txt
Expand Up @@ -330,7 +330,7 @@ edp_redy==0.0.2
einder==0.3.1

# homeassistant.components.sensor.eliqonline
eliqonline==1.0.14
eliqonline==1.2.2

# homeassistant.components.elkm1
elkm1-lib==0.7.12
Expand Down

0 comments on commit e4e7bd2

Please sign in to comment.