Skip to content

Commit

Permalink
0.2.2-b2, MQTT + external switch
Browse files Browse the repository at this point in the history
  • Loading branch information
hakana committed Feb 17, 2021
1 parent b8bf4db commit b965183
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 21 deletions.
12 changes: 11 additions & 1 deletion custom_components/shelly/.translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"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"
"mqtt_port": "Tcpip port to listen on for integrated MQTT server"
},
"title": "Shelly - Settings [1/3]",
"description": "Configuration (1/2)"
Expand All @@ -44,6 +44,16 @@
"title": "Shelly - Settings [1/3]",
"description": "Configuration (2/2)"
},
"config_mqtt": {
"data": {
"mqtt_server_host": "Host or ip address",
"mqtt_server_port": "Port",
"mqtt_server_username": "User name",
"mqtt_server_password": "Password"
},
"title": "Shelly - Settings [1/3]",
"description": "MQTT server connection"
},
"attributes": {
"data": {
"ip_address": "IP address",
Expand Down
9 changes: 8 additions & 1 deletion custom_components/shelly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

_LOGGER = logging.getLogger(__name__)

__version__ = "0.2.2-b1"
__version__ = "0.2.2-b2"
VERSION = __version__

async def async_setup(hass, config):
Expand Down Expand Up @@ -189,13 +189,20 @@ async def start_up(self):
pys.only_device_id = pys.only_device_id.upper()
pys.igmp_fix_enabled = conf.get(CONF_IGMPFIX)
pys.mdns_enabled = conf.get(CONF_MDNS)
pys.shelly_instance = self #Used for debuging only
host_ip = conf.get(CONF_HOST_IP)
if host_ip:
if host_ip == 'ha':
pys.host_ip = get_local_ip()
else:
pys.host_ip = host_ip
pys.mqtt_port = conf.get(CONF_MQTT_PORT, 0)

pys.mqtt_server_host = conf.get(CONF_MQTT_SERVER_HOST, '')
pys.mqtt_server_port = conf.get(CONF_MQTT_SERVER_PORT, 1883)
pys.mqtt_server_username = conf.get(CONF_MQTT_SERVER_USERNAME, '')
pys.mqtt_server_password = conf.get(CONF_MQTT_SERVER_PASSWORD, '')

pys.start()
pys.discover()

Expand Down
2 changes: 1 addition & 1 deletion custom_components/shelly/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def is_on(self):
@property
def icon(self):
"""Return the button icon."""
return "mdi:hockey-puck"
return "mdi:light-switch"

def _millis(self):
return int(round(time.time() * 1000))
Expand Down
18 changes: 17 additions & 1 deletion custom_components/shelly/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
CONF_CLOUD_AUTH_KEY, CONF_CLOUD_SERVER,
CONF_TMPL_NAME, CONF_ADDITIONAL_INFO,
CONF_OBJECT_ID_PREFIX,
CONF_UNAVALABLE_AFTER_SEC
CONF_UNAVALABLE_AFTER_SEC,
CONF_MQTT_SERVER_HOST, CONF_MQTT_SERVER_PORT,
CONF_MQTT_SERVER_USERNAME, CONF_MQTT_SERVER_PASSWORD
)
from .configuration_schema import STEP_SCHEMA

Expand Down Expand Up @@ -149,6 +151,20 @@ async def async_step_config_2(self, user_input=None):

self._options.update(user_input)

return await self.async_step_config_mqtt()

async def async_step_config_mqtt(self, user_input=None):
if not user_input:
schema = vol.Schema({
self.v(CONF_MQTT_SERVER_HOST): str,
self.v(CONF_MQTT_SERVER_PORT): int,
self.v(CONF_MQTT_SERVER_USERNAME): str,
self.v(CONF_MQTT_SERVER_PASSWORD): str,
})
return self.async_show_form(step_id="config_mqtt", data_schema=schema)

self._options.update(user_input)

return await self.async_step_attributes()


Expand Down
6 changes: 5 additions & 1 deletion custom_components/shelly/configuration_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@
vol.Optional(CONF_MDNS, default=DEFAULT_MDNS): cv.boolean,
vol.Optional(CONF_HOST_IP, default='') : cv.string,
vol.Optional(CONF_SETTINGS, default={}): SETTINGS_SCHEMA,
vol.Optional(CONF_MQTT_PORT, default=0): cv.positive_int
vol.Optional(CONF_MQTT_PORT): cv.positive_int,
vol.Optional(CONF_MQTT_SERVER_HOST, default=0): cv.string,
vol.Optional(CONF_MQTT_SERVER_PORT, default=1883): cv.positive_int,
vol.Optional(CONF_MQTT_SERVER_USERNAME, default=''): cv.string,
vol.Optional(CONF_MQTT_SERVER_PASSWORD, default=''): cv.string
})

