diff --git a/.gitignore b/.gitignore index 894a44c..6ee96b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +components/shelly/pyShelly + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/components/shelly/__init__.py b/components/shelly/__init__.py index d41813a..d91d705 100755 --- a/components/shelly/__init__.py +++ b/components/shelly/__init__.py @@ -11,7 +11,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity -REQUIREMENTS = ['pyShelly==0.0.23'] +REQUIREMENTS = ['pyShelly==0.0.22'] _LOGGER = logging.getLogger(__name__) @@ -32,8 +32,9 @@ DEFAULT_SCAN_INTERVAL = timedelta(seconds=60) DEFAULT_SHOW_ID_IN_NAME = True -SHELLY_DATA = 'shellyData' -SHELLY_CONFIG = 'shellyCfg' +SHELLY_DEVICES = 'shelly_devices' +SHELLY_CONFIG = 'shelly_cfg' +SHELLY_DEVICE_ID = 'device_id' __version__ = "0.0.6" VERSION = __version__ @@ -79,7 +80,10 @@ def get_device_config(conf, device_id): if item[CONF_ID] == device_id: return item return {} - + +def get_device_from_hass(hass, discovery_info): + device_key = discovery_info[SHELLY_DEVICE_ID] + return hass.data[SHELLY_DEVICES][device_key] def setup(hass, config): """Setup Shelly component""" @@ -87,24 +91,31 @@ def setup(hass, config): conf = config.get(DOMAIN, {}) update_interval = conf.get(CONF_SCAN_INTERVAL) - hass.data[SHELLY_CONFIG] = conf - + hass.data[SHELLY_CONFIG] = conf discover = conf.get(CONF_DISCOVERY) - from pyShelly import pyShelly + try: + from .pyShelly import pyShelly #Used for development only + except: + from pyShelly import pyShelly - devices = [] + devices = {} block_sensors = {} + hass.data[SHELLY_DEVICES] = devices def _device_added(dev, code): - devices.append(dev) + deviceKey = dev.id + + if deviceKey in devices: + return + devices[deviceKey]=dev device_config = get_device_config(conf, dev.id) if not discover and device_config == {}: return - data_key = SHELLY_DATA + dev.id + dev.devType - hass.data[data_key] = dev - attr = {'dataKey': data_key} + #data_key = SHELLY_DATA + dev.id + dev.devType + #hass.data[data_key] = dev + attr = {SHELLY_DEVICE_ID : deviceKey} if conf.get(CONF_ADDITIONAL_INFO): dev.block.update_status_information() @@ -126,37 +137,32 @@ def _device_added(dev, code): if conf.get(CONF_WIFI_SENSOR) \ and block_sensors.get(dev.block.id + "_rssi") is None: rssi_attr = {'rssi': dev.infoValues.get('rssi'), - 'dataKey': data_key} + SHELLY_DEVICE_ID : deviceKey} discovery.load_platform(hass, 'sensor', DOMAIN, rssi_attr, config) - block_sensors[dev.block.id] = True + block_sensors[dev.block.id + "_rssi"] = True if conf.get(CONF_UPTIME_SENSOR) \ and block_sensors.get(dev.block.id + "_uptime") is None: upt_attr = {'uptime': dev.infoValues.get('uptime'), - 'dataKey': data_key} + SHELLY_DEVICE_ID : deviceKey} discovery.load_platform(hass, 'sensor', DOMAIN, upt_attr, config) - block_sensors[dev.block.id] = True + block_sensors[dev.block.id + "_uptime"] = True def _device_removed(dev, code): dev.shelly_device.async_remove() - devices.remove(dev) + try: + del devices[dev.id] + except: + pass block_sensors[dev.dev.block.id + "_rssi"] = None block_sensors[dev.dev.block.id + "_uptime"] = None - # def _deviceAddFilter(id, devType, addr): - # if discover: - # return True - # devConf = getDeviceConfig(conf, id) - # if devConf != {}: - # return True - pys = pyShelly() _LOGGER.info("pyShelly, %s", pys.version()) pys.cb_deviceAdded = _device_added pys.cb_deviceRemoved = _device_removed - # pys.cb_deviceAddFilter = _deviceAddFilter pys.username = conf.get(CONF_USERNAME) pys.password = conf.get(CONF_PASSWORD) pys.igmpFixEnabled = conf.get(CONF_IGMPFIX) diff --git a/components/shelly/cover.py b/components/shelly/cover.py index ab34df4..adf3882 100755 --- a/components/shelly/cover.py +++ b/components/shelly/cover.py @@ -8,16 +8,13 @@ SUPPORT_OPEN, SUPPORT_STOP, SUPPORT_SET_POSITION) -from . import ShellyDevice +from . import ShellyDevice, get_device_from_hass def setup_platform(hass, config, add_devices, discovery_info=None): """Set up the Shelly cover platform.""" - data_key = discovery_info['dataKey'] - dev = hass.data[data_key] + dev = get_device_from_hass(hass, discovery_info) add_devices([ShellyCover(dev, hass)]) - hass.data[data_key] = None - class ShellyCover(ShellyDevice, CoverDevice): """Shelly cover device.""" diff --git a/components/shelly/light.py b/components/shelly/light.py index 332622c..2ddd948 100755 --- a/components/shelly/light.py +++ b/components/shelly/light.py @@ -15,7 +15,7 @@ color_temperature_kelvin_to_mired as kelvin_to_mired, color_temperature_mired_to_kelvin as mired_to_kelvin) -from . import ShellyDevice +from . import ShellyDevice, get_device_from_hass _LOGGER = logging.getLogger(__name__) @@ -52,17 +52,13 @@ SUPPORT_SHELLYRGB_COLOR = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR) SUPPORT_SHELLYRGB_WHITE = (SUPPORT_BRIGHTNESS) - def setup_platform(hass, config, add_devices, discovery_info=None): """Setup Shelly Light platform.""" - data_key = discovery_info['dataKey'] - dev = hass.data[data_key] + dev = get_device_from_hass(hass, discovery_info) if dev.devType == "RELAY": add_devices([ShellyLight(dev, hass)]) else: add_devices([ShellyRGB(dev, hass)]) - hass.data[data_key] = None - class ShellyLight(ShellyDevice, Light): """Representation of an Shelly Switch.""" diff --git a/components/shelly/sensor.py b/components/shelly/sensor.py index 6e90daf..d8b8030 100755 --- a/components/shelly/sensor.py +++ b/components/shelly/sensor.py @@ -11,7 +11,7 @@ DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS, POWER_WATT) from homeassistant.helpers.entity import Entity -from . import CONF_OBJECT_ID_PREFIX, SHELLY_CONFIG, ShellyDevice +from . import CONF_OBJECT_ID_PREFIX, SHELLY_CONFIG, ShellyDevice, get_device_from_hass _LOGGER = logging.getLogger(__name__) @@ -34,7 +34,6 @@ ['Uptime', 's', 'mdi:timer', None] } - def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the Shelly Sensor platform.""" if 'version' in discovery_info: @@ -42,8 +41,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): discovery_info.get('pyShellyVersion'))]) return - data_key = discovery_info['dataKey'] - dev = hass.data[data_key] + dev = get_device_from_hass(hass, discovery_info) if 'rssi' in discovery_info: add_devices([ @@ -66,7 +64,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None): ShellySensor(dev, hass, SENSOR_TYPE_TEMPERATURE, 'temperature'), ShellySensor(dev, hass, SENSOR_TYPE_HUMIDITY, 'humidity') ]) - hass.data[data_key] = None class ShellySensor(ShellyDevice, Entity): diff --git a/components/shelly/switch.py b/components/shelly/switch.py index 722b0a0..dfb6d00 100755 --- a/components/shelly/switch.py +++ b/components/shelly/switch.py @@ -9,19 +9,16 @@ from homeassistant.components.switch import SwitchDevice -# from .sensor import ShellySensor, SENSOR_TYPE_POWER -from . import ShellyDevice +# from .sensor import ShellySensor +from . import ShellyDevice, get_device_from_hass _LOGGER = logging.getLogger(__name__) def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the Shelly Switch platform.""" - data_key = discovery_info['dataKey'] - dev = hass.data[data_key] + dev = get_device_from_hass(hass, discovery_info) add_devices([ShellySwitch(dev, hass)]) - hass.data[data_key] = None - class ShellySwitch(ShellyDevice, SwitchDevice): """Representation of an Shelly Switch.""" diff --git a/custom_updater.json b/custom_updater.json index 5bd4cea..280f87c 100644 --- a/custom_updater.json +++ b/custom_updater.json @@ -1,6 +1,6 @@ { "shelly": { - "version": "0.0.5", + "version": "0.0.6", "local_location": "/custom_components/shelly/__init__.py", "remote_location": "https://raw.githubusercontent.com/StyraHem/hass/master/components/shelly/__init__.py", "visit_repo": "https://github.com/StyraHem/hass/tree/master/components/shelly", diff --git a/screenshots/DeviceInfo.png b/screenshots/DeviceInfo.png new file mode 100644 index 0000000..7cdce18 Binary files /dev/null and b/screenshots/DeviceInfo.png differ diff --git a/screenshots/Uptime.png b/screenshots/Uptime.png new file mode 100644 index 0000000..f40a94a Binary files /dev/null and b/screenshots/Uptime.png differ diff --git a/screenshots/WiFiSignal.png b/screenshots/WiFiSignal.png new file mode 100644 index 0000000..6d4b49f Binary files /dev/null and b/screenshots/WiFiSignal.png differ