当前位置: 首页>>代码示例>>Python>>正文


Python Script.run方法代码示例

本文整理汇总了Python中homeassistant.helpers.script.Script.run方法的典型用法代码示例。如果您正苦于以下问题:Python Script.run方法的具体用法?Python Script.run怎么用?Python Script.run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在homeassistant.helpers.script.Script的用法示例。


在下文中一共展示了Script.run方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: WOLSwitch

# 需要导入模块: from homeassistant.helpers.script import Script [as 别名]
# 或者: from homeassistant.helpers.script.Script import run [as 别名]
class WOLSwitch(SwitchDevice):
    """Representation of a wake on lan switch."""

    def __init__(self, hass, name, host, mac_address,
                 off_action, broadcast_address):
        """Initialize the WOL switch."""
        from wakeonlan import wol
        self._hass = hass
        self._name = name
        self._host = host
        self._mac_address = mac_address
        self._broadcast_address = broadcast_address
        self._off_script = Script(hass, off_action) if off_action else None
        self._state = False
        self._wol = wol
        self.update()

    @property
    def should_poll(self):
        """Return the polling state."""
        return True

    @property
    def is_on(self):
        """Return true if switch is on."""
        return self._state

    @property
    def name(self):
        """Return the name of the switch."""
        return self._name

    def turn_on(self):
        """Turn the device on."""
        if self._broadcast_address:
            self._wol.send_magic_packet(
                self._mac_address, ip_address=self._broadcast_address)
        else:
            self._wol.send_magic_packet(self._mac_address)

    def turn_off(self):
        """Turn the device off if an off action is present."""
        if self._off_script is not None:
            self._off_script.run()

    def update(self):
        """Check if device is on and update the state."""
        if platform.system().lower() == 'windows':
            ping_cmd = ['ping', '-n', '1', '-w',
                        str(DEFAULT_PING_TIMEOUT * 1000), str(self._host)]
        else:
            ping_cmd = ['ping', '-c', '1', '-W',
                        str(DEFAULT_PING_TIMEOUT), str(self._host)]

        status = sp.call(ping_cmd, stdout=sp.DEVNULL, stderr=sp.DEVNULL)
        self._state = not bool(status)
开发者ID:Khabi,项目名称:home-assistant,代码行数:58,代码来源:wake_on_lan.py

示例2: WOLSwitch

# 需要导入模块: from homeassistant.helpers.script import Script [as 别名]
# 或者: from homeassistant.helpers.script.Script import run [as 别名]
class WOLSwitch(SwitchDevice):
    """Representation of a wake on lan switch."""

    def __init__(self, hass, name, host, mac_address, off_action):
        """Initialize the WOL switch."""
        from wakeonlan import wol

        self._hass = hass
        self._name = name
        self._host = host
        self._mac_address = mac_address
        self._off_script = Script(hass, off_action) if off_action else None
        self._state = False
        self._wol = wol
        self.update()

    @property
    def should_poll(self):
        """Poll for status regularly."""
        return True

    @property
    def is_on(self):
        """Return true if switch is on."""
        return self._state

    @property
    def name(self):
        """The name of the switch."""
        return self._name

    def turn_on(self):
        """Turn the device on."""
        self._wol.send_magic_packet(self._mac_address)

    def turn_off(self):
        """Turn the device off if an off action is present."""
        if self._off_script is not None:
            self._off_script.run()

    def update(self):
        """Check if device is on and update the state."""
        if platform.system().lower() == "windows":
            ping_cmd = "ping -n 1 -w {} {}".format(DEFAULT_PING_TIMEOUT * 1000, self._host)
        else:
            ping_cmd = "ping -c 1 -W {} {}".format(DEFAULT_PING_TIMEOUT, self._host)

        status = sp.getstatusoutput(ping_cmd)[0]
        self._state = not bool(status)
开发者ID:dmeulen,项目名称:home-assistant,代码行数:51,代码来源:wake_on_lan.py

示例3: ScriptEntity

