From 3ffa0176cc11b8860857b311f9b1b32d57978ce9 Mon Sep 17 00:00:00 2001 From: Johann Kellerman Date: Sun, 6 Jan 2019 19:20:19 +0200 Subject: [PATCH] SMA sensor - updated library (#19753) --- homeassistant/components/sensor/sma.py | 39 ++++++++++++++++++-------- requirements_all.txt | 2 +- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/sensor/sma.py b/homeassistant/components/sensor/sma.py index acf1ead186c..4bfa62bf6dd 100644 --- a/homeassistant/components/sensor/sma.py +++ b/homeassistant/components/sensor/sma.py @@ -19,7 +19,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity from homeassistant.helpers.event import async_track_time_interval -REQUIREMENTS = ['pysma==0.2.2'] +REQUIREMENTS = ['pysma==0.3.1'] _LOGGER = logging.getLogger(__name__) @@ -37,11 +37,13 @@ def _check_sensor_schema(conf): """Check sensors and attributes are valid.""" try: import pysma - valid = [s.name for s in pysma.SENSORS] + valid = [s.name for s in pysma.Sensors()] except (ImportError, AttributeError): return conf - valid.extend(conf[CONF_CUSTOM].keys()) + 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)) @@ -65,7 +67,8 @@ PLATFORM_SCHEMA = vol.All(PLATFORM_SCHEMA.extend({ vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean, vol.Required(CONF_PASSWORD): cv.string, vol.Optional(CONF_GROUP, default=GROUPS[0]): vol.In(GROUPS), - vol.Required(CONF_SENSORS): vol.Schema({cv.slug: cv.ensure_list}), + vol.Optional(CONF_SENSORS, default={}): + vol.Schema({cv.slug: cv.ensure_list}), vol.Optional(CONF_CUSTOM, default={}): vol.Schema({cv.slug: CUSTOM_SCHEMA}), }, extra=vol.PREVENT_EXTRA), _check_sensor_schema) @@ -79,22 +82,29 @@ async def async_setup_platform( # Check config again during load - dependency available config = _check_sensor_schema(config) - # Sensor_defs from the custom config - for name, prop in config[CONF_CUSTOM].items(): - n_s = pysma.Sensor(name, prop['key'], prop['unit'], prop['factor']) - pysma.add_sensor(n_s) + # 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[CONF_SENSORS].items(): - sub_sensors = [pysma.get_sensor(s) for s in attr] - hass_sensors.append(SMAsensor(pysma.get_sensor(name), sub_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) async_add_entities(hass_sensors) - used_sensors = [pysma.get_sensor(s) for s in set(used_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]) @@ -195,3 +205,8 @@ class SMAsensor(Entity): self._state = self._sensor.value return self.async_update_ha_state() if update else None + + @property + def unique_id(self): + """Return a unique identifier for this sensor.""" + return "sma-{}-{}".format(self._sensor.key, self._sensor.name) diff --git a/requirements_all.txt b/requirements_all.txt index 3f98fb7e2e1..7f162434bd7 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1198,7 +1198,7 @@ pysesame==0.1.0 pysher==1.0.1 # homeassistant.components.sensor.sma -pysma==0.2.2 +pysma==0.3.1 # homeassistant.components.device_tracker.snmp # homeassistant.components.sensor.snmp