Velbus auto-discovery (#13742)
* remove velbus fan and light platforms these platforms should not be there since they can be created with template components based on switch platform * use latest version of python-velbus which supports auto-discovery of modules * fix linting errors * fix linting errors * fix linting errors * address review comments from @MartinHjelmare * update based on automatic feedback * fix linting errors * update dependency * syntax corrections * fix lint warning * split out common functionality in VelbusEntity use sync methods for loading platforms support unique_ids so that entities are registred in entity registry * fix linting errors * fix linting errors * fix linting errors * integrate review comments (common functionality in VelbusEntity class) * rename DOMAIN import to VELBUS_DOMAIN * revert change created by requirements script * regen
This commit is contained in:
1 parent
5e1836f3a2
commit
8a81ee3b4f
6 files changed
+100
-456
No files matched your search
@@ -4,108 +4,42 @@ Support for Velbus switches.
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/switch.velbus/
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import CONF_NAME, CONF_DEVICES
|
||||
from homeassistant.components.switch import SwitchDevice, PLATFORM_SCHEMA
|
||||
from homeassistant.components.velbus import DOMAIN
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.switch import SwitchDevice
|
||||
from homeassistant.components.velbus import (
|
||||
DOMAIN as VELBUS_DOMAIN, VelbusEntity)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
SWITCH_SCHEMA = {
|
||||
vol.Required('module'): cv.positive_int,
|
||||
vol.Required('channel'): cv.positive_int,
|
||||
vol.Required(CONF_NAME): cv.string
|
||||
}
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_DEVICES):
|
||||
vol.All(cv.ensure_list, [SWITCH_SCHEMA])
|
||||
})
|
||||
|
||||
DEPENDENCIES = ['velbus']
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up the Switch."""
|
||||
velbus = hass.data[DOMAIN]
|
||||
devices = []
|
||||
|
||||
for switch in config[CONF_DEVICES]:
|
||||
devices.append(VelbusSwitch(switch, velbus))
|
||||
add_devices(devices)
|
||||
return True
|
||||
async def async_setup_platform(hass, config, async_add_devices,
|
||||
discovery_info=None):
|
||||
"""Set up the Velbus Switch platform."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
switches = []
|
||||
for switch in discovery_info:
|
||||
module = hass.data[VELBUS_DOMAIN].get_module(switch[0])
|
||||
channel = switch[1]
|
||||
switches.append(VelbusSwitch(module, channel))
|
||||
async_add_devices(switches)
|
||||
|
||||
|
||||
class VelbusSwitch(SwitchDevice):
|
||||
class VelbusSwitch(VelbusEntity, SwitchDevice):
|
||||
"""Representation of a switch."""
|
||||
|
||||
def __init__(self, switch, velbus):
|
||||
"""Initialize a Velbus switch."""
|
||||
self._velbus = velbus
|
||||
self._name = switch[CONF_NAME]
|
||||
self._module = switch['module']
|
||||
self._channel = switch['channel']
|
||||
self._state = False
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_added_to_hass(self):
|
||||
"""Add listener for Velbus messages on bus."""
|
||||
def _init_velbus():
|
||||
"""Initialize Velbus on startup."""
|
||||
self._velbus.subscribe(self._on_message)
|
||||
self.get_status()
|
||||
|
||||
yield from self.hass.async_add_job(_init_velbus)
|
||||
|
||||
def _on_message(self, message):
|
||||
import velbus
|
||||
if isinstance(message, velbus.RelayStatusMessage) and \
|
||||
message.address == self._module and \
|
||||
message.channel == self._channel:
|
||||
self._state = message.is_on()
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the display name of this switch."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Disable polling."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if the switch is on."""
|
||||
return self._state
|
||||
return self._module.is_on(self._channel)
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Instruct the switch to turn on."""
|
||||
import velbus
|
||||
message = velbus.SwitchRelayOnMessage()
|
||||
message.set_defaults(self._module)
|
||||
message.relay_channels = [self._channel]
|
||||
self._velbus.send(message)
|
||||
self._module.turn_on(self._channel)
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Instruct the switch to turn off."""
|
||||
import velbus
|
||||
message = velbus.SwitchRelayOffMessage()
|
||||
message.set_defaults(self._module)
|
||||
message.relay_channels = [self._channel]
|
||||
self._velbus.send(message)
|
||||
|
||||
def get_status(self):
|
||||
"""Retrieve current status."""
|
||||
import velbus
|
||||
message = velbus.ModuleStatusRequestMessage()
|
||||
message.set_defaults(self._module)
|
||||
message.channels = [self._channel]
|
||||
self._velbus.send(message)
|
||||
self._module.turn_off(self._channel)
|
||||
Reference in new issue
Block a user