Add support for Stretch product to Plugwise integration (#40108)

* Initial switch-group/stretch with failing wk_lisa_battery test

* Adding switch tests, but TypeErrors, needs more investigation

* Fixes and tests aligned

* Review updates

* Use const

* Comments

* Add stretch hostname for testing part

* Remove unused consts

* Revert guardings in line with -beta

* Catchup with dev (mostly with ourselves from #41201)

* Update docstring

* Remove debug logging

* Fix for #42725 (incorrect entity namingi)

* Fix naming for gas interval

* Add missing CONF_USERNAME and use of it

* Change "dummy" to "class"

* Don't use "class"

* Fix CONF_USERNAME default, dummy and other consts

Co-authored-by: Bouwe Westerdijk <11290930+bouwew@users.noreply.github.com>
This commit is contained in:
Tom 2020-11-08 18:09:43 +01:00 committed by GitHub
parent 55cdec8c4e
commit 877bfcb308
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 300 additions and 108 deletions

View file

@ -7,7 +7,7 @@ from Plugwise_Smile.Smile import Smile
from homeassistant.components.switch import SwitchEntity
from homeassistant.core import callback
from .const import COORDINATOR, DOMAIN
from .const import COORDINATOR, DOMAIN, SWITCH_ICON
from .gateway import SmileGateway
_LOGGER = logging.getLogger(__name__)
@ -19,12 +19,27 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
entities = []
switch_classes = ["plug", "switch_group"]
all_devices = api.get_all_devices()
for dev_id, device_properties in all_devices.items():
if "plug" in device_properties["types"]:
model = "Metered Switch"
members = None
model = None
if any(
switch_class in device_properties["types"]
for switch_class in switch_classes
):
if "plug" in device_properties["types"]:
model = "Metered Switch"
if "switch_group" in device_properties["types"]:
members = device_properties["members"]
model = "Switch Group"
entities.append(
PwSwitch(api, coordinator, device_properties["name"], dev_id, model)
PwSwitch(
api, coordinator, device_properties["name"], dev_id, members, model
)
)
async_add_entities(entities, True)
@ -33,13 +48,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class PwSwitch(SmileGateway, SwitchEntity):
"""Representation of a Plugwise plug."""
def __init__(self, api, coordinator, name, dev_id, model):
def __init__(self, api, coordinator, name, dev_id, members, model):
"""Set up the Plugwise API."""
super().__init__(api, coordinator, name, dev_id)
self._members = members
self._model = model
self._is_on = False
self._icon = SWITCH_ICON
self._unique_id = f"{dev_id}-plug"
@ -48,10 +65,18 @@ class PwSwitch(SmileGateway, SwitchEntity):
"""Return true if device is on."""
return self._is_on
@property
def icon(self):
"""Return the icon of this entity."""
return self._icon
async def async_turn_on(self, **kwargs):
"""Turn the device on."""
try:
if await self._api.set_relay_state(self._dev_id, "on"):
state_on = await self._api.set_relay_state(
self._dev_id, self._members, "on"
)
if state_on:
self._is_on = True
self.async_write_ha_state()
except Smile.PlugwiseError:
@ -60,7 +85,10 @@ class PwSwitch(SmileGateway, SwitchEntity):
async def async_turn_off(self, **kwargs):
"""Turn the device off."""
try:
if await self._api.set_relay_state(self._dev_id, "off"):
state_off = await self._api.set_relay_state(
self._dev_id, self._members, "off"
)
if state_off:
self._is_on = False
self.async_write_ha_state()
except Smile.PlugwiseError:
@ -69,8 +97,6 @@ class PwSwitch(SmileGateway, SwitchEntity):
@callback
def _async_process_data(self):
"""Update the data from the Plugs."""
_LOGGER.debug("Update switch called")
data = self._api.get_device_data(self._dev_id)
if not data: