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))