コード例 #1
0
ファイル: __init__.py プロジェクト: 12-hak/hak-assistant
    def handle_turn_service(service):
        """Method to handle calls to homeassistant.turn_on/off."""
        entity_ids = extract_entity_ids(hass, service)

        # Generic turn on/off method requires entity id
        if not entity_ids:
            _LOGGER.error(
                "homeassistant/%s cannot be called without entity_id",
                service.service)
            return

        # Group entity_ids by domain. groupby requires sorted data.
        by_domain = it.groupby(sorted(entity_ids),
                               lambda item: split_entity_id(item)[0])

        for domain, ent_ids in by_domain:
            # We want to block for all calls and only return when all calls
            # have been processed. If a service does not exist it causes a 10
            # second delay while we're blocking waiting for a response.
            # But services can be registered on other HA instances that are
            # listening to the bus too. So as a in between solution, we'll
            # block only if the service is defined in the current HA instance.
            blocking = hass.services.has_service(domain, service.service)

            # Create a new dict for this call
            data = dict(service.data)

            # ent_ids is a generator, convert it to a list.
            data[ATTR_ENTITY_ID] = list(ent_ids)

            hass.services.call(domain, service.service, data, blocking)
コード例 #2
0
ファイル: myhome.py プロジェクト: darookee/hass-config
 def update_room_state(state, service):
     entity_ids = extract_entity_ids(hass, service)
     for entity_id in entity_ids:
         room = myhome.get_room(entity_id)
         if room is None:
             _LOGGER.warning('Room %s not found', entity_id)
             return
         room.state = state
コード例 #3
0
ファイル: lifx.py プロジェクト: JiShangShiDai/home-assistant
    def service_to_entities(self, service):
        """Return the known devices that a service call mentions."""
        entity_ids = extract_entity_ids(self.hass, service)
        if entity_ids:
            entities = [entity for entity in self.entities.values()
                        if entity.entity_id in entity_ids]
        else:
            entities = list(self.entities.values())

        return entities
コード例 #4
0
ファイル: test_service.py プロジェクト: 12-hak/hak-assistant
    def test_extract_entity_ids(self):
        """Test extract_entity_ids method."""
        self.hass.states.set('light.Bowl', STATE_ON)
        self.hass.states.set('light.Ceiling', STATE_OFF)
        self.hass.states.set('light.Kitchen', STATE_OFF)

        loader.get_component('group').Group(
            self.hass, 'test', ['light.Ceiling', 'light.Kitchen'])

        call = ha.ServiceCall('light', 'turn_on',
                              {ATTR_ENTITY_ID: 'light.Bowl'})

        self.assertEqual(['light.bowl'],
                         service.extract_entity_ids(self.hass, call))

        call = ha.ServiceCall('light', 'turn_on',
                              {ATTR_ENTITY_ID: 'group.test'})

        self.assertEqual(['light.ceiling', 'light.kitchen'],
                         service.extract_entity_ids(self.hass, call))
コード例 #5
0
    def extract_from_service(self, service):
        """
        Takes a service and extracts all known entities.
        Will return all if no entity IDs given in service.
        """
        with self.lock:
            if ATTR_ENTITY_ID not in service.data:
                return list(self.entities.values())

            return [self.entities[entity_id] for entity_id
                    in extract_entity_ids(self.hass, service)
                    if entity_id in self.entities]
コード例 #6
0
ファイル: effects.py プロジェクト: compvter/home-assistant
    def async_service_handle(service):
        """Apply a service."""
        entity_ids = extract_entity_ids(hass, service)
        if entity_ids:
            devices = [entity for entity in lifx_manager.entities.values()
                       if entity.entity_id in entity_ids]
        else:
            devices = list(lifx_manager.entities.values())

        if devices:
            yield from start_effect(hass, devices,
                                    service.service, **service.data)