# 需要导入模块: from homeassistant.helpers.script import Script [as 别名]
# 或者: from homeassistant.helpers.script.Script import run [as 别名]
class ScriptEntity(ToggleEntity):
    """Representation of a script entity."""

    # pylint: disable=too-many-instance-attributes
    def __init__(self, hass, object_id, name, sequence):
        """Initialize the script."""
        self.entity_id = ENTITY_ID_FORMAT.format(object_id)
        self.script = Script(hass, sequence, name, self.update_ha_state)

    @property
    def should_poll(self):
        """No polling needed."""
        return False

    @property
    def name(self):
        """Return the name of the entity."""
        return self.script.name

    @property
    def state_attributes(self):
        """Return the state attributes."""
        attrs = {}
        if self.script.can_cancel:
            attrs[ATTR_CAN_CANCEL] = self.script.can_cancel
        if self.script.last_action:
            attrs[ATTR_LAST_ACTION] = self.script.last_action
        return attrs

    @property
    def is_on(self):
        """Return true if script is on."""
        return self.script.is_running

    def turn_on(self, **kwargs):
        """Turn the entity on."""
        self.script.run(kwargs.get(ATTR_VARIABLES))

    def turn_off(self, **kwargs):
        """Turn script off."""
        self.script.stop()
开发者ID:12-hak,项目名称:hak-assistant,代码行数:43,代码来源:script.py

示例4: SwitchTemplate

# 需要导入模块: from homeassistant.helpers.script import Script [as 别名]
# 或者: from homeassistant.helpers.script.Script import run [as 别名]
class SwitchTemplate(SwitchDevice):
    """Representation of a Template switch."""

    # pylint: disable=too-many-arguments
    def __init__(self, hass, device_id, friendly_name, state_template,
                 on_action, off_action, entity_ids):
        """Initialize the Template switch."""
        self.hass = hass
        self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id,
                                            hass=hass)
        self._name = friendly_name
        self._template = state_template
        self._on_script = Script(hass, on_action)
        self._off_script = Script(hass, off_action)
        self._state = False

        self.update()

        def template_switch_state_listener(entity, old_state, new_state):
            """Called when the target device changes state."""
            self.update_ha_state(True)

        track_state_change(hass, entity_ids, template_switch_state_listener)

    @property
    def name(self):
        """Return the name of the switch."""
        return self._name

    @property
    def is_on(self):
        """Return true if device is on."""
        return self._state

    @property
    def should_poll(self):
        """No polling needed."""
        return False

    @property
    def available(self):
        """If switch is available."""
        return self._state is not None

    def turn_on(self, **kwargs):
        """Fire the on action."""
        self._on_script.run()

    def turn_off(self, **kwargs):
        """Fire the off action."""
        self._off_script.run()

    def update(self):
        """Update the state from the template."""
        try:
            state = template.render(self.hass, self._template).lower()

            if state in _VALID_STATES:
                self._state = state in ('true', STATE_ON)
            else:
                _LOGGER.error(
                    'Received invalid switch is_on state: %s. Expected: %s',
                    state, ', '.join(_VALID_STATES))
                self._state = None

        except TemplateError as ex:
            _LOGGER.error(ex)
            self._state = None
开发者ID:gazzer82,项目名称:home-assistant,代码行数:70,代码来源:template.py

示例5: SwitchTemplate

