Skip to content

Commit

Permalink
0.2.1-b2
Browse files Browse the repository at this point in the history
  • Loading branch information
hakana committed Nov 20, 2020
1 parent 4fd9830 commit 8b37efa
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 31 deletions.
5 changes: 4 additions & 1 deletion custom_components/shelly/.translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"mdns": "Enable mDns for discovery, try this if your Shelly devices not discovered",
"version": "Create a entry showing current version of ShellyForHass",
"upgrade_switch": "Create upgrade switch when Shelly has new firmware",
"igmp_fix": "Repeatly send IGMP ADD_MEMBER, try this if instant update stop some minutes",
"upgrade_beta_switch": "Enable upgrade to Beta version switch",
"igmp_fix": "Repeatly send IGMP ADD_MEMBER, try this if instant update stop after some minutes",
"host_ip": "Ip address for Home Assistant",
"mqtt_port": "Tcpip port of integrated MQTT server"
},
Expand Down Expand Up @@ -50,6 +51,7 @@
"shelly_id": "Shelly ID",
"ssid": "SSID",
"rssi": "RSSI",
"rssi_level": "RSSI Level",
"uptime": "Uptime",
"has_firmware_update": "Has firmware update",
"latest_fw_version": "Latest firmware version",
Expand Down Expand Up @@ -84,6 +86,7 @@
"sensors": {
"data": {
"rssi": "RSSI",
"rssi_level": "RSSI Level",
"uptime": "Uptime",
"current_consumption": "Current consumption",
"total_consumption": "Total consumption",
Expand Down
17 changes: 11 additions & 6 deletions custom_components/shelly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
from homeassistant.helpers.restore_state import RestoreStateData
from homeassistant.helpers.storage import Store
from homeassistant.helpers.json import JSONEncoder
from homeassistant.components.zeroconf import (
async_get_instance as zeroconf_async_get_instance
)
try:
from homeassistant.components.zeroconf import (
async_get_instance as zeroconf_async_get_instance
)
except:
zeroconf_async_get_instance = None
try: #Backward compatible with HA
from homeassistant.helpers.entity_registry import ATTR_RESTORED
except:
Expand All @@ -42,7 +45,7 @@

_LOGGER = logging.getLogger(__name__)

__version__ = "0.2.1-b1"
__version__ = "0.2.1-b2"
VERSION = __version__

async def async_setup(hass, config):
Expand Down Expand Up @@ -129,9 +132,10 @@ def __init__(self, hass, config_entry, conf):
self.conf_attributes.add(ATTRIBUTE_SWITCH + "_1")
self.conf_attributes.add(ATTRIBUTE_SWITCH + "_2")
sensors = self.conf.get(CONF_SENSORS, {})
if SENSOR_DEFAULT in sensors:
self.conf[CONF_SENSORS] = DEFAULT_SENSORS
if SENSOR_ALL in sensors:
self.conf[CONF_SENSORS] = [*ALL_SENSORS.keys()]

self._debug_msg = conf.get(CONF_DEBUG_ENABLE_INFO)

hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.stop)
Expand Down Expand Up @@ -173,7 +177,8 @@ async def start_up(self):
pys.password = conf.get(CONF_PASSWORD)
pys.cloud_auth_key = conf.get(CONF_CLOUD_AUTH_KEY)
pys.cloud_server = conf.get(CONF_CLOUD_SERVER)
pys.zeroconf = await zeroconf_async_get_instance(self.hass)
if zeroconf_async_get_instance:
pys.zeroconf = await zeroconf_async_get_instance(self.hass)
tmpl_name = conf.get(CONF_TMPL_NAME)
if tmpl_name:
pys.tmpl_name = tmpl_name
Expand Down
9 changes: 8 additions & 1 deletion custom_components/shelly/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self, block, instance, prefix=""):
self.async_on_remove(self._remove_handler)
self._master_unit = False
self._settings = instance.get_settings(block.id)
self.last_ha_update = None

def _remove_handler(self):
self._is_removed = True
Expand All @@ -56,13 +57,19 @@ def name(self):
name += " [" + self._block.id + "]"
return name

def _update_ha_state(self):
#snapshot = { "attr" : self.device_state_attributes, "state" : self.state, "available": self.available }
#if self.last_ha_update != snapshot:
self.schedule_update_ha_state()
#self.last_ha_update = snapshot

def _updated(self, _block):
"""Receive events when the switch state changed (by mobile,
switch etc)"""
disabled = self.registry_entry and self.registry_entry.disabled_by
if self.entity_id is not None and not self._is_removed \
and not disabled:
self.schedule_update_ha_state(True)
self._update_ha_state()

@property
def device_state_attributes(self):
Expand Down
8 changes: 6 additions & 2 deletions custom_components/shelly/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# pylint: disable=dangerous-default-value
import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant import config_entries
from homeassistant.core import callback

Expand Down Expand Up @@ -40,8 +41,11 @@ async def async_step_user(self, user_input={}):
)

async def async_step_input(self, user_input={}):
title = "Shelly"
if user_input and 'id_prefix' in user_input:
title = user_input["id_prefix"]
return self.async_create_entry(
title=user_input["id_prefix"],
title=title,
data=user_input
)

