Skip to content

Commit

Permalink
Bug fixes, hass.data
Browse files Browse the repository at this point in the history
  • Loading branch information
hakana committed Apr 5, 2019
1 parent e24a152 commit 89c20b5
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
components/shelly/pyShelly

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
56 changes: 31 additions & 25 deletions components/shelly/__init__.py
Expand Up @@ -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__)

Expand All @@ -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__
Expand Down Expand Up @@ -79,32 +80,42 @@ 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"""
_LOGGER.info("Starting shelly, %s", __version__)

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()
Expand All @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions components/shelly/cover.py
Expand Up @@ -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."""
Expand Down
8 changes: 2 additions & 6 deletions components/shelly/light.py
Expand Up @@ -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__)

Expand Down Expand Up @@ -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."""
Expand Down
7 changes: 2 additions & 5 deletions components/shelly/sensor.py
Expand Up @@ -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__)

Expand All @@ -34,16 +34,14 @@
['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:
add_devices([ShellyVersion(hass, discovery_info.get('version'),
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([
Expand All @@ -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):
Expand Down
9 changes: 3 additions & 6 deletions components/shelly/switch.py
Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion 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",
Expand Down
Binary file added screenshots/DeviceInfo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/Uptime.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/WiFiSignal.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 89c20b5

Please sign in to comment.