# 需要导入模块: from homeassistant.helpers.script import Script [as 别名]
# 或者: from homeassistant.helpers.script.Script import run [as 别名]
class SwitchTemplate(SwitchDevice):
    """Representation of a Template switch."""

    def __init__(self, hass, device_id, friendly_name, state_template,
                 icon_template, on_action, off_action, entity_ids):
        """Initialize the Template switch."""
        self.hass = hass
        self.entity_id = async_generate_entity_id(
            ENTITY_ID_FORMAT, device_id, hass=hass)
        self._name = friendly_name
        self._template = state_template
        self._on_script = Script(hass, on_action)
        self._off_script = Script(hass, off_action)
        self._state = False
        self._icon_template = icon_template
        self._icon = None
        self._entities = entity_ids

    @asyncio.coroutine
    def async_added_to_hass(self):
        """Register callbacks."""
        state = yield from async_get_last_state(self.hass, self.entity_id)
        if state:
            self._state = state.state == STATE_ON

        @callback
        def template_switch_state_listener(entity, old_state, new_state):
            """Handle target device state changes."""
            self.hass.async_add_job(self.async_update_ha_state(True))

        @callback
        def template_switch_startup(event):
            """Update template on startup."""
            async_track_state_change(
                self.hass, self._entities, template_switch_state_listener)

            self.hass.async_add_job(self.async_update_ha_state(True))

        self.hass.bus.async_listen_once(
            EVENT_HOMEASSISTANT_START, template_switch_startup)

    @property
    def name(self):
        """Return the name of the switch."""
        return self._name

    @property
    def is_on(self):
        """Return true if device is on."""
        return self._state

    @property
    def should_poll(self):
        """Return the polling state."""
        return False

    @property
    def available(self):
        """If switch is available."""
        return self._state is not None

    @property
    def icon(self):
        """Return the icon to use in the frontend, if any."""
        return self._icon

    def turn_on(self, **kwargs):
        """Fire the on action."""
        self._on_script.run()

    def turn_off(self, **kwargs):
        """Fire the off action."""
        self._off_script.run()

    @asyncio.coroutine
    def async_update(self):
        """Update the state from the template."""
        try:
            state = self._template.async_render().lower()

            if state in _VALID_STATES:
                self._state = state in ('true', STATE_ON)
            else:
                _LOGGER.error(
                    'Received invalid switch is_on state: %s. Expected: %s',
                    state, ', '.join(_VALID_STATES))
                self._state = None

        except TemplateError as ex:
            _LOGGER.error(ex)
            self._state = None

        if self._icon_template is not None:
            try:
                self._icon = self._icon_template.async_render()
            except TemplateError as ex:
                if ex.args and ex.args[0].startswith(
                        "UndefinedError: 'None' has no attribute"):
                    # Common during HA startup - so just a warning
                    _LOGGER.warning('Could not render icon template %s,'
#.........这里部分代码省略.........
开发者ID:Khabi,项目名称:home-assistant,代码行数:103,代码来源:template.py

示例6: SwitchTemplate

# 需要导入模块: from homeassistant.helpers.script import Script [as 别名]
# 或者: from homeassistant.helpers.script.Script import run [as 别名]
class SwitchTemplate(SwitchDevice):
    """Representation of a Template switch."""

    def __init__(self, hass, device_id, friendly_name, state_template,
                 on_action, off_action, entity_ids):
        """Initialize the Template switch."""
        self.hass = hass
        self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, device_id,
                                                  hass=hass)
        self._name = friendly_name
        self._template = state_template
        self._on_script = Script(hass, on_action)
        self._off_script = Script(hass, off_action)
        self._state = False
        self._entities = entity_ids

    @asyncio.coroutine
    def async_added_to_hass(self):
        """Register callbacks."""
        state = yield from async_get_last_state(self.hass, self.entity_id)
        if state:
            self._state = state.state == STATE_ON

        @callback
        def template_switch_state_listener(entity, old_state, new_state):
            """Called when the target device changes state."""
            self.hass.async_add_job(self.async_update_ha_state(True))

        @callback
        def template_switch_startup(event):
            """Update template on startup."""
            async_track_state_change(
                self.hass, self._entities, template_switch_state_listener)

            self.hass.async_add_job(self.async_update_ha_state(True))

        self.hass.bus.async_listen_once(
            EVENT_HOMEASSISTANT_START, template_switch_startup)

    @property
    def name(self):
        """Return the name of the switch."""
        return self._name

    @property
    def is_on(self):
        """Return true if device is on."""
        return self._state

    @property
    def should_poll(self):
        """No polling needed."""
        return False

    @property
    def available(self):
        """If switch is available."""
        return self._state is not None

    def turn_on(self, **kwargs):
        """Fire the on action."""
        self._on_script.run()

    def turn_off(self, **kwargs):
        """Fire the off action."""
        self._off_script.run()

    @asyncio.coroutine
    def async_update(self):
        """Update the state from the template."""
        try:
            state = self._template.async_render().lower()

            if state in _VALID_STATES:
                self._state = state in ('true', STATE_ON)
            else:
                _LOGGER.error(
                    'Received invalid switch is_on state: %s. Expected: %s',
                    state, ', '.join(_VALID_STATES))
                self._state = None

        except TemplateError as ex:
            _LOGGER.error(ex)
            self._state = None
开发者ID:azogue,项目名称:home-assistant,代码行数:86,代码来源:template.py


注:本文中的homeassistant.helpers.script.Script.run方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。