CONFIG_SCHEMA = vol.Schema({
Expand Down
9 changes: 8 additions & 1 deletion custom_components/shelly/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
CONF_DIV = 'div'
CONF_UNIT = 'unit'
CONF_MQTT_PORT = 'mqtt_port'
CONF_MQTT_SERVER_HOST = 'mqtt_server_host'
CONF_MQTT_SERVER_PORT = 'mqtt_server_port'
CONF_MQTT_SERVER_USERNAME = 'mqtt_server_username'
CONF_MQTT_SERVER_PASSWORD = 'mqtt_server_password'

#Debug settings used for testing
CONF_LOCAL_PY_SHELLY = 'debug_local_py_shelly'
Expand Down Expand Up @@ -287,6 +291,7 @@
SENSOR_TYPE_TILT = 'tilt'
SENSOR_TYPE_PPM = 'ppm'
SENSOR_TYPE_TOTAL_WORK_TIME = 'total_work_time'
SENSOR_TYPE_EXT_SWITCH = 'external_switch'

SENSOR_TYPES_CFG = {
SENSOR_TYPE_DEFAULT:
Expand Down Expand Up @@ -335,7 +340,7 @@
SENSOR_TYPE_CURRENT:
['Current', 'A', 'mdi:alpha-i-circle-outline', None, None],
SENSOR_TYPE_CLICK_TYPE:
['Click type', '', 'mdi:hockey-puck', None, None],
['Click type', '', 'mdi:light-switch', None, None],
SENSOR_TYPE_TILT:
['Tilt', '', 'mdi:angle-acute', None, None],
SENSOR_TYPE_VIBRATION:
Expand All @@ -344,4 +349,6 @@
['Concentration', 'PPM', 'mdi:gauge', None, None],
SENSOR_TYPE_TOTAL_WORK_TIME:
['Total work time', 's', 'mdi:briefcase-clock', None, None],
SENSOR_TYPE_EXT_SWITCH:
['External switch', '', 'mdi:electric-switch', None, 'bool'],
}
1 change: 0 additions & 1 deletion custom_components/shelly/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def set_cover_position(self, **kwargs):
pos = kwargs[ATTR_POSITION]
self._dev.set_position(pos)
self._position = pos

self._update_ha_state()

def stop_cover(self, **_kwargs):
Expand Down
13 changes: 10 additions & 3 deletions custom_components/shelly/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ def __init__(self, dev, instance):

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

def _update_ha_state(self):
def _update_ha_state(self):
self.schedule_update_ha_state(True)

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._update_ha_state()

if self._dev.info_values is not None:
Expand Down Expand Up @@ -94,7 +93,11 @@ def _debug_info(self, key, dev):
if key in dev.info_values_coap:
dbg += ", C=" + str(dev.info_values_coap[key])
if key in dev.info_values_status:
dbg += ", S=" + str(dev.info_values_status[key])
dbg += ", R=" + str(dev.info_values_status[key])
if key in dev.info_values_mqtt:
dbg += ", M=" + str(dev.info_values_mqtt[key])
if key in dev.info_values_mqtt_status:
dbg += ", MS=" + str(dev.info_values_mqtt_status[key])
return dbg

def _debug_add_state_info(self, attrs):
Expand All @@ -104,6 +107,10 @@ def _debug_add_state_info(self, attrs):
attrs['state_CoAP'] = self._dev.state_coap
if self._dev.state_status is not None:
attrs['state_RESTAPI'] = self._dev.state_status
if self._dev.state_mqtt is not None:
attrs['state_MQTT'] = self._dev.state_mqtt
if self._dev.state_mqtt_status is not None:
attrs['state_MQTT_status'] = self._dev.state_mqtt_status

@property
def device_state_attributes(self):
Expand Down
62 changes: 56 additions & 6 deletions custom_components/shelly/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
"""
import logging

from homeassistant.const import (
ATTR_STATE
)
from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT, ATTR_HS_COLOR,
ATTR_WHITE_VALUE,
SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_COLOR_TEMP, SUPPORT_EFFECT,
SUPPORT_WHITE_VALUE,
)
from functools import partial
from homeassistant.helpers import config_validation as cv, entity_platform, service
import voluptuous as vol

try:
from homeassistant.components.light import (LightEntity)
except:
Expand Down Expand Up @@ -52,6 +59,17 @@ async def async_discover_light(dev, instance):
else:
async_add_entities([ShellyDimmer(dev, instance)])

platform = entity_platform.current_platform.get()
platform.async_register_entity_service(
'set_value',
{
vol.Optional(ATTR_STATE): bool,
vol.Optional(ATTR_BRIGHTNESS): cv.positive_int,
vol.Optional(ATTR_COLOR_TEMP): cv.positive_int,
},
"async_set_value"
)

async_dispatcher_connect(
hass,
"shelly_new_light",
Expand All @@ -75,14 +93,12 @@ def is_on(self):
def turn_on(self, **kwargs):
self._dev.turn_on()
self._state = True

self._update_ha_state()


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

self._update_ha_state()

def update(self):
Expand Down Expand Up @@ -136,13 +152,30 @@ def turn_on(self, **kwargs):
else:
self._dev.turn_on(brightness)
self._state = True

self._update_ha_state()

def set_value(self, **kwargs):
brightness = None
color_temp = None
state = None
if ATTR_BRIGHTNESS in kwargs:
brightness = int(kwargs[ATTR_BRIGHTNESS] / 2.55)
if ATTR_COLOR_TEMP in kwargs:
color_temp = int(mired_to_kelvin(kwargs[ATTR_COLOR_TEMP]))
if color_temp > self._color_temp_max:
color_temp = self._color_temp_max
if color_temp < self._color_temp_min:
color_temp = self._color_temp_min
if ATTR_STATE in kwargs:
state = kwargs[ATTR_STATE]
self._dev.set_values(state=state, brightness=brightness, color_temp=color_temp)

async def async_set_value(self, **kwargs):
await self.hass.async_add_executor_job(partial(self.set_value, **kwargs))

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

self._update_ha_state()

@property
Expand Down Expand Up @@ -229,6 +262,25 @@ def is_on(self):
"""Return status of light"""
return self._state

def set_value(self, **kwargs):
brightness = None
color_temp = None
state = None
if ATTR_BRIGHTNESS in kwargs:
brightness = int(kwargs[ATTR_BRIGHTNESS] / 2.55)
if ATTR_COLOR_TEMP in kwargs:
color_temp = int(mired_to_kelvin(kwargs[ATTR_COLOR_TEMP]))
if color_temp > self._color_temp_max:
color_temp = self._color_temp_max
if color_temp < self._color_temp_min:
color_temp = self._color_temp_min
if ATTR_STATE in kwargs:
state = kwargs[ATTR_STATE]
self._dev.set_values(state=state, brightness=brightness, color_temp=color_temp)

async def async_set_value(self, **kwargs):
await self.hass.async_add_executor_job(partial(self.set_value, **kwargs))

def turn_on(self, **kwargs):
"""Turn on light"""
brightness = None
Expand Down Expand Up @@ -276,14 +328,12 @@ def turn_on(self, **kwargs):
mode=mode, effect=effect_nr, white_value=white_value)

self._state = True

self._update_ha_state()

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

self._update_ha_state()

def update(self):
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.13"]
"requirements": ["pyShelly==0.2.21", "paho-mqtt==1.5.1"]
}
4 changes: 0 additions & 4 deletions custom_components/shelly/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@ def turn_on(self, **_kwargs):
"""Turn on device"""
self._dev.turn_on()
self._state = True

self._update_ha_state()

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

self._update_ha_state()

def update(self):
Expand Down Expand Up @@ -133,13 +131,11 @@ def is_on(self):
async def async_turn_on(self, **_kwargs):
"""Trig the firmware update"""
self._updating = True

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._update_ha_state()

def remove(self):
Expand Down
10 changes: 10 additions & 0 deletions custom_components/shelly/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
},
"title": "Shelly - Settings [1/3]",
"description": "Configuration (2/2)"
},
"config_mqtt": {
"data": {
"mqtt_server_host": "Host or ip address",
"mqtt_server_port": "Port",
"mqtt_server_username": "User name",
"mqtt_server_password": "Password"
},
"title": "Shelly - Settings [1/3]",
"description": "MQTT server connection"
},
"attributes": {
"data": {
Expand Down

0 comments on commit b965183

Please sign in to comment.