Expand Down Expand Up @@ -121,7 +125,7 @@ async def async_step_config_1(self, user_input=None):
self.v(CONF_UPGRADE_BETA_SWITCH): bool,
self.v(CONF_IGMPFIX): bool,
self.v(CONF_HOST_IP) : str,
self.v(CONF_MQTT_PORT) : int
self.v(CONF_MQTT_PORT) : cv.positive_int
})
return self.async_show_form(step_id="config_1", data_schema=schema)

Expand Down
3 changes: 2 additions & 1 deletion custom_components/shelly/configuration_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
vol.Optional('voltage'): SETTING_SCHEMA,
vol.Optional('power_factor'): SETTING_SCHEMA,
vol.Optional('uptime'): SETTING_SCHEMA,
vol.Optional('rssi'): SETTING_SCHEMA
vol.Optional('rssi'): SETTING_SCHEMA,
vol.Optional('rssi_level'): SETTING_SCHEMA
})

DEVICE_SCHEMA = vol.Schema({
Expand Down
21 changes: 16 additions & 5 deletions custom_components/shelly/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
ATTRIBUTE_SHELLY_ID = 'shelly_id'
ATTRIBUTE_SSID = 'ssid'
ATTRIBUTE_RSSI = 'rssi'
ATTRIBUTE_RSSI_LEVEL = 'rssi_level'
ATTRIBUTE_UPTIME = 'uptime'
ATTRIBUTE_HAS_FIRMWARE_UPDATE = 'has_firmware_update'
ATTRIBUTE_LATEST_FW = 'latest_fw_version'
Expand Down Expand Up @@ -129,6 +130,7 @@
ATTRIBUTE_SHELLY_ID,
ATTRIBUTE_SSID,
ATTRIBUTE_RSSI,
ATTRIBUTE_RSSI_LEVEL,
ATTRIBUTE_UPTIME,
ATTRIBUTE_HAS_FIRMWARE_UPDATE,
ATTRIBUTE_LATEST_FW,
Expand Down Expand Up @@ -172,12 +174,13 @@
ATTRIBUTE_SHELLY_TYPE,
ATTRIBUTE_SHELLY_ID,
ATTRIBUTE_HAS_FIRMWARE_UPDATE,
#ATTRIBUTE_RSSI_LEVEL,
ATTRIBUTE_CLOUD_STATUS,
ATTRIBUTE_SWITCH,
ATTRIBUTE_OVER_POWER,
ATTRIBUTE_OVER_TEMP,
ATTRIBUTE_TOTAL_CONSUMPTION,
ATTRIBUTE_VOLTAGE,
#ATTRIBUTE_VOLTAGE,
ATTRIBUTE_BATTERY,
ATTRIBUTE_CLICK_CNT,
ATTRIBUTE_CLICK_TYPE,
Expand All @@ -191,7 +194,9 @@
}

SENSOR_ALL = 'all'
SENSOR_DEFAULT = 'default'
SENSOR_RSSI = 'rssi'
SENSOR_RSSI_LEVEL = 'rssi_level'
SENSOR_POWER = 'power' #depreated, same as consumption
SENSOR_CONSUMPTION = 'consumption'
SENSOR_CURRENT_CONSUMPTION = 'current_consumption'
Expand All @@ -218,6 +223,7 @@

ALL_SENSORS = {
SENSOR_RSSI: {'attr':'rssi'},
SENSOR_RSSI_LEVEL: {'attr':'rssi_level'},
SENSOR_UPTIME: {'attr':'uptime'},
SENSOR_OVER_POWER: {'attr':'over_power'},
SENSOR_CURRENT_CONSUMPTION: {},
Expand All @@ -243,19 +249,22 @@

EXTRA_SENSORS = {
SENSOR_ALL: {},
SENSOR_DEFAULT: {},
SENSOR_POWER: {},
SENSOR_CONSUMPTION: {}
}

DEFAULT_SENSORS = [
SENSOR_CURRENT_CONSUMPTION,
SENSOR_TOTAL_CONSUMPTION
SENSOR_TOTAL_CONSUMPTION,
SENSOR_SWITCH
]

SENSOR_TYPE_TEMPERATURE = 'temperature'
SENSOR_TYPE_HUMIDITY = 'humidity'
SENSOR_TYPE_POWER = 'current_consumption'
SENSOR_TYPE_RSSI = 'rssi'
SENSOR_TYPE_RSSI_LEVEL = 'rssi_level'
SENSOR_TYPE_UPTIME = 'uptime'
SENSOR_TYPE_BATTERY = 'battery'
SENSOR_TYPE_OVER_POWER = 'over_power'
Expand Down Expand Up @@ -290,6 +299,8 @@
['Consumption', POWER_WATT, 'mdi:flash-outline', None, None],
SENSOR_TYPE_RSSI:
['RSSI', 'dB', 'mdi:wifi', None, None],
SENSOR_TYPE_RSSI_LEVEL:
['RSSI Level', None, 'mdi:wifi', None, None],
SENSOR_TYPE_UPTIME:
['Uptime', 's', 'mdi:timer-outline', None, None],
SENSOR_TYPE_BATTERY:
Expand Down Expand Up @@ -318,11 +329,11 @@
['Total returned', ENERGY_WATT_HOUR,
'mdi:flash-circle', DEVICE_CLASS_POWER, None],
SENSOR_TYPE_VOLTAGE:
['Voltage', 'V', 'mdi:flash', None, None],
['Voltage', 'V', 'mdi:alpha-v-circle-outline', None, None],
SENSOR_TYPE_POWER_FACTOR:
['Power factor', None, 'mdi:flash', None, None],
SENSOR_TYPE_CURRENT:
['Current', 'A', 'mdi:flash', None, None],
['Current', 'A', 'mdi:alpha-i-circle-outline', None, None],
SENSOR_TYPE_CLICK_TYPE:
['Click type', '', 'mdi:hockey-puck', None, None],
SENSOR_TYPE_TILT:
Expand All @@ -333,4 +344,4 @@
['Concentration', 'PPM', 'mdi:gauge', None, None],
SENSOR_TYPE_TOTAL_WORK_TIME:
['Total work time', 's', 'mdi:briefcase-clock', None, None],
}
}
3 changes: 2 additions & 1 deletion custom_components/shelly/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def set_cover_position(self, **kwargs):
pos = kwargs[ATTR_POSITION]
self._dev.set_position(pos)
self._position = pos
self.schedule_update_ha_state()

