mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Update plugwise to async and config_flow switch part (#36383)
* Add switch component * Update homeassistant/components/plugwise/switch.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/plugwise/switch.py Co-authored-by: J. Nick Koston <nick@koston.org> * Improvements by @bdraco Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
1771fbbd34
commit
61c08e792d
@ -607,6 +607,7 @@ omit =
|
|||||||
homeassistant/components/plugwise/binary_sensor.py
|
homeassistant/components/plugwise/binary_sensor.py
|
||||||
homeassistant/components/plugwise/climate.py
|
homeassistant/components/plugwise/climate.py
|
||||||
homeassistant/components/plugwise/sensor.py
|
homeassistant/components/plugwise/sensor.py
|
||||||
|
homeassistant/components/plugwise/switch.py
|
||||||
homeassistant/components/plum_lightpad/*
|
homeassistant/components/plum_lightpad/*
|
||||||
homeassistant/components/pocketcasts/sensor.py
|
homeassistant/components/pocketcasts/sensor.py
|
||||||
homeassistant/components/point/*
|
homeassistant/components/point/*
|
||||||
|
@ -24,7 +24,7 @@ CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA)
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
SENSOR_PLATFORMS = ["sensor"]
|
SENSOR_PLATFORMS = ["sensor"]
|
||||||
ALL_PLATFORMS = ["binary_sensor", "climate", "sensor"]
|
ALL_PLATFORMS = ["binary_sensor", "climate", "sensor", "switch"]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: dict):
|
async def async_setup(hass: HomeAssistant, config: dict):
|
||||||
|
84
homeassistant/components/plugwise/switch.py
Normal file
84
homeassistant/components/plugwise/switch.py
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
"""Plugwise Switch component for HomeAssistant."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from Plugwise_Smile.Smile import Smile
|
||||||
|
|
||||||
|
from homeassistant.components.switch import SwitchEntity
|
||||||
|
from homeassistant.core import callback
|
||||||
|
|
||||||
|
from . import SmileGateway
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
|
"""Set up the Smile switches from a config entry."""
|
||||||
|
api = hass.data[DOMAIN][config_entry.entry_id]["api"]
|
||||||
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
|
||||||
|
|
||||||
|
entities = []
|
||||||
|
all_devices = api.get_all_devices()
|
||||||
|
for dev_id, device_properties in all_devices.items():
|
||||||
|
if "plug" in device_properties["types"]:
|
||||||
|
model = "Metered Switch"
|
||||||
|
entities.append(
|
||||||
|
PwSwitch(api, coordinator, device_properties["name"], dev_id, model)
|
||||||
|
)
|
||||||
|
|
||||||
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
|
||||||
|
class PwSwitch(SmileGateway, SwitchEntity):
|
||||||
|
"""Representation of a Plugwise plug."""
|
||||||
|
|
||||||
|
def __init__(self, api, coordinator, name, dev_id, model):
|
||||||
|
"""Set up the Plugwise API."""
|
||||||
|
super().__init__(api, coordinator, name, dev_id)
|
||||||
|
|
||||||
|
self._model = model
|
||||||
|
|
||||||
|
self._is_on = False
|
||||||
|
|
||||||
|
self._unique_id = f"{dev_id}-plug"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self):
|
||||||
|
"""Return true if device is on."""
|
||||||
|
return self._is_on
|
||||||
|
|
||||||
|
async def async_turn_on(self, **kwargs):
|
||||||
|
"""Turn the device on."""
|
||||||
|
try:
|
||||||
|
if await self._api.set_relay_state(self._dev_id, "on"):
|
||||||
|
self._is_on = True
|
||||||
|
self.async_write_ha_state()
|
||||||
|
except Smile.PlugwiseError:
|
||||||
|
_LOGGER.error("Error while communicating to device")
|
||||||
|
|
||||||
|
async def async_turn_off(self, **kwargs):
|
||||||
|
"""Turn the device off."""
|
||||||
|
try:
|
||||||
|
if await self._api.set_relay_state(self._dev_id, "off"):
|
||||||
|
self._is_on = False
|
||||||
|
self.async_write_ha_state()
|
||||||
|
except Smile.PlugwiseError:
|
||||||
|
_LOGGER.error("Error while communicating to device")
|
||||||
|
|
||||||
|
@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:
|
||||||
|
_LOGGER.error("Received no data for device %s.", self._name)
|
||||||
|
self.async_write_ha_state()
|
||||||
|
return
|
||||||
|
|
||||||
|
if "relay" in data:
|
||||||
|
self._is_on = data["relay"]
|
||||||
|
|
||||||
|
self.async_write_ha_state()
|
Loading…
x
Reference in New Issue
Block a user