コード例 #7
0
ファイル: alert.py プロジェクト: ozzpy/home-assistant
    def async_handle_alert_service(service_call):
        """Handle calls to alert services."""
        alert_ids = service.extract_entity_ids(hass, service_call)

        for alert_id in alert_ids:
            alert = all_alerts[alert_id]
            if service_call.service == SERVICE_TURN_ON:
                yield from alert.async_turn_on()
            elif service_call.service == SERVICE_TOGGLE:
                yield from alert.async_toggle()
            else:
                yield from alert.async_turn_off()
コード例 #8
0
ファイル: alert.py プロジェクト: tmcarr/home-assistant
    async def async_handle_alert_service(service_call):
        """Handle calls to alert services."""
        alert_ids = service.extract_entity_ids(hass, service_call)

        for alert_id in alert_ids:
            alert = all_alerts[alert_id]
            alert.async_set_context(service_call.context)
            if service_call.service == SERVICE_TURN_ON:
                await alert.async_turn_on()
            elif service_call.service == SERVICE_TOGGLE:
                await alert.async_toggle()
            else:
                await alert.async_turn_off()
コード例 #9
0
 async def async_extract_from_service(service):
     entity_ids = service.data.get(ATTR_ENTITY_ID)
     if entity_ids == ENTITY_MATCH_ALL:
         return [entity for entity in hass.data.get(DATA_AMCREST_CAMS, [])
                 if entity.available]
     try:
         # async_extract_entity_ids introduced in 0.90.0
         entity_ids = await async_extract_entity_ids(hass, service)
     except NameError:
         # Before 0.90.0 extract_entity_ids was async_friendly
         entity_ids = extract_entity_ids(hass, service)
     return [entity for entity in hass.data.get(DATA_AMCREST_CAMS, [])
             if entity.available and entity.entity_id in entity_ids]
コード例 #10
0
    def extract_from_service(self, service):
        """Extract all known entities from a service call.

        Will return all entities if no entities specified in call.
        Will return an empty list if entities specified but unknown.
        """
        with self.lock:
            if ATTR_ENTITY_ID not in service.data:
                return list(self.entities.values())

            return [self.entities[entity_id] for entity_id
                    in extract_entity_ids(self.hass, service)
                    if entity_id in self.entities]
コード例 #11
0
    async def async_handle_alert_service(service_call):
        """Handle calls to alert services."""
        alert_ids = service.extract_entity_ids(hass, service_call)

        for alert_id in alert_ids:
            alert = all_alerts[alert_id]
            alert.async_set_context(service_call.context)
            if service_call.service == SERVICE_TURN_ON:
                await alert.async_turn_on()
            elif service_call.service == SERVICE_TOGGLE:
                await alert.async_toggle()
            else:
                await alert.async_turn_off()
コード例 #12
0
    def extract_from_service(self, service):
        """Extract all known entities from a service call.

        Will return all entities if no entities specified in call.
        Will return an empty list if entities specified but unknown.
        """
        with self.lock:
            if ATTR_ENTITY_ID not in service.data:
                return list(self.entities.values())

            return [self.entities[entity_id] for entity_id
                    in extract_entity_ids(self.hass, service)
                    if entity_id in self.entities]
コード例 #13
0
    def extract_from_service(self, service):
        """
        Takes a service and extracts all known entities.
        Will return all if no entity IDs given in service.
        """
        with self.lock:
            if ATTR_ENTITY_ID not in service.data:
                return list(self.entities.values())

            return [
                self.entities[entity_id]
                for entity_id in extract_entity_ids(self.hass, service)
                if entity_id in self.entities
            ]
コード例 #14
0
    def test_extract_entity_ids(self):
        """Test extract_entity_ids method."""
        self.hass.states.set('light.Bowl', STATE_ON)
        self.hass.states.set('light.Ceiling', STATE_OFF)
        self.hass.states.set('light.Kitchen', STATE_OFF)

        loader.get_component(self.hass, 'group').Group.create_group(
            self.hass, 'test', ['light.Ceiling', 'light.Kitchen'])

        call = ha.ServiceCall('light', 'turn_on',
                              {ATTR_ENTITY_ID: 'light.Bowl'})

        assert ['light.bowl'] == \
            service.extract_entity_ids(self.hass, call)

        call = ha.ServiceCall('light', 'turn_on',
                              {ATTR_ENTITY_ID: 'group.test'})

        assert ['light.ceiling', 'light.kitchen'] == \
            service.extract_entity_ids(self.hass, call)

        assert ['group.test'] == service.extract_entity_ids(
            self.hass, call, expand_group=False)
