Python homeassistant.const.ATTR_ENTITY_ID Examples

The following are 2 code examples of homeassistant.const.ATTR_ENTITY_ID(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module homeassistant.const , or try the search function .
Example #1
Source File: bind.py    From havcs with Apache License 2.0 5 votes vote down vote up
def sync_device(self):
        remove_listener = self._sync_manager.get('remove_listener')
        if remove_listener:
            remove_listener()
    
        @callback
        def report_device(event):
            # _LOGGER.debug("[skill] %s changed, try to report", event.data[ATTR_ENTITY_ID])
            self._hass.add_job(async_report_device(event))

        async def async_report_device(event):
            """report device state when changed. """
            entity = self._hass.states.get(event.data[ATTR_ENTITY_ID])
            if entity is None:
                return
            for platform, handler in self._hass.data[INTEGRATION][DATA_HAVCS_HANDLER].items():
                if hasattr(handler, 'report_device'):
                    device_ids = handler.vcdm.get_entity_related_device_ids(self._hass, entity.entity_id)
                    for device_id in device_ids:
                        payload = handler.report_device(device_id)
                        _LOGGER.debug("[skill] report device to %s: platform = %s, device_id = %s (entity_id = %s), data = %s", platform, device_id, event.data[ATTR_ENTITY_ID], platform, payload)
                        if payload:
                            url = HAVCS_SERVICE_URL + '/skill/'+platform+'.php?v=report&AppKey=' + self._sync_manager.get('app_key')
                            data = havcs_util.AESCipher(self._sync_manager.get('decrypt_key')).encrypt(json.dumps(payload, ensure_ascii = False).encode('utf8'))
                            try:
                                session = async_get_clientsession(self._hass, verify_ssl=False)
                                with async_timeout.timeout(5, loop=self._hass.loop):
                                    response = await session.post(url, data=data)
                                    _LOGGER.debug("[skill] get report device result from %s: %s", platform, await response.text())
                            except(asyncio.TimeoutError, aiohttp.ClientError):
                                _LOGGER.error("[skill] fail to access %s, report device fail: timeout", url)
                            except:
                                _LOGGER.error("[skill] fail to access %s, report device fail: %s", url, traceback.format_exc())
        
        self._sync_manager['remove_listener'] = self._hass.bus.async_listen(EVENT_STATE_CHANGED, report_device) 
Example #2
Source File: switch.py    From wiserHomeAssistantPlatform with MIT License 4 votes vote down vote up
def async_setup_entry(hass, config_entry, async_add_entities):
    """Add the Wiser System Switch entities."""
    data = hass.data[DOMAIN][config_entry.entry_id][DATA]  # Get Handler

    # Add System Switches
    wiser_switches = []
    for switch in WISER_SWITCHES:
        wiser_switches.append(
            WiserSwitch(data, switch["name"], switch["key"], switch["icon"])
        )

    async_add_entities(wiser_switches)

    # Add SmartPlugs (if any)
    if data.wiserhub.getSmartPlugs() is not None:
        wiser_smart_plugs = [
            WiserSmartPlug(data, plug.get("id"), "Wiser {}".format(plug.get("Name")))
            for plug in data.wiserhub.getSmartPlugs()
        ]
        async_add_entities(wiser_smart_plugs)

    @callback
    def set_smartplug_mode(service):
        entity_id = service.data[ATTR_ENTITY_ID]
        smart_plug_mode = service.data[ATTR_PLUG_MODE]

        _LOGGER.debug(
            "Set Smartplub called - entity %s mode %s ", entity_id, smart_plug_mode,
        )

        for smart_plug in wiser_smart_plugs:

            if smart_plug.entity_id == entity_id:
                hass.async_create_task(smart_plug.set_smartplug_mode(smart_plug_mode))
            smart_plug.schedule_update_ha_state(True)
            break

    @callback
    def set_hotwater_mode(service):
        hotwater_mode = service.data[ATTR_HOTWATER_MODE]
        hass.async_create_task(data.set_hotwater_mode(hotwater_mode))

    # Register Services
    hass.services.async_register(
        DOMAIN,
        WISER_SERVICES["SERVICE_SET_SMARTPLUG_MODE"],
        set_smartplug_mode,
        schema=SET_PLUG_MODE_SCHEMA,
    )
    hass.services.async_register(
        DOMAIN,
        WISER_SERVICES["SERVICE_SET_HOTWATER_MODE"],
        set_hotwater_mode,
        schema=SET_HOTWATER_MODE_SCHEMA,
    )
    return True