Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
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(
            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))
Ejemplo n.º 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]
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    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()
Ejemplo n.º 8
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()
Ejemplo n.º 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]
Ejemplo n.º 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]
Ejemplo n.º 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()
Ejemplo n.º 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]
Ejemplo n.º 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
            ]
Ejemplo n.º 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)
Ejemplo n.º 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]
Ejemplo n.º 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]
Ejemplo n.º 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]
Ejemplo n.º 18
0
    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)
Ejemplo n.º 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))
Ejemplo n.º 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
        ]
Ejemplo n.º 21
0
 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)
Ejemplo n.º 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)
Ejemplo n.º 23
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)
Ejemplo n.º 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()
Ejemplo n.º 25
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_UNLATCH:
             lock.unlatch()
Ejemplo n.º 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()
Ejemplo n.º 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
Ejemplo n.º 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]
Ejemplo n.º 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]
Ejemplo n.º 30
0
    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)
Ejemplo n.º 31
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
Ejemplo n.º 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