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:
parent
dd11f8d3fe
commit
62df6cbd09
4 changed files with 26 additions and 28 deletions
|
@ -5,9 +5,9 @@ import voluptuous as vol
|
|||
|
||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT
|
||||
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__)
|
||||
|
||||
|
@ -27,22 +27,24 @@ CONFIG_SCHEMA = vol.Schema({
|
|||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
async def async_setup(hass, config):
|
||||
"""Set up the Sense sensor."""
|
||||
from sense_energy import Senseable, SenseAuthenticationException
|
||||
from sense_energy import ASyncSenseable, SenseAuthenticationException
|
||||
|
||||
username = config[DOMAIN][CONF_EMAIL]
|
||||
password = config[DOMAIN][CONF_PASSWORD]
|
||||
|
||||
timeout = config[DOMAIN][CONF_TIMEOUT]
|
||||
try:
|
||||
hass.data[SENSE_DATA] = Senseable(
|
||||
hass.data[SENSE_DATA] = ASyncSenseable(
|
||||
api_timeout=timeout, wss_timeout=timeout)
|
||||
hass.data[SENSE_DATA].authenticate(username, password)
|
||||
hass.data[SENSE_DATA].rate_limit = ACTIVE_UPDATE_RATE
|
||||
await hass.data[SENSE_DATA].authenticate(username, password)
|
||||
except SenseAuthenticationException:
|
||||
_LOGGER.error("Could not authenticate with sense server")
|
||||
return False
|
||||
load_platform(hass, 'sensor', DOMAIN, {}, config)
|
||||
load_platform(hass, 'binary_sensor', DOMAIN, {}, config)
|
||||
hass.async_create_task(
|
||||
async_load_platform(hass, 'sensor', DOMAIN, None, config))
|
||||
hass.async_create_task(
|
||||
async_load_platform(hass, 'binary_sensor', DOMAIN, None, config))
|
||||
return True
|
||||
|
|
|
@ -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."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
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
|
||||
if device['tags']['DeviceListAllowed'] == 'true']
|
||||
add_entities(devices)
|
||||
async_add_entities(devices)
|
||||
|
||||
|
||||
def sense_to_mdi(sense_icon):
|
||||
|
@ -103,11 +101,11 @@ class SenseDevice(BinarySensorDevice):
|
|||
"""Return the device class of the binary sensor."""
|
||||
return BIN_SENSOR_CLASS
|
||||
|
||||
def update(self):
|
||||
async def async_update(self):
|
||||
"""Retrieve latest state."""
|
||||
from sense_energy.sense_api import SenseAPITimeoutException
|
||||
try:
|
||||
self._data.get_realtime()
|
||||
await self._data.update_realtime()
|
||||
except SenseAPITimeoutException:
|
||||
_LOGGER.error("Timeout retrieving data")
|
||||
return
|
||||
|
|
|
@ -45,21 +45,19 @@ SENSOR_TYPES = {
|
|||
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."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
data = hass.data[SENSE_DATA]
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_DAILY_UPDATES)
|
||||
def update_trends():
|
||||
async def update_trends():
|
||||
"""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."""
|
||||
data.get_realtime()
|
||||
await data.update_realtime()
|
||||
|
||||
devices = []
|
||||
for typ in SENSOR_TYPES.values():
|
||||
|
@ -74,7 +72,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
devices.append(Sense(
|
||||
data, name, sensor_type, is_production, update_call))
|
||||
|
||||
add_entities(devices)
|
||||
async_add_entities(devices)
|
||||
|
||||
|
||||
class Sense(Entity):
|
||||
|
@ -115,11 +113,11 @@ class Sense(Entity):
|
|||
"""Icon to use in the frontend, if any."""
|
||||
return ICON
|
||||
|
||||
def update(self):
|
||||
async def async_update(self):
|
||||
"""Get the latest data, update state."""
|
||||
from sense_energy import SenseAPITimeoutException
|
||||
try:
|
||||
self.update_sensor()
|
||||
await self.update_sensor()
|
||||
except SenseAPITimeoutException:
|
||||
_LOGGER.error("Timeout retrieving data")
|
||||
return
|
||||
|
|
|
@ -1566,7 +1566,7 @@ sendgrid==5.6.0
|
|||
sense-hat==2.2.0
|
||||
|
||||
# homeassistant.components.sense
|
||||
sense_energy==0.6.0
|
||||
sense_energy==0.7.0
|
||||
|
||||
# homeassistant.components.media_player.aquostv
|
||||
sharp_aquos_rc==0.3.2
|
||||
|
|
Loading…
Add table
Reference in a new issue