Best Python code snippet using autotest_python
velbus.py
Source:velbus.py  
1"""2Support for Velbus covers.3For more details about this platform, please refer to the documentation at4https://home-assistant.io/components/cover.velbus/5"""6import logging7import time8import voluptuous as vol9from homeassistant.components.cover import (10    CoverDevice, PLATFORM_SCHEMA, SUPPORT_OPEN, SUPPORT_CLOSE,11    SUPPORT_STOP)12from homeassistant.components.velbus import DOMAIN13from homeassistant.const import (CONF_COVERS, CONF_NAME)14import homeassistant.helpers.config_validation as cv15_LOGGER = logging.getLogger(__name__)16COVER_SCHEMA = vol.Schema({17    vol.Required('module'): cv.positive_int,18    vol.Required('open_channel'): cv.positive_int,19    vol.Required('close_channel'): cv.positive_int,20    vol.Required(CONF_NAME): cv.string21})22PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({23    vol.Required(CONF_COVERS): vol.Schema({cv.slug: COVER_SCHEMA}),24})25DEPENDENCIES = ['velbus']26def setup_platform(hass, config, add_entities, discovery_info=None):27    """Set up cover controlled by Velbus."""28    devices = config.get(CONF_COVERS, {})29    covers = []30    velbus = hass.data[DOMAIN]31    for device_name, device_config in devices.items():32        covers.append(33            VelbusCover(34                velbus,35                device_config.get(CONF_NAME, device_name),36                device_config.get('module'),37                device_config.get('open_channel'),38                device_config.get('close_channel')39            )40        )41    if not covers:42        _LOGGER.error("No covers added")43        return False44    add_entities(covers)45class VelbusCover(CoverDevice):46    """Representation a Velbus cover."""47    def __init__(self, velbus, name, module, open_channel, close_channel):48        """Initialize the cover."""49        self._velbus = velbus50        self._name = name51        self._close_channel_state = None52        self._open_channel_state = None53        self._module = module54        self._open_channel = open_channel55        self._close_channel = close_channel56    async def async_added_to_hass(self):57        """Add listener for Velbus messages on bus."""58        def _init_velbus():59            """Initialize Velbus on startup."""60            self._velbus.subscribe(self._on_message)61            self.get_status()62        await self.hass.async_add_job(_init_velbus)63    def _on_message(self, message):64        import velbus65        if isinstance(message, velbus.RelayStatusMessage):66            if message.address == self._module:67                if message.channel == self._close_channel:68                    self._close_channel_state = message.is_on()69                    self.schedule_update_ha_state()70                if message.channel == self._open_channel:71                    self._open_channel_state = message.is_on()72                    self.schedule_update_ha_state()73    @property74    def supported_features(self):75        """Flag supported features."""76        return SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP77    @property78    def should_poll(self):79        """Disable polling."""80        return False81    @property82    def name(self):83        """Return the name of the cover."""84        return self._name85    @property86    def is_closed(self):87        """Return if the cover is closed."""88        return self._close_channel_state89    @property90    def current_cover_position(self):91        """Return current position of cover.92        None is unknown.93        """94        return None95    def _relay_off(self, channel):96        import velbus97        message = velbus.SwitchRelayOffMessage()98        message.set_defaults(self._module)99        message.relay_channels = [channel]100        self._velbus.send(message)101    def _relay_on(self, channel):102        import velbus103        message = velbus.SwitchRelayOnMessage()104        message.set_defaults(self._module)105        message.relay_channels = [channel]106        self._velbus.send(message)107    def open_cover(self, **kwargs):108        """Open the cover."""109        self._relay_off(self._close_channel)110        time.sleep(0.3)111        self._relay_on(self._open_channel)112    def close_cover(self, **kwargs):113        """Close the cover."""114        self._relay_off(self._open_channel)115        time.sleep(0.3)116        self._relay_on(self._close_channel)117    def stop_cover(self, **kwargs):118        """Stop the cover."""119        self._relay_off(self._open_channel)120        time.sleep(0.3)121        self._relay_off(self._close_channel)122    def get_status(self):123        """Retrieve current status."""124        import velbus125        message = velbus.ModuleStatusRequestMessage()126        message.set_defaults(self._module)127        message.channels = [self._open_channel, self._close_channel]...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