コード例 #15
0
    def async_extract_from_service(self, service):
        """Extract all known entities from a service call.

        Will return all entities if no entities specified in call.
        Will return an empty list if entities specified but unknown.

        This method must be run in the event loop.
        """
        if ATTR_ENTITY_ID not in service.data:
            return list(self.entities.values())

        return [self.entities[entity_id] for entity_id
                in extract_entity_ids(self.hass, service)
                if entity_id in self.entities]
コード例 #16
0
    def async_extract_from_service(self, service, expand_group=True):
        """Extract all known and available entities from a service call.

        Will return all entities if no entities specified in call.
        Will return an empty list if entities specified but unknown.

        This method must be run in the event loop.
        """
        if ATTR_ENTITY_ID not in service.data:
            return [entity for entity in self.entities if entity.available]

        entity_ids = set(extract_entity_ids(self.hass, service, expand_group))
        return [entity for entity in self.entities
                if entity.available and entity.entity_id in entity_ids]
コード例 #17
0
    def async_extract_from_service(self, service, expand_group=True):
        """Extract all known and available entities from a service call.

        Will return all entities if no entities specified in call.
        Will return an empty list if entities specified but unknown.

        This method must be run in the event loop.
        """
        if ATTR_ENTITY_ID not in service.data:
            return [entity for entity in self.entities if entity.available]

        entity_ids = set(extract_entity_ids(self.hass, service, expand_group))
        return [entity for entity in self.entities
                if entity.available and entity.entity_id in entity_ids]
コード例 #18
0
ファイル: effects.py プロジェクト: zircop/home-assistant
    def async_service_handle(service):
        """Internal func for applying a service."""
        entity_ids = extract_entity_ids(hass, service)
        if entity_ids:
            devices = [
                entity for entity in lifx_manager.entities.values()
                if entity.entity_id in entity_ids
            ]
        else:
            devices = list(lifx_manager.entities.values())

        if devices:
            yield from start_effect(hass, devices, service.service,
                                    **service.data)
コード例 #19
0
    def test_extract_entity_ids(self):
        """Test extract_entity_ids method."""
        self.hass.states.set('light.Bowl', STATE_ON)
        self.hass.states.set('light.Ceiling', STATE_OFF)
        self.hass.states.set('light.Kitchen', STATE_OFF)

        loader.get_component('group').Group.create_group(
            self.hass, 'test', ['light.Ceiling', 'light.Kitchen'])

        call = ha.ServiceCall('light', 'turn_on',
                              {ATTR_ENTITY_ID: 'light.Bowl'})

        self.assertEqual(['light.bowl'],
                         service.extract_entity_ids(self.hass, call))

        call = ha.ServiceCall('light', 'turn_on',
                              {ATTR_ENTITY_ID: 'group.test'})

        self.assertEqual(['light.ceiling', 'light.kitchen'],
                         service.extract_entity_ids(self.hass, call))

        self.assertEqual(['group.test'], service.extract_entity_ids(
            self.hass, call, expand_group=False))
コード例 #20
0
    def async_extract_from_service(self, service, expand_group=True):
        """Extract all known entities from a service call.

        Will return all entities if no entities specified in call.
        Will return an empty list if entities specified but unknown.

        This method must be run in the event loop.
        """
        if ATTR_ENTITY_ID not in service.data:
            return list(self.entities.values())

        return [
            self.entities[entity_id] for entity_id in extract_entity_ids(
                self.hass, service, expand_group) if entity_id in self.entities
        ]
コード例 #21
0
ファイル: onvif.py プロジェクト: MoshonkaKita/Golovastik
 def handle_ptz(service):
     """Handle PTZ service call."""
     pan = service.data.get(ATTR_PAN, None)
     tilt = service.data.get(ATTR_TILT, None)
     zoom = service.data.get(ATTR_ZOOM, None)
     all_cameras = hass.data[ONVIF_DATA][ENTITIES]
     entity_ids = extract_entity_ids(hass, service)
     target_cameras = []
     if not entity_ids:
         target_cameras = all_cameras
     else:
         target_cameras = [camera for camera in all_cameras
                           if camera.entity_id in entity_ids]
     for camera in target_cameras:
         camera.perform_ptz(pan, tilt, zoom)
コード例 #22
0
    def service_handler(service):
        """Dispatch service calls to target entities."""
        params = {key: value for key, value in service.data.items()
                  if key != ATTR_ENTITY_ID}

        entity_ids = extract_entity_ids(hass, service)
        target_devices = [light for light in hass.data[data_key]
                          if light.entity_id in entity_ids]

        for target_device in target_devices:
            if service.service == SERVICE_SET_MODE:
                target_device.set_mode(**params)
            elif service.service == SERVICE_START_FLOW:
                params[ATTR_TRANSITIONS] = \
                    _transitions_config_parser(params[ATTR_TRANSITIONS])
                target_device.start_flow(**params)
コード例 #23
0
ファイル: light.py プロジェクト: iantrich/home-assistant
    def service_handler(service):
        """Dispatch service calls to target entities."""
        params = {key: value for key, value in service.data.items()
                  if key != ATTR_ENTITY_ID}

        entity_ids = extract_entity_ids(hass, service)
        target_devices = [light for light in hass.data[data_key]
                          if light.entity_id in entity_ids]

        for target_device in target_devices:
            if service.service == SERVICE_SET_MODE:
                target_device.set_mode(**params)
            elif service.service == SERVICE_START_FLOW:
                params[ATTR_TRANSITIONS] = \
                    _transitions_config_parser(params[ATTR_TRANSITIONS])
                target_device.start_flow(**params)
コード例 #24
0
    def reload_images_service(service):
        """Handle reload images service call."""
        _LOGGER.debug("Reloading images with service call.")

        all_cameras = hass.data[MULTISOURCE_DATA][ENTITIES]
        entity_ids = extract_entity_ids(hass, service)
        target_cameras = []
        if not entity_ids:
            target_cameras = all_cameras
        else:
            target_cameras = [
                camera for camera in all_cameras
                if camera.entity_id in entity_ids
            ]
        for camera in target_cameras:
            camera.reload_images()
コード例 #25
0
ファイル: nuki.py プロジェクト: DavidMStraub/home-assistant
 def service_handler(service):
     """Service handler for nuki services."""
     entity_ids = extract_entity_ids(hass, service)
     all_locks = hass.data[NUKI_DATA][DOMAIN]
     target_locks = []
     if not entity_ids:
         target_locks = all_locks
     else:
         for lock in all_locks:
             if lock.entity_id in entity_ids:
                 target_locks.append(lock)
     for lock in target_locks:
         if service.service == SERVICE_LOCK_N_GO:
             unlatch = service.data[ATTR_UNLATCH]
             lock.lock_n_go(unlatch=unlatch)
         elif service.service == SERVICE_UNLATCH:
             lock.unlatch()
コード例 #26
0
 def service_handler(service):
     """Service handler for nuki services."""
     entity_ids = extract_entity_ids(hass, service)
     all_locks = hass.data[NUKI_DATA][DOMAIN]
     target_locks = []
     if not entity_ids:
         target_locks = all_locks
     else:
         for lock in all_locks:
             if lock.entity_id in entity_ids:
                 target_locks.append(lock)
     for lock in target_locks:
         if service.service == SERVICE_LOCK_N_GO:
             unlatch = service.data[ATTR_UNLATCH]
             lock.lock_n_go(unlatch=unlatch)
         elif service.service == SERVICE_CHECK_CONNECTION:
             lock.check_connection()
コード例 #27
0
    def service_to_entities(call):
        """Return the known devices that a service call mentions."""
        entity_ids = extract_entity_ids(hass, call)
        if entity_ids:
            devices = []
            for account, account_dict in (hass.data[DATA_ALEXAMEDIA]
                                          ['accounts'].items()):
                devices = devices + list(account_dict
                                         ['entities']['media_player'].values())
                _LOGGER.debug("Account: %s Devices: %s",
                              hide_email(account),
                              devices)
            entities = [entity for entity in devices
                        if entity.entity_id in entity_ids]
        else:
            entities = None

        return entities
コード例 #28
0
    def async_extract_from_service(self, service, expand_group=True):
        """Extract all known and available entities from a service call.

        Will return all entities if no entities specified in call.
        Will return an empty list if entities specified but unknown.

        This method must be run in the event loop.
        """
        data_ent_id = service.data.get(ATTR_ENTITY_ID)

        if data_ent_id in (None, MATCH_ALL):
            if data_ent_id is None:
                self.logger.warning(
                    'Not passing an entity ID to a service to target all '
                    'entities is deprecated. Update your call to %s.%s to be '
                    'instead: entity_id: "*"', service.domain, service.service)

            return [entity for entity in self.entities if entity.available]

        entity_ids = set(extract_entity_ids(self.hass, service, expand_group))
        return [entity for entity in self.entities
                if entity.available and entity.entity_id in entity_ids]
コード例 #29
0
    def async_extract_from_service(self, service, expand_group=True):
        """Extract all known and available entities from a service call.

        Will return all entities if no entities specified in call.
        Will return an empty list if entities specified but unknown.

        This method must be run in the event loop.
        """
        data_ent_id = service.data.get(ATTR_ENTITY_ID)

        if data_ent_id in (None, ENTITY_MATCH_ALL):
            if data_ent_id is None:
                self.logger.warning(
                    'Not passing an entity ID to a service to target all '
                    'entities is deprecated. Update your call to %s.%s to be '
                    'instead: entity_id: %s', service.domain, service.service,
                    ENTITY_MATCH_ALL)

            return [entity for entity in self.entities if entity.available]

        entity_ids = set(extract_entity_ids(self.hass, service, expand_group))
        return [entity for entity in self.entities
                if entity.available and entity.entity_id in entity_ids]
コード例 #30
0
ファイル: __init__.py プロジェクト: sara0871/-.gitignore-
    def async_handle_turn_service(service):
        """Handle calls to homeassistant.turn_on/off."""
        entity_ids = extract_entity_ids(hass, service)

        # Generic turn on/off method requires entity id
        if not entity_ids:
            _LOGGER.error(
                "homeassistant/%s cannot be called without entity_id",
                service.service)
            return

        # Group entity_ids by domain. groupby requires sorted data.
        by_domain = it.groupby(sorted(entity_ids),
                               lambda item: ha.split_entity_id(item)[0])

        tasks = []

        for domain, ent_ids in by_domain:
            # We want to block for all calls and only return when all calls
            # have been processed. If a service does not exist it causes a 10
            # second delay while we're blocking waiting for a response.
            # But services can be registered on other HA instances that are
            # listening to the bus too. So as an in between solution, we'll
            # block only if the service is defined in the current HA instance.
            blocking = hass.services.has_service(domain, service.service)

            # Create a new dict for this call
            data = dict(service.data)

            # ent_ids is a generator, convert it to a list.
            data[ATTR_ENTITY_ID] = list(ent_ids)

            tasks.append(
                hass.services.async_call(domain, service.service, data,
                                         blocking))

        yield from asyncio.wait(tasks, loop=hass.loop)
コード例 #31
0
ファイル: vacuum.py プロジェクト: pp81381/home-assistant
 def service_to_entities(call):
     """Return the known devices that a service call mentions."""
     entity_ids = extract_entity_ids(hass, call)
     entities = [entity for entity in dev if entity.entity_id in entity_ids]
     return entities
コード例 #32
0
 def service_to_entities(call):
     """Return the known devices that a service call mentions."""
     entity_ids = extract_entity_ids(hass, call)
     entities = [entity for entity in dev
                 if entity.entity_id in entity_ids]
     return entities