self._update_ha_state()

def stop_cover(self, **_kwargs):
"""Stop the cover."""
Expand Down
11 changes: 10 additions & 1 deletion custom_components/shelly/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,21 @@ def __init__(self, dev, instance):

self._settings = instance.get_settings(dev.id, dev.block.id)

self.last_ha_update = None

def _update_ha_state(self):
#snapshot = { "attr" : self.device_state_attributes, "state" : self.state, "available": self.available }
#if self.last_ha_update != snapshot:
self.schedule_update_ha_state()
#self.last_ha_update = snapshot

def _updated(self, _block):
"""Receive events when the switch state changed (by mobile,
switch etc)"""
disabled = self.registry_entry and self.registry_entry.disabled_by
if self.entity_id is not None and not self._is_removed and not disabled:
self.schedule_update_ha_state(True)

self._update_ha_state()

if self._dev.info_values is not None:
device_sensors = self.instance.device_sensors
Expand Down
18 changes: 12 additions & 6 deletions custom_components/shelly/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ def is_on(self):
def turn_on(self, **kwargs):
self._dev.turn_on()
self._state = True
self.schedule_update_ha_state()

self._update_ha_state()


def turn_off(self, **kwargs):
self._dev.turn_off()
self._state = False
self.schedule_update_ha_state()

self._update_ha_state()

def update(self):
"""Fetch new state data for this light."""
Expand Down Expand Up @@ -134,12 +136,14 @@ def turn_on(self, **kwargs):
else:
self._dev.turn_on(brightness)
self._state = True
self.schedule_update_ha_state()

self._update_ha_state()

def turn_off(self, **_kwargs):
self._dev.turn_off()
self._state = False
self.schedule_update_ha_state()

self._update_ha_state()

@property
def brightness(self):
Expand Down Expand Up @@ -272,13 +276,15 @@ def turn_on(self, **kwargs):
mode=mode, effect=effect_nr, white_value=white_value)

self._state = True
self.schedule_update_ha_state()

self._update_ha_state()

def turn_off(self, **_kwargs):
"""Turn off light"""
self._dev.turn_off()
self._state = False
self.schedule_update_ha_state()

self._update_ha_state()

def update(self):
"""Fetch new state data for this light."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/shelly/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"documentation": "https://github.com/StyraHem/ShellyForHASS/blob/master/README.md",
"dependencies": ["zeroconf"],
"codeowners": ["@hakana","@StyraHem"],
"requirements": ["pyShelly==0.2.10"]
"requirements": ["pyShelly==0.2.11"]
}
12 changes: 8 additions & 4 deletions custom_components/shelly/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ def turn_on(self, **_kwargs):
"""Turn on device"""
self._dev.turn_on()
self._state = True
self.schedule_update_ha_state()

self._update_ha_state()

def turn_off(self, **_kwargs):
"""Turn off device"""
self._dev.turn_off()
self._state = False
self.schedule_update_ha_state()

self._update_ha_state()

def update(self):
"""Fetch new state data for this switch."""
Expand Down Expand Up @@ -131,12 +133,14 @@ def is_on(self):
async def async_turn_on(self, **_kwargs):
"""Trig the firmware update"""
self._updating = True
self.schedule_update_ha_state()

self._update_ha_state()
await self.hass.async_add_executor_job(self._block.update_firmware, self._beta)

async def async_turn_off(self, **_kwargs):
"""Do nothing"""
self.schedule_update_ha_state()

self._update_ha_state()

def remove(self):
print("REMOVE*******************")
Expand Down
Loading

0 comments on commit 8b37efa

Please sign in to comment.