Update to sense component to fully be async (#21698)

* Update to sense component to fully be async

* Shortened lines

* Whitespace
This commit is contained in:
kbickar 2019-03-12 09:44:53 -04:00 committed by Pascal Vizeli
parent dd11f8d3fe
commit 62df6cbd09
4 changed files with 26 additions and 28 deletions

View file

@ -5,9 +5,9 @@ import voluptuous as vol
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.discovery import load_platform from homeassistant.helpers.discovery import async_load_platform
REQUIREMENTS = ['sense_energy==0.6.0'] REQUIREMENTS = ['sense_energy==0.7.0']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -27,22 +27,24 @@ CONFIG_SCHEMA = vol.Schema({
}, extra=vol.ALLOW_EXTRA) }, extra=vol.ALLOW_EXTRA)
def setup(hass, config): async def async_setup(hass, config):
"""Set up the Sense sensor.""" """Set up the Sense sensor."""
from sense_energy import Senseable, SenseAuthenticationException from sense_energy import ASyncSenseable, SenseAuthenticationException
username = config[DOMAIN][CONF_EMAIL] username = config[DOMAIN][CONF_EMAIL]
password = config[DOMAIN][CONF_PASSWORD] password = config[DOMAIN][CONF_PASSWORD]
timeout = config[DOMAIN][CONF_TIMEOUT] timeout = config[DOMAIN][CONF_TIMEOUT]
try: try:
hass.data[SENSE_DATA] = Senseable( hass.data[SENSE_DATA] = ASyncSenseable(
api_timeout=timeout, wss_timeout=timeout) api_timeout=timeout, wss_timeout=timeout)
hass.data[SENSE_DATA].authenticate(username, password)
hass.data[SENSE_DATA].rate_limit = ACTIVE_UPDATE_RATE hass.data[SENSE_DATA].rate_limit = ACTIVE_UPDATE_RATE
await hass.data[SENSE_DATA].authenticate(username, password)
except SenseAuthenticationException: except SenseAuthenticationException:
_LOGGER.error("Could not authenticate with sense server") _LOGGER.error("Could not authenticate with sense server")
return False return False
load_platform(hass, 'sensor', DOMAIN, {}, config) hass.async_create_task(
load_platform(hass, 'binary_sensor', DOMAIN, {}, config) async_load_platform(hass, 'sensor', DOMAIN, None, config))
hass.async_create_task(
async_load_platform(hass, 'binary_sensor', DOMAIN, None, config))
return True return True

View file

@ -49,17 +49,15 @@ MDI_ICONS = {
} }
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 Sense binary sensor.""" """Set up the Sense binary sensor."""
if discovery_info is None:
return
data = hass.data[SENSE_DATA] data = hass.data[SENSE_DATA]
sense_devices = data.get_discovered_device_data() sense_devices = await data.get_discovered_device_data()
devices = [SenseDevice(data, device) for device in sense_devices devices = [SenseDevice(data, device) for device in sense_devices
if device['tags']['DeviceListAllowed'] == 'true'] if device['tags']['DeviceListAllowed'] == 'true']
add_entities(devices) async_add_entities(devices)
def sense_to_mdi(sense_icon): def sense_to_mdi(sense_icon):
@ -103,11 +101,11 @@ class SenseDevice(BinarySensorDevice):
"""Return the device class of the binary sensor.""" """Return the device class of the binary sensor."""
return BIN_SENSOR_CLASS return BIN_SENSOR_CLASS
def update(self): async def async_update(self):
"""Retrieve latest state.""" """Retrieve latest state."""
from sense_energy.sense_api import SenseAPITimeoutException from sense_energy.sense_api import SenseAPITimeoutException
try: try:
self._data.get_realtime() await self._data.update_realtime()
except SenseAPITimeoutException: except SenseAPITimeoutException:
_LOGGER.error("Timeout retrieving data") _LOGGER.error("Timeout retrieving data")
return return

View file

@ -45,21 +45,19 @@ SENSOR_TYPES = {
SENSOR_VARIANTS = [PRODUCTION_NAME.lower(), CONSUMPTION_NAME.lower()] SENSOR_VARIANTS = [PRODUCTION_NAME.lower(), CONSUMPTION_NAME.lower()]
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 Sense sensor.""" """Set up the Sense sensor."""
if discovery_info is None:
return
data = hass.data[SENSE_DATA] data = hass.data[SENSE_DATA]
@Throttle(MIN_TIME_BETWEEN_DAILY_UPDATES) @Throttle(MIN_TIME_BETWEEN_DAILY_UPDATES)
def update_trends(): async def update_trends():
"""Update the daily power usage.""" """Update the daily power usage."""
data.update_trend_data() await data.update_trend_data()
def update_active(): async def update_active():
"""Update the active power usage.""" """Update the active power usage."""
data.get_realtime() await data.update_realtime()
devices = [] devices = []
for typ in SENSOR_TYPES.values(): for typ in SENSOR_TYPES.values():
@ -74,7 +72,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
devices.append(Sense( devices.append(Sense(
data, name, sensor_type, is_production, update_call)) data, name, sensor_type, is_production, update_call))
add_entities(devices) async_add_entities(devices)
class Sense(Entity): class Sense(Entity):
@ -115,11 +113,11 @@ class Sense(Entity):
"""Icon to use in the frontend, if any.""" """Icon to use in the frontend, if any."""
return ICON return ICON
def update(self): async def async_update(self):
"""Get the latest data, update state.""" """Get the latest data, update state."""
from sense_energy import SenseAPITimeoutException from sense_energy import SenseAPITimeoutException
try: try:
self.update_sensor() await self.update_sensor()
except SenseAPITimeoutException: except SenseAPITimeoutException:
_LOGGER.error("Timeout retrieving data") _LOGGER.error("Timeout retrieving data")
return return

View file

@ -1566,7 +1566,7 @@ sendgrid==5.6.0
sense-hat==2.2.0 sense-hat==2.2.0
# homeassistant.components.sense # homeassistant.components.sense
sense_energy==0.6.0 sense_energy==0.7.0
# homeassistant.components.media_player.aquostv # homeassistant.components.media_player.aquostv
sharp_aquos_rc==0.3.2 sharp_aquos_rc==0.3.2