SMA: Optional import in schema & backoff fix (#18099)

This commit is contained in:
Johann Kellerman 2018-11-04 19:09:14 +02:00 committed by GitHub
parent e161dc3b77
commit 44556a86e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -35,7 +35,10 @@ GROUPS = ['user', 'installer']
def _check_sensor_schema(conf): def _check_sensor_schema(conf):
"""Check sensors and attributes are valid.""" """Check sensors and attributes are valid."""
import pysma try:
import pysma
except ImportError:
return conf
valid = list(conf[CONF_CUSTOM].keys()) valid = list(conf[CONF_CUSTOM].keys())
valid.extend([s.name for s in pysma.SENSORS]) valid.extend([s.name for s in pysma.SENSORS])
@ -73,6 +76,9 @@ async def async_setup_platform(
"""Set up SMA WebConnect sensor.""" """Set up SMA WebConnect sensor."""
import pysma import pysma
# Check config again during load - dependency available
config = _check_sensor_schema(config)
# Sensor_defs from the custom config # Sensor_defs from the custom config
for name, prop in config[CONF_CUSTOM].items(): for name, prop in config[CONF_CUSTOM].items():
n_s = pysma.Sensor(name, prop['key'], prop['unit'], prop['factor']) n_s = pysma.Sensor(name, prop['key'], prop['unit'], prop['factor'])
@ -107,18 +113,24 @@ async def async_setup_platform(
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, async_close_session) hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, async_close_session)
backoff = 0 backoff = 0
backoff_step = 0
async def async_sma(event): async def async_sma(event):
"""Update all the SMA sensors.""" """Update all the SMA sensors."""
nonlocal backoff nonlocal backoff, backoff_step
if backoff > 1: if backoff > 1:
backoff -= 1 backoff -= 1
return return
values = await sma.read(used_sensors) values = await sma.read(used_sensors)
if values is None: if not values:
backoff = 10 try:
backoff = [1, 1, 1, 6, 30][backoff_step]
backoff_step += 1
except IndexError:
backoff = 60
return return
backoff_step = 0
tasks = [] tasks = []
for sensor in hass_sensors: for sensor in hass_sensors: