Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _check_sensor_schema(conf):
"""Check sensors and attributes are valid."""
try:
valid = [s.name for s in pysma.Sensors()]
except (ImportError, AttributeError):
return conf
customs = list(conf[CONF_CUSTOM].keys())
for sensor in conf[CONF_SENSORS]:
if sensor in customs:
_LOGGER.warning(
"All custom sensors will be added automatically, no need to include them in sensors: %s",
sensor,
)
elif sensor not in valid:
raise vol.Invalid(f"{sensor} does not exist")
return conf
def _check_sensor_schema(conf):
"""Check sensors and attributes are valid."""
try:
import pysma
valid = [s.name for s in pysma.Sensors()]
except (ImportError, AttributeError):
return conf
for name in conf[CONF_CUSTOM]:
valid.append(name)
for sname, attrs in conf[CONF_SENSORS].items():
if sname not in valid:
raise vol.Invalid("{} does not exist".format(sname))
for attr in attrs:
if attr in valid:
continue
raise vol.Invalid("{} does not exist [{}]".format(attr, sname))
return conf
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up SMA WebConnect sensor."""
# Check config again during load - dependency available
config = _check_sensor_schema(config)
# Init all default sensors
sensor_def = pysma.Sensors()
# Sensor from the custom config
sensor_def.add(
[
pysma.Sensor(o[CONF_KEY], n, o[CONF_UNIT], o[CONF_FACTOR], o.get(CONF_PATH))
for n, o in config[CONF_CUSTOM].items()
]
)
# Use all sensors by default
config_sensors = config[CONF_SENSORS]
hass_sensors = []
used_sensors = []
if isinstance(config_sensors, dict): # will be remove from 0.99
if not config_sensors: # Use all sensors by default
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None):
"""Set up SMA WebConnect sensor."""
import pysma
# Check config again during load - dependency available
config = _check_sensor_schema(config)
# Init all default sensors
sensor_def = pysma.Sensors()
# Sensor from the custom config
sensor_def.add([pysma.Sensor(o[CONF_KEY], n, o[CONF_UNIT], o[CONF_FACTOR])
for n, o in config[CONF_CUSTOM].items()])
# Use all sensors by default
config_sensors = config[CONF_SENSORS]
if not config_sensors:
config_sensors = {s.name: [] for s in sensor_def}
# Prepare all HASS sensor entities
hass_sensors = []
used_sensors = []
for name, attr in config_sensors.items():
sub_sensors = [sensor_def[s] for s in attr]
hass_sensors.append(SMAsensor(sensor_def[name], sub_sensors))
sub_sensors = [sensor_def[s] for s in attr]
hass_sensors.append(SMAsensor(sensor_def[name], sub_sensors))
used_sensors.append(name)
used_sensors.extend(attr)
async_add_entities(hass_sensors)
used_sensors = [sensor_def[s] for s in set(used_sensors)]
# Init the SMA interface
session = async_get_clientsession(hass, verify_ssl=config[CONF_VERIFY_SSL])
grp = config[CONF_GROUP]
url = "http{}://{}".format(
"s" if config[CONF_SSL] else "", config[CONF_HOST])
sma = pysma.SMA(session, url, config[CONF_PASSWORD], group=grp)
# Ensure we logout on shutdown
async def async_close_session(event):
"""Close the session."""
await sma.close_session()
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, async_close_session)
backoff = 0
backoff_step = 0
async def async_sma(event):
"""Update all the SMA sensors."""
nonlocal backoff, backoff_step
if backoff > 1:
backoff -= 1
if not config_sensors: # Use all sensors by default
config_sensors = [s.name for s in sensor_def]
used_sensors = list(set(config_sensors + list(config[CONF_CUSTOM].keys())))
for sensor in used_sensors:
hass_sensors.append(SMAsensor(sensor_def[sensor], []))
used_sensors = [sensor_def[s] for s in set(used_sensors)]
async_add_entities(hass_sensors)
# Init the SMA interface
session = async_get_clientsession(hass, verify_ssl=config[CONF_VERIFY_SSL])
grp = config[CONF_GROUP]
url = "http{}://{}".format("s" if config[CONF_SSL] else "", config[CONF_HOST])
sma = pysma.SMA(session, url, config[CONF_PASSWORD], group=grp)
# Ensure we logout on shutdown
async def async_close_session(event):
"""Close the session."""
await sma.close_session()
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, async_close_session)
backoff = 0
backoff_step = 0
async def async_sma(event):
"""Update all the SMA sensors."""
nonlocal backoff, backoff_step
if backoff > 1:
backoff -= 1
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up SMA WebConnect sensor."""
# Check config again during load - dependency available
config = _check_sensor_schema(config)
# Init all default sensors
sensor_def = pysma.Sensors()
# Sensor from the custom config
sensor_def.add(
[
pysma.Sensor(o[CONF_KEY], n, o[CONF_UNIT], o[CONF_FACTOR], o.get(CONF_PATH))
for n, o in config[CONF_CUSTOM].items()
]
)
# Use all sensors by default
config_sensors = config[CONF_SENSORS]
hass_sensors = []
used_sensors = []
if isinstance(config_sensors, dict): # will be remove from 0.99
if not config_sensors: # Use all sensors by default
config_sensors = {s.name: [] for s in sensor_def}
# Prepare all HASS sensor entities
for name, attr in config_sensors.items():
sub_sensors = [sensor_def[s] for s in attr]
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None):
"""Set up SMA WebConnect sensor."""
import pysma
# Check config again during load - dependency available
config = _check_sensor_schema(config)
# Init all default sensors
sensor_def = pysma.Sensors()
# Sensor from the custom config
sensor_def.add([pysma.Sensor(o[CONF_KEY], n, o[CONF_UNIT], o[CONF_FACTOR])
for n, o in config[CONF_CUSTOM].items()])
# Use all sensors by default
config_sensors = config[CONF_SENSORS]
if not config_sensors:
config_sensors = {s.name: [] for s in sensor_def}
# Prepare all HASS sensor entities
hass_sensors = []
used_sensors = []
for name, attr in config_sensors.items():
sub_sensors = [sensor_def[s] for s in attr]
hass_sensors.append(SMAsensor(sensor_def[name], sub_sensors))
used_sensors.append(name)
used_sensors.extend(attr)