Skip to content

Commit

Permalink
Add TRV support
Browse files Browse the repository at this point in the history
  • Loading branch information
hakana committed May 10, 2022
1 parent 1879962 commit 31f97e5
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 28 deletions.
14 changes: 3 additions & 11 deletions custom_components/shelly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

_LOGGER = logging.getLogger(__name__)

__version__ = "1.0.0-b1"
__version__ = "1.0.0-b2"
VERSION = __version__

async def async_setup(hass, config):
Expand Down Expand Up @@ -632,16 +632,6 @@ async def _async_device_added(self, dev, _code):
SENSOR_CONSUMPTION in sensor_cfg or \
SENSOR_POWER in sensor_cfg: #POWER deprecated
self.add_device("sensor", dev)
# if SENSOR_TOTAL_CONSUMPTION in sensor_cfg or \
# SENSOR_CONSUMPTION in sensor_cfg or \
# SENSOR_POWER in sensor_cfg: #POWER deprecated
# self.add_device("sensor", {'sensor_type' : 'total_consumption',
# 'itm': dev})
# if SENSOR_TOTAL_RETURNED in sensor_cfg or \
# SENSOR_CONSUMPTION in sensor_cfg or \
# SENSOR_POWER in sensor_cfg: #POWER deprecated
# self.add_device("sensor", {'sensor_type' : 'total_returned',
# 'itm': dev})
elif dev.device_type == 'SWITCH':
sensor_cfg = self._get_sensor_config(dev.id, dev.block.id)
if SENSOR_SWITCH in sensor_cfg:
Expand All @@ -652,6 +642,8 @@ async def _async_device_added(self, dev, _code):
self.add_device("binary_sensor", dev)
elif dev.device_type in ["LIGHT", "DIMMER", "RGBLIGHT"]:
self.add_device("light", dev)
elif dev.device_type == 'TRV':
self.add_device("climate", dev)
else:
_LOGGER.error("Unknown device type, %s", dev.device_type)

Expand Down
82 changes: 82 additions & 0 deletions custom_components/shelly/climate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
Shelly platform for the climate component.
For more details about this platform, please refer to the documentation
https://home-assistant.io/components/shelly/
"""

import logging

from homeassistant.components.climate import ClimateEntity
from homeassistant.const import (
ATTR_TEMPERATURE,
PRECISION_WHOLE,
TEMP_CELSIUS
)
try:
from homeassistant.components.climate.const import HVACMode, ClimateEntityFeature
MODE_HEAT = HVACMode.HEAT
MODE_OFF = HVACMode.OFF
FEATURES = ClimateEntityFeature.TARGET_TEMPERATURE
except :
#deprecated as of Home Assistant 2022.5
from homeassistant.components.climate.const import CURRENT_HVAC_OFF, CURRENT_HVAC_HEAT, SUPPORT_TARGET_TEMPERATURE
MODE_HEAT = CURRENT_HVAC_HEAT
MODE_OFF = CURRENT_HVAC_OFF
FEATURES = SUPPORT_TARGET_TEMPERATURE

from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import *

# from .sensor import ShellySensor
from .device import ShellyDevice
from .block import ShellyBlock

from typing import Any

_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Shelly climate dynamically."""
async def async_discover_climate(dev, instance):
"""Discover and add a discovered sensor."""
async_add_entities([ShellyClimate(dev, instance)])

async_dispatcher_connect(
hass,
"shelly_new_climate",
async_discover_climate
)

class ShellyClimate(ShellyDevice, ClimateEntity):
_attr_hvac_mode = MODE_HEAT
_attr_hvac_modes = [MODE_HEAT] #MODE_OFF,
_attr_max_temp = 30
_attr_min_temp = 5
_attr_supported_features = FEATURES
_attr_target_temperature_step = PRECISION_WHOLE
_attr_temperature_unit = TEMP_CELSIUS

"""Representation of an Shelly Switch."""
def __init__(self, dev, instance):
"""Initialize an ShellyClimate."""
ShellyDevice.__init__(self, dev, instance)
self.entity_id = "climate" + self.entity_id
self._state = None
self._master_unit = True

async def async_set_hvac_mode(self, hvac_mode: str) -> None:
pass

def set_temperature(self, **kwargs: Any) -> None:
if (temp := kwargs.get(ATTR_TEMPERATURE)) is None:
return
self._dev.set_target_temp(temp)

async def async_update(self) -> None:
self._attr_target_temperature = self._dev.info_values.get("target_temperature")
self._attr_current_temperature = self._dev.info_values.get("temperature")
self._attr_available = True
self._attr_hvac_mode = MODE_HEAT
self._attr_icon = "mdi:radiator"

25 changes: 21 additions & 4 deletions custom_components/shelly/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@
'battery': {CONF_UNIT:'%'},
'ppm': {CONF_UNIT:'PPM'},
'total_work_time': DEFAULT_TIME,
'position': {CONF_UNIT:'%'},
'target_temperature': {CONF_UNIT:'°C', CONF_DECIMALS:1}
}

