2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Velbus switches."""
|
2017-07-26 12:03:29 +00:00
|
|
|
import logging
|
|
|
|
|
2019-07-30 10:56:40 +00:00
|
|
|
from velbus.util import VelbusException
|
|
|
|
|
2018-08-05 08:47:17 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-07-29 07:21:26 +00:00
|
|
|
from . import VelbusEntity
|
|
|
|
from .const import DOMAIN
|
2017-07-26 12:03:29 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-07-29 07:21:26 +00:00
|
|
|
"""Old way."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
|
|
"""Set up Velbus switch based on config_entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
cntrl = hass.data[DOMAIN][entry.entry_id]["cntrl"]
|
|
|
|
modules_data = hass.data[DOMAIN][entry.entry_id]["switch"]
|
2019-07-29 07:21:26 +00:00
|
|
|
entities = []
|
|
|
|
for address, channel in modules_data:
|
|
|
|
module = cntrl.get_module(address)
|
2019-07-31 19:25:30 +00:00
|
|
|
entities.append(VelbusSwitch(module, channel))
|
2019-07-29 07:21:26 +00:00
|
|
|
async_add_entities(entities)
|
2017-07-26 12:03:29 +00:00
|
|
|
|
|
|
|
|
2018-08-05 08:47:17 +00:00
|
|
|
class VelbusSwitch(VelbusEntity, SwitchDevice):
|
2017-07-26 12:03:29 +00:00
|
|
|
"""Representation of a switch."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the switch is on."""
|
2018-08-05 08:47:17 +00:00
|
|
|
return self._module.is_on(self._channel)
|
2017-07-26 12:03:29 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Instruct the switch to turn on."""
|
2019-07-30 10:56:40 +00:00
|
|
|
try:
|
|
|
|
self._module.turn_on(self._channel)
|
|
|
|
except VelbusException as err:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("A Velbus error occurred: %s", err)
|
2017-07-26 12:03:29 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Instruct the switch to turn off."""
|
2019-07-30 10:56:40 +00:00
|
|
|
try:
|
|
|
|
self._module.turn_off(self._channel)
|
|
|
|
except VelbusException as err:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("A Velbus error occurred: %s", err)
|