SHELLY_DEVICE_ID = 'device_id'
Expand Down Expand Up @@ -220,6 +222,7 @@
ATTRIBUTE_PPM = 'ppm'
ATTRIBUTE_SENSOR = 'sensor'
ATTRIBUTE_TOTAL_WORK_TIME = 'total_work_time'
ATTRIBUTE_POSITION = 'position'

ALL_ATTRIBUTES = {
ATTRIBUTE_IP_ADDRESS,
Expand Down Expand Up @@ -256,7 +259,8 @@
ATTRIBUTE_ILLUMINANCE,
ATTRIBUTE_PPM,
ATTRIBUTE_SENSOR,
ATTRIBUTE_TOTAL_WORK_TIME
ATTRIBUTE_TOTAL_WORK_TIME,
ATTRIBUTE_POSITION
}

EXTRA_ATTRIBUTES = {
Expand Down Expand Up @@ -290,7 +294,8 @@
ATTRIBUTE_ILLUMINANCE,
ATTRIBUTE_PPM,
ATTRIBUTE_SENSOR,
ATTRIBUTE_TOTAL_WORK_TIME
ATTRIBUTE_TOTAL_WORK_TIME,
ATTRIBUTE_POSITION
}

SENSOR_ALL = 'all'
Expand Down Expand Up @@ -324,6 +329,8 @@
SENSOR_ILLUMINANCE = 'illuminance'
SENSOR_PPM = 'ppm'
SENSOR_TOTAL_WORK_TIME = 'total_work_time'
SENSOR_POSITION = 'position'
SENSOR_TARGET_TEMP = 'target_temperature'

ALL_SENSORS = {
SENSOR_RSSI: {'attr':'rssi'},
Expand All @@ -350,7 +357,9 @@
SENSOR_HUMIDITY : {'attr':'humidity'},
SENSOR_ILLUMINANCE : {'attr':'illuminance'},
SENSOR_PPM : {'attr':'ppm'},
SENSOR_TOTAL_WORK_TIME : {'attr':'total_work_time'}
SENSOR_TOTAL_WORK_TIME : {'attr':'total_work_time'},
SENSOR_POSITION : {'attr':'position'},
SENSOR_TARGET_TEMP: {'attr':'target_temperature'},
}

EXTRA_SENSORS = {
Expand All @@ -365,7 +374,8 @@
DEFAULT_SENSORS = [
SENSOR_CURRENT_CONSUMPTION,
SENSOR_TOTAL_CONSUMPTION,
SENSOR_SWITCH
SENSOR_SWITCH,
SENSOR_POSITION
]

SENSOR_TYPE_TEMPERATURE = 'temperature'
Expand Down Expand Up @@ -398,6 +408,8 @@
SENSOR_TYPE_TOTAL_WORK_TIME = 'total_work_time'
SENSOR_TYPE_EXT_SWITCH = 'external_switch'
SENSOR_TYPE_MOTION = 'motion'
SENSOR_TYPE_POSITION = 'position'
SENSOR_TYPE_TARGET_TEMP = 'target_temperature'

SENSOR_TYPES_CFG = {
SENSOR_TYPE_DEFAULT:
Expand Down Expand Up @@ -461,5 +473,10 @@
['External switch', '', 'mdi:electric-switch', None, 'bool'],
SENSOR_TYPE_MOTION:
['Motion', '', 'mdi:motion-sensor', DEVICE_CLASS_MOTION, 'bool'],
SENSOR_TYPE_POSITION:
['Position', '', 'mdi:percent', None, None],
SENSOR_TYPE_TARGET_TEMP:
['Target temperature', TEMP_CELSIUS, "mdi:target-variant", None, None],

}

10 changes: 0 additions & 10 deletions custom_components/shelly/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@
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."""
# dev = get_device_from_hass(hass, discovery_info)
# if dev.device_type == "RELAY":
# add_devices([ShellyLightRelay(dev, hass)])
# elif dev.device_type == "DIMMER":
# add_devices([ShellyDimmer(dev, hass)])
# else:
# add_devices([ShellyRGB(dev, hass)])

async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Shelly light sensors dynamically."""
async def async_discover_light(dev, instance):
Expand Down
4 changes: 2 additions & 2 deletions custom_components/shelly/manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"domain": "shelly",
"name": "Shelly smart home",
"version": "1.0.0-b1",
"version": "1.0.0-b2",
"config_flow": true,
"documentation": "https://github.com/StyraHem/ShellyForHASS/blob/master/README.md",
"dependencies": ["zeroconf"],
"codeowners": ["@hakana","@StyraHem"],
"requirements": ["pyShelly==1.0.1", "paho-mqtt==1.6.1", "websocket-client"],
"requirements": ["pyShelly==1.0.2", "paho-mqtt==1.6.1", "websocket-client"],
"iot_class": "local_push"
}
4 changes: 3 additions & 1 deletion custom_components/shelly/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"total_work_time": "Total work time",
"latest_beta_fw_version": "Latest beta version",
"mqtt": "MQTT connected",
"cloud": "Cloud status"
"cloud": "Cloud status",
"position": "Position",
"target_temperature" : "Target temperature"
},
"config" : {
"groups" : {
Expand Down

0 comments on commit 31f97e5

Please sign in to comment.