Exemple #1
0
def async_setup(hass, config):
    """Track states and offer events for binary sensors."""
    component = EntityComponent(
        logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL)

    yield from component.async_setup(config)
    return True
def async_setup(hass, config: dict):
    """Expose fan control via statemachine and services."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_FANS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_fan_service(service):
        """Handle service call for fans."""
        method = SERVICE_TO_METHOD.get(service.service)
        params = service.data.copy()

        # Convert the entity ids to valid fan ids
        target_fans = component.async_extract_from_service(service)
        params.pop(ATTR_ENTITY_ID, None)

        update_tasks = []
        for fan in target_fans:
            yield from getattr(fan, method['method'])(**params)
            if not fan.should_poll:
                continue
            update_tasks.append(fan.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    for service_name in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service_name].get('schema')
        hass.services.async_register(
            DOMAIN, service_name, async_handle_fan_service, schema=schema)

    return True
Exemple #3
0
def async_setup(hass, config):
    """Setup the list component."""
    component = EntityComponent(_LOGGER, DOMAIN, hass)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_list_service(service):
        target_lists = component.async_extract_from_service(service)
        method = SERVICE_TO_METHOD.get(service.service)
        params = service.data.copy()

        for list in target_lists:
            yield from getattr(list, method['method'])(**params)

        for list in target_lists:
            yield from hass.loop.create_task(list.async_update_ha_state(True))

    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    for service_name in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service_name].get('schema',
                                                     LIST_SERVICE_SCHEMA)
        hass.services.async_register(DOMAIN,
                                     service_name,
                                     async_handle_list_service,
                                     descriptions.get(service_name),
                                     schema=schema)

    return True
def async_setup(hass, config):
    """Set up the vacuum component."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_VACUUMS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_vacuum_service(service):
        """Map services to methods on VacuumDevice."""
        method = SERVICE_TO_METHOD.get(service.service)
        target_vacuums = component.async_extract_from_service(service)
        params = service.data.copy()
        params.pop(ATTR_ENTITY_ID, None)

        update_tasks = []
        for vacuum in target_vacuums:
            yield from getattr(vacuum, method['method'])(**params)
            if not vacuum.should_poll:
                continue
            update_tasks.append(vacuum.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    for service in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service].get(
            'schema', VACUUM_SERVICE_SCHEMA)
        hass.services.async_register(
            DOMAIN, service, async_handle_vacuum_service,
            schema=schema)

    return True
def test_pararell_updates_async_platform_with_constant(hass):
    """Warn we log when platform setup takes a long time."""
    platform = MockPlatform()

    @asyncio.coroutine
    def mock_update(*args, **kwargs):
        pass

    platform.async_setup_platform = mock_update
    platform.PARALLEL_UPDATES = 1

    loader.set_component('test_domain.platform', platform)

    component = EntityComponent(_LOGGER, DOMAIN, hass)
    component._platforms = {}

    yield from component.async_setup({
        DOMAIN: {
            'platform': 'platform',
        }
    })

    handle = list(component._platforms.values())[-1]

    assert handle.parallel_updates is not None
def async_setup(hass, config):
    """Set up the vacuum component."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_VACUUMS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_vacuum_service(service):
        """Map services to methods on VacuumDevice."""
        method = SERVICE_TO_METHOD.get(service.service)
        target_vacuums = component.async_extract_from_service(service)
        params = service.data.copy()
        params.pop(ATTR_ENTITY_ID, None)

        update_tasks = []
        for vacuum in target_vacuums:
            yield from getattr(vacuum, method['method'])(**params)
            if not vacuum.should_poll:
                continue
            update_tasks.append(vacuum.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    for service in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service].get(
            'schema', VACUUM_SERVICE_SCHEMA)
        hass.services.async_register(
            DOMAIN, service, async_handle_vacuum_service,
            schema=schema)

    return True
def async_setup(hass, config):
    """Set up image processing."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)

    yield from component.async_setup(config)

    descriptions = yield from hass.async_add_job(
        load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    @asyncio.coroutine
    def async_scan_service(service):
        """Service handler for scan."""
        image_entities = component.async_extract_from_service(service)

        update_task = [entity.async_update_ha_state(True) for
                       entity in image_entities]
        if update_task:
            yield from asyncio.wait(update_task, loop=hass.loop)

    hass.services.async_register(
        DOMAIN, SERVICE_SCAN, async_scan_service,
        descriptions.get(SERVICE_SCAN), schema=SERVICE_SCAN_SCHEMA)

    return True
def async_setup(hass, config):
    """Track states and offer events for calendars."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, DOMAIN)

    yield from component.async_setup(config)
    return True
Exemple #9
0
def async_setup(hass, config):
    """Track states and offer events for covers."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_COVERS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_cover_service(service):
        """Handle calls to the cover services."""
        covers = component.async_extract_from_service(service)
        method = SERVICE_TO_METHOD.get(service.service)
        params = service.data.copy()
        params.pop(ATTR_ENTITY_ID, None)

        # call method
        update_tasks = []
        for cover in covers:
            yield from getattr(cover, method['method'])(**params)
            if not cover.should_poll:
                continue
            update_tasks.append(cover.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    for service_name in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service_name].get('schema',
                                                     COVER_SERVICE_SCHEMA)
        hass.services.async_register(DOMAIN,
                                     service_name,
                                     async_handle_cover_service,
                                     schema=schema)

    return True
def async_setup(hass, config):
    """Track states and offer events for sensors."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL)

    yield from component.async_setup(config)
    return True
Exemple #11
0
def async_setup(hass, config: dict):
    """Expose fan control via statemachine and services."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_FANS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_fan_service(service):
        """Hande service call for fans."""
        method = SERVICE_TO_METHOD.get(service.service)
        params = service.data.copy()

        # Convert the entity ids to valid fan ids
        target_fans = component.async_extract_from_service(service)
        params.pop(ATTR_ENTITY_ID, None)

        update_tasks = []
        for fan in target_fans:
            yield from getattr(fan, method['method'])(**params)
            if not fan.should_poll:
                continue
            update_tasks.append(fan.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    for service_name in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service_name].get('schema')
        hass.services.async_register(DOMAIN,
                                     service_name,
                                     async_handle_fan_service,
                                     schema=schema)

    return True
def async_setup(hass, config):
    """Track states and offer events for covers."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_COVERS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_cover_service(service):
        """Handle calls to the cover services."""
        covers = component.async_extract_from_service(service)
        method = SERVICE_TO_METHOD.get(service.service)
        params = service.data.copy()
        params.pop(ATTR_ENTITY_ID, None)

        # call method
        update_tasks = []
        for cover in covers:
            yield from getattr(cover, method['method'])(**params)
            if not cover.should_poll:
                continue
            update_tasks.append(cover.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    for service_name in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service_name].get(
            'schema', COVER_SERVICE_SCHEMA)
        hass.services.async_register(
            DOMAIN, service_name, async_handle_cover_service,
            schema=schema)

    return True
Exemple #13
0
def async_setup(hass, config):
    """Set up image processing."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)

    yield from component.async_setup(config)

    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    @asyncio.coroutine
    def async_scan_service(service):
        """Service handler for scan."""
        image_entities = component.async_extract_from_service(service)

        update_task = [
            entity.async_update_ha_state(True) for entity in image_entities
        ]
        if update_task:
            yield from asyncio.wait(update_task, loop=hass.loop)

    hass.services.async_register(DOMAIN,
                                 SERVICE_SCAN,
                                 async_scan_service,
                                 descriptions.get(SERVICE_SCAN),
                                 schema=SERVICE_SCAN_SCHEMA)

    return True
Exemple #14
0
def test_setup_dependencies_platform(hass):
    """Test we setup the dependencies of a platform.

    We're explictely testing that we process dependencies even if a component
    with the same name has already been loaded.
    """
    loader.set_component(hass, 'test_component', MockModule('test_component'))
    loader.set_component(hass, 'test_component2',
                         MockModule('test_component2'))
    loader.set_component(
        hass, 'test_domain.test_component',
        MockPlatform(dependencies=['test_component', 'test_component2']))

    component = EntityComponent(_LOGGER, DOMAIN, hass)

    yield from async_setup_component(hass, 'test_component', {})

    yield from component.async_setup({
        DOMAIN: {
            'platform': 'test_component',
        }
    })

    assert 'test_component' in hass.config.components
    assert 'test_component2' in hass.config.components
    assert 'test_domain.test_component' in hass.config.components
def async_setup(hass, config):
    """Track states and offer events for sensors."""
    component = EntityComponent(
        logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_alarm_service_handler(service):
        """Map services to methods on Alarm."""
        target_alarms = component.async_extract_from_service(service)

        code = service.data.get(ATTR_CODE)

        method = "async_{}".format(SERVICE_TO_METHOD[service.service])

        update_tasks = []
        for alarm in target_alarms:
            yield from getattr(alarm, method)(code)

            if not alarm.should_poll:
                continue
            update_tasks.append(alarm.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    for service in SERVICE_TO_METHOD:
        hass.services.async_register(
            DOMAIN, service, async_alarm_service_handler,
            schema=ALARM_SERVICE_SCHEMA)

    return True
def async_setup(hass, config):
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_DISPLAYS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_display_service(service):
        method = SERVICE_TO_METHOD.get(service.service)
        target_displays = component.async_extract_from_service(service)
        params = service.data.copy()
        params.pop(ATTR_ENTITY_ID, None)

        update_tasks = []
        for display in target_displays:
            yield from getattr(display, method['method'])(**params)
            if not display.should_poll:
                continue
            update_tasks.append(display.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    for service in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service].get('schema',
                                                DISPLAY_SERVICE_SCHEMA)
        hass.services.async_register(DOMAIN,
                                     service,
                                     async_handle_display_service,
                                     schema=schema)

    return True
Exemple #17
0
def async_setup(hass, config: dict):
    """Expose fan control via statemachine and services."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_FANS)

    yield from component.async_setup(config)

    component.async_register_entity_service(SERVICE_TURN_ON,
                                            FAN_TURN_ON_SCHEMA,
                                            'async_turn_on')
    component.async_register_entity_service(SERVICE_TURN_OFF,
                                            FAN_TURN_OFF_SCHEMA,
                                            'async_turn_off')
    component.async_register_entity_service(SERVICE_TOGGLE, FAN_TOGGLE_SCHEMA,
                                            'async_toggle')
    component.async_register_entity_service(SERVICE_SET_SPEED,
                                            FAN_SET_SPEED_SCHEMA,
                                            'async_set_speed')
    component.async_register_entity_service(SERVICE_OSCILLATE,
                                            FAN_OSCILLATE_SCHEMA,
                                            'async_oscillate')
    component.async_register_entity_service(SERVICE_SET_DIRECTION,
                                            FAN_SET_DIRECTION_SCHEMA,
                                            'async_set_direction')

    return True
def async_setup(hass, config):
    """Track states and offer events for remotes."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_REMOTES)
    yield from component.async_setup(config)

    component.async_register_entity_service(
        SERVICE_TURN_OFF, REMOTE_SERVICE_ACTIVITY_SCHEMA,
        'async_turn_off'
    )

    component.async_register_entity_service(
        SERVICE_TURN_ON, REMOTE_SERVICE_ACTIVITY_SCHEMA,
        'async_turn_on'
    )

    component.async_register_entity_service(
        SERVICE_TOGGLE, REMOTE_SERVICE_ACTIVITY_SCHEMA,
        'async_toggle'
    )

    component.async_register_entity_service(
        SERVICE_SEND_COMMAND, REMOTE_SERVICE_SEND_COMMAND_SCHEMA,
        'async_send_command'
    )

    return True
def async_setup(hass, config):
    """Set up the camera component."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)

    hass.http.register_view(CameraImageView(component.entities))
    hass.http.register_view(CameraMjpegStream(component.entities))

    yield from component.async_setup(config)

    @callback
    def update_tokens(time):
        """Update tokens of the entities."""
        for entity in component.entities.values():
            entity.async_update_token()
            hass.async_add_job(entity.async_update_ha_state())

    async_track_time_interval(hass, update_tokens, TOKEN_CHANGE_INTERVAL)

    @asyncio.coroutine
    def async_handle_camera_service(service):
        """Handle calls to the camera services."""
        target_cameras = component.async_extract_from_service(service)

        for camera in target_cameras:
            if service.service == SERVICE_EN_MOTION:
                yield from camera.async_enable_motion_detection()
            elif service.service == SERVICE_DISEN_MOTION:
                yield from camera.async_disable_motion_detection()

        update_tasks = []
        for camera in target_cameras:
            if not camera.should_poll:
                continue

            update_coro = hass.async_add_job(
                camera.async_update_ha_state(True))
            if hasattr(camera, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.async_add_job(
        load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    hass.services.async_register(DOMAIN,
                                 SERVICE_EN_MOTION,
                                 async_handle_camera_service,
                                 descriptions.get(SERVICE_EN_MOTION),
                                 schema=CAMERA_SERVICE_SCHEMA)
    hass.services.async_register(DOMAIN,
                                 SERVICE_DISEN_MOTION,
                                 async_handle_camera_service,
                                 descriptions.get(SERVICE_DISEN_MOTION),
                                 schema=CAMERA_SERVICE_SCHEMA)

    return True
def test_pararell_updates_async_platform_with_constant(hass):
    """Warn we log when platform setup takes a long time."""
    platform = MockPlatform()

    @asyncio.coroutine
    def mock_update(*args, **kwargs):
        pass

    platform.async_setup_platform = mock_update
    platform.PARALLEL_UPDATES = 1

    loader.set_component('test_domain.platform', platform)

    component = EntityComponent(_LOGGER, DOMAIN, hass)
    component._platforms = {}

    yield from component.async_setup({
        DOMAIN: {
            'platform': 'platform',
        }
    })

    handle = list(component._platforms.values())[-1]

    assert handle.parallel_updates is not None
def async_setup(hass, config):
    """Track states and offer events for sensors."""
    component = EntityComponent(logging.getLogger(__name__), DOMAIN, hass,
                                SCAN_INTERVAL)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_alarm_service_handler(service):
        """Map services to methods on Alarm."""
        target_alarms = component.async_extract_from_service(service)

        code = service.data.get(ATTR_CODE)

        method = "async_{}".format(SERVICE_TO_METHOD[service.service])

        update_tasks = []
        for alarm in target_alarms:
            yield from getattr(alarm, method)(code)

            if not alarm.should_poll:
                continue
            update_tasks.append(alarm.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    for service in SERVICE_TO_METHOD:
        hass.services.async_register(DOMAIN,
                                     service,
                                     async_alarm_service_handler,
                                     schema=ALARM_SERVICE_SCHEMA)

    return True
def async_setup(hass, config: dict):
    """Expose fan control via statemachine and services."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_FANS)

    yield from component.async_setup(config)

    component.async_register_entity_service(
        SERVICE_TURN_ON, FAN_TURN_ON_SCHEMA,
        'async_turn_on'
    )
    component.async_register_entity_service(
        SERVICE_TURN_OFF, FAN_TURN_OFF_SCHEMA,
        'async_turn_off'
    )
    component.async_register_entity_service(
        SERVICE_TOGGLE, FAN_TOGGLE_SCHEMA,
        'async_toggle'
    )
    component.async_register_entity_service(
        SERVICE_SET_SPEED, FAN_SET_SPEED_SCHEMA,
        'async_set_speed'
    )
    component.async_register_entity_service(
        SERVICE_OSCILLATE, FAN_OSCILLATE_SCHEMA,
        'async_oscillate'
    )
    component.async_register_entity_service(
        SERVICE_SET_DIRECTION, FAN_SET_DIRECTION_SCHEMA,
        'async_set_direction'
    )

    return True
Exemple #23
0
def async_setup(hass, config):
    """Track states and offer events for remotes."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_REMOTES)
    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_remote_service(service):
        """Handle calls to the remote services."""
        target_remotes = component.async_extract_from_service(service)

        activity_id = service.data.get(ATTR_ACTIVITY)
        device = service.data.get(ATTR_DEVICE)
        command = service.data.get(ATTR_COMMAND)
        num_repeats = service.data.get(ATTR_NUM_REPEATS)
        delay_secs = service.data.get(ATTR_DELAY_SECS)

        for remote in target_remotes:
            if service.service == SERVICE_TURN_ON:
                yield from remote.async_turn_on(activity=activity_id)
            elif service.service == SERVICE_SEND_COMMAND:
                yield from remote.async_send_command(
                    device=device, command=command,
                    num_repeats=num_repeats, delay_secs=delay_secs)
            else:
                yield from remote.async_turn_off()

        update_tasks = []
        for remote in target_remotes:
            if not remote.should_poll:
                continue

            update_coro = hass.async_add_job(
                remote.async_update_ha_state(True))
            if hasattr(remote, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.async_add_job(
        load_yaml_config_file, os.path.join(
            os.path.dirname(__file__), 'services.yaml'))
    hass.services.async_register(
        DOMAIN, SERVICE_TURN_OFF, async_handle_remote_service,
        descriptions.get(SERVICE_TURN_OFF),
        schema=REMOTE_SERVICE_SCHEMA)
    hass.services.async_register(
        DOMAIN, SERVICE_TURN_ON, async_handle_remote_service,
        descriptions.get(SERVICE_TURN_ON),
        schema=REMOTE_SERVICE_TURN_ON_SCHEMA)
    hass.services.async_register(
        DOMAIN, SERVICE_SEND_COMMAND, async_handle_remote_service,
        descriptions.get(SERVICE_SEND_COMMAND),
        schema=REMOTE_SERVICE_SEND_COMMAND_SCHEMA)

    return True
def async_setup(hass, config):
    """Track states and offer events for remotes."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_REMOTES)
    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_remote_service(service):
        """Handle calls to the remote services."""
        target_remotes = component.async_extract_from_service(service)

        activity_id = service.data.get(ATTR_ACTIVITY)
        device = service.data.get(ATTR_DEVICE)
        command = service.data.get(ATTR_COMMAND)

        for remote in target_remotes:
            if service.service == SERVICE_TURN_ON:
                yield from remote.async_turn_on(activity=activity_id)
            elif service.service == SERVICE_SEND_COMMAND:
                yield from remote.async_send_command(device=device,
                                                     command=command)
            else:
                yield from remote.async_turn_off()

        update_tasks = []
        for remote in target_remotes:
            if not remote.should_poll:
                continue

            update_coro = hass.loop.create_task(
                remote.async_update_ha_state(True))
            if hasattr(remote, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))
    hass.services.async_register(DOMAIN,
                                 SERVICE_TURN_OFF,
                                 async_handle_remote_service,
                                 descriptions.get(SERVICE_TURN_OFF),
                                 schema=REMOTE_SERVICE_SCHEMA)
    hass.services.async_register(DOMAIN,
                                 SERVICE_TURN_ON,
                                 async_handle_remote_service,
                                 descriptions.get(SERVICE_TURN_ON),
                                 schema=REMOTE_SERVICE_TURN_ON_SCHEMA)
    hass.services.async_register(DOMAIN,
                                 SERVICE_SEND_COMMAND,
                                 async_handle_remote_service,
                                 descriptions.get(SERVICE_SEND_COMMAND),
                                 schema=REMOTE_SERVICE_SEND_COMMAND_SCHEMA)

    return True
Exemple #25
0
def async_setup(hass, config):
    """Track states and offer events for media_players."""
    component = EntityComponent(logging.getLogger(__name__), DOMAIN, hass,
                                SCAN_INTERVAL)

    hass.http.register_view(MediaPlayerImageView(component.entities))

    yield from component.async_setup(config)

    descriptions = yield from hass.async_add_job(
        load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    @asyncio.coroutine
    def async_service_handler(service):
        """Map services to methods on MediaPlayerDevice."""
        method = SERVICE_TO_METHOD.get(service.service)
        if not method:
            return

        params = {}
        if service.service == SERVICE_VOLUME_SET:
            params['volume'] = service.data.get(ATTR_MEDIA_VOLUME_LEVEL)
        elif service.service == SERVICE_VOLUME_MUTE:
            params['mute'] = service.data.get(ATTR_MEDIA_VOLUME_MUTED)
        elif service.service == SERVICE_MEDIA_SEEK:
            params['position'] = service.data.get(ATTR_MEDIA_SEEK_POSITION)
        elif service.service == SERVICE_SELECT_SOURCE:
            params['source'] = service.data.get(ATTR_INPUT_SOURCE)
        elif service.service == SERVICE_PLAY_MEDIA:
            params['media_type'] = \
                service.data.get(ATTR_MEDIA_CONTENT_TYPE)
            params['media_id'] = service.data.get(ATTR_MEDIA_CONTENT_ID)
            params[ATTR_MEDIA_ENQUEUE] = \
                service.data.get(ATTR_MEDIA_ENQUEUE)
        elif service.service == SERVICE_SHUFFLE_SET:
            params[ATTR_MEDIA_SHUFFLE] = \
                service.data.get(ATTR_MEDIA_SHUFFLE)
        target_players = component.async_extract_from_service(service)

        update_tasks = []
        for player in target_players:
            yield from getattr(player, method['method'])(**params)
            if not player.should_poll:
                continue
            update_tasks.append(player.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    for service in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service].get('schema', MEDIA_PLAYER_SCHEMA)
        hass.services.async_register(DOMAIN,
                                     service,
                                     async_service_handler,
                                     descriptions.get(service),
                                     schema=schema)

    return True
Exemple #26
0
def async_setup(hass, config):
    """Track states and offer events for media_players."""
    component = EntityComponent(
        logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL)

    hass.http.register_view(MediaPlayerImageView(component.entities))

    yield from component.async_setup(config)

    descriptions = yield from hass.async_add_job(
        load_yaml_config_file, os.path.join(
            os.path.dirname(__file__), 'services.yaml'))

    @asyncio.coroutine
    def async_service_handler(service):
        """Map services to methods on MediaPlayerDevice."""
        method = SERVICE_TO_METHOD.get(service.service)
        if not method:
            return

        params = {}
        if service.service == SERVICE_VOLUME_SET:
            params['volume'] = service.data.get(ATTR_MEDIA_VOLUME_LEVEL)
        elif service.service == SERVICE_VOLUME_MUTE:
            params['mute'] = service.data.get(ATTR_MEDIA_VOLUME_MUTED)
        elif service.service == SERVICE_MEDIA_SEEK:
            params['position'] = service.data.get(ATTR_MEDIA_SEEK_POSITION)
        elif service.service == SERVICE_SELECT_SOURCE:
            params['source'] = service.data.get(ATTR_INPUT_SOURCE)
        elif service.service == SERVICE_PLAY_MEDIA:
            params['media_type'] = \
                service.data.get(ATTR_MEDIA_CONTENT_TYPE)
            params['media_id'] = service.data.get(ATTR_MEDIA_CONTENT_ID)
            params[ATTR_MEDIA_ENQUEUE] = \
                service.data.get(ATTR_MEDIA_ENQUEUE)
        elif service.service == SERVICE_SHUFFLE_SET:
            params[ATTR_MEDIA_SHUFFLE] = \
                service.data.get(ATTR_MEDIA_SHUFFLE)
        target_players = component.async_extract_from_service(service)

        update_tasks = []
        for player in target_players:
            yield from getattr(player, method['method'])(**params)
            if not player.should_poll:
                continue
            update_tasks.append(player.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    for service in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service].get(
            'schema', MEDIA_PLAYER_SCHEMA)
        hass.services.async_register(
            DOMAIN, service, async_service_handler,
            descriptions.get(service), schema=schema)

    return True
Exemple #27
0
def async_setup(hass, config):
    """Setup the camera component."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)

    hass.http.register_view(CameraImageView(component.entities))
    hass.http.register_view(CameraMjpegStream(component.entities))

    yield from component.async_setup(config)
    return True
def async_setup(hass, config):
    """Set up the vacuum component."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_VACUUMS)

    yield from component.async_setup(config)

    component.async_register_entity_service(
        SERVICE_TURN_ON, VACUUM_SERVICE_SCHEMA,
        'async_turn_on'
    )
    component.async_register_entity_service(
        SERVICE_TURN_OFF, VACUUM_SERVICE_SCHEMA,
        'async_turn_off'
    )
    component.async_register_entity_service(
        SERVICE_TOGGLE, VACUUM_SERVICE_SCHEMA,
        'async_toggle'
    )
    component.async_register_entity_service(
        SERVICE_START_PAUSE, VACUUM_SERVICE_SCHEMA,
        'async_start_pause'
    )
    component.async_register_entity_service(
        SERVICE_START, VACUUM_SERVICE_SCHEMA,
        'async_start'
    )
    component.async_register_entity_service(
        SERVICE_PAUSE, VACUUM_SERVICE_SCHEMA,
        'async_pause'
    )
    component.async_register_entity_service(
        SERVICE_RETURN_TO_BASE, VACUUM_SERVICE_SCHEMA,
        'async_return_to_base'
    )
    component.async_register_entity_service(
        SERVICE_CLEAN_SPOT, VACUUM_SERVICE_SCHEMA,
        'async_clean_spot'
    )
    component.async_register_entity_service(
        SERVICE_LOCATE, VACUUM_SERVICE_SCHEMA,
        'async_locate'
    )
    component.async_register_entity_service(
        SERVICE_STOP, VACUUM_SERVICE_SCHEMA,
        'async_stop'
    )
    component.async_register_entity_service(
        SERVICE_SET_FAN_SPEED, VACUUM_SET_FAN_SPEED_SERVICE_SCHEMA,
        'async_set_fan_speed'
    )
    component.async_register_entity_service(
        SERVICE_SEND_COMMAND, VACUUM_SEND_COMMAND_SERVICE_SCHEMA,
        'async_send_command'
    )

    return True
def async_setup(hass, config):
    """Setup the camera component."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)

    hass.http.register_view(CameraImageView(component.entities))
    hass.http.register_view(CameraMjpegStream(component.entities))

    yield from component.async_setup(config)
    return True
def async_setup(hass, config):
    """Expose light control via statemachine and services."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_LIGHTS)
    yield from component.async_setup(config)

    # load profiles from files
    profiles_valid = yield from Profiles.load_profiles(hass)
    if not profiles_valid:
        return False

    @asyncio.coroutine
    def async_handle_light_service(service):
        """Hande a turn light on or off service call."""
        # Get the validated data
        params = service.data.copy()

        # Convert the entity ids to valid light ids
        target_lights = component.async_extract_from_service(service)
        params.pop(ATTR_ENTITY_ID, None)

        preprocess_turn_on_alternatives(params)

        update_tasks = []
        for light in target_lights:
            if service.service == SERVICE_TURN_ON:
                yield from light.async_turn_on(**params)
            elif service.service == SERVICE_TURN_OFF:
                yield from light.async_turn_off(**params)
            else:
                yield from light.async_toggle(**params)

            if not light.should_poll:
                continue
            update_tasks.append(light.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    # Listen for light on and light off service calls.
    descriptions = yield from hass.async_add_job(
        load_yaml_config_file, os.path.join(
            os.path.dirname(__file__), 'services.yaml'))

    hass.services.async_register(
        DOMAIN, SERVICE_TURN_ON, async_handle_light_service,
        descriptions.get(SERVICE_TURN_ON), schema=LIGHT_TURN_ON_SCHEMA)

    hass.services.async_register(
        DOMAIN, SERVICE_TURN_OFF, async_handle_light_service,
        descriptions.get(SERVICE_TURN_OFF), schema=LIGHT_TURN_OFF_SCHEMA)

    hass.services.async_register(
        DOMAIN, SERVICE_TOGGLE, async_handle_light_service,
        descriptions.get(SERVICE_TOGGLE), schema=LIGHT_TOGGLE_SCHEMA)

    return True
Exemple #31
0
def async_setup(hass, config):
    """Expose light control via state machine and services."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_LIGHTS)
    yield from component.async_setup(config)

    # load profiles from files
    profiles_valid = yield from Profiles.load_profiles(hass)
    if not profiles_valid:
        return False

    @asyncio.coroutine
    def async_handle_light_service(service):
        """Handle a turn light on or off service call."""
        # Get the validated data
        params = service.data.copy()

        # Convert the entity ids to valid light ids
        target_lights = component.async_extract_from_service(service)
        params.pop(ATTR_ENTITY_ID, None)

        preprocess_turn_on_alternatives(params)

        update_tasks = []
        for light in target_lights:
            if service.service == SERVICE_TURN_ON:
                yield from light.async_turn_on(**params)
            elif service.service == SERVICE_TURN_OFF:
                yield from light.async_turn_off(**params)
            else:
                yield from light.async_toggle(**params)

            if not light.should_poll:
                continue
            update_tasks.append(light.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    # Listen for light on and light off service calls.
    hass.services.async_register(DOMAIN,
                                 SERVICE_TURN_ON,
                                 async_handle_light_service,
                                 schema=LIGHT_TURN_ON_SCHEMA)

    hass.services.async_register(DOMAIN,
                                 SERVICE_TURN_OFF,
                                 async_handle_light_service,
                                 schema=LIGHT_TURN_OFF_SCHEMA)

    hass.services.async_register(DOMAIN,
                                 SERVICE_TOGGLE,
                                 async_handle_light_service,
                                 schema=LIGHT_TOGGLE_SCHEMA)

    return True
Exemple #32
0
def async_setup(hass, config):
    """Set up the camera component."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)

    hass.http.register_view(CameraImageView(component.entities))
    hass.http.register_view(CameraMjpegStream(component.entities))

    yield from component.async_setup(config)

    @callback
    def update_tokens(time):
        """Update tokens of the entities."""
        for entity in component.entities.values():
            entity.async_update_token()
            hass.async_add_job(entity.async_update_ha_state())

    async_track_time_interval(hass, update_tokens, TOKEN_CHANGE_INTERVAL)

    @asyncio.coroutine
    def async_handle_camera_service(service):
        """Handle calls to the camera services."""
        target_cameras = component.async_extract_from_service(service)

        for camera in target_cameras:
            if service.service == SERVICE_EN_MOTION:
                yield from camera.async_enable_motion_detection()
            elif service.service == SERVICE_DISEN_MOTION:
                yield from camera.async_disable_motion_detection()

        update_tasks = []
        for camera in target_cameras:
            if not camera.should_poll:
                continue

            update_coro = hass.async_add_job(
                camera.async_update_ha_state(True))
            if hasattr(camera, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.async_add_job(
        load_yaml_config_file, os.path.join(
            os.path.dirname(__file__), 'services.yaml'))

    hass.services.async_register(
        DOMAIN, SERVICE_EN_MOTION, async_handle_camera_service,
        descriptions.get(SERVICE_EN_MOTION), schema=CAMERA_SERVICE_SCHEMA)
    hass.services.async_register(
        DOMAIN, SERVICE_DISEN_MOTION, async_handle_camera_service,
        descriptions.get(SERVICE_DISEN_MOTION), schema=CAMERA_SERVICE_SCHEMA)

    return True
def async_setup(hass, config):
    """Track states and offer events for switches."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_SWITCHES)
    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_switch_service(service):
        """Handle calls to the switch services."""
        target_switches = component.async_extract_from_service(service)

        for switch in target_switches:
            if service.service == SERVICE_TURN_ON:
                yield from switch.async_turn_on()
            elif service.service == SERVICE_TOGGLE:
                yield from switch.async_toggle()
            else:
                yield from switch.async_turn_off()

        update_tasks = []
        for switch in target_switches:
            if not switch.should_poll:
                continue

            update_coro = hass.loop.create_task(
                switch.async_update_ha_state(True))
            if hasattr(switch, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    hass.services.async_register(DOMAIN,
                                 SERVICE_TURN_OFF,
                                 async_handle_switch_service,
                                 descriptions.get(SERVICE_TURN_OFF),
                                 schema=SWITCH_SERVICE_SCHEMA)
    hass.services.async_register(DOMAIN,
                                 SERVICE_TURN_ON,
                                 async_handle_switch_service,
                                 descriptions.get(SERVICE_TURN_ON),
                                 schema=SWITCH_SERVICE_SCHEMA)
    hass.services.async_register(DOMAIN,
                                 SERVICE_TOGGLE,
                                 async_handle_switch_service,
                                 descriptions.get(SERVICE_TOGGLE),
                                 schema=SWITCH_SERVICE_SCHEMA)

    return True
Exemple #34
0
def async_setup(hass, config):
    """Track states and offer events for remotes."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_REMOTES)
    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_remote_service(service):
        """Handle calls to the remote services."""
        target_remotes = component.async_extract_from_service(service)
        kwargs = service.data.copy()

        update_tasks = []
        for remote in target_remotes:
            if service.service == SERVICE_TURN_ON:
                yield from remote.async_turn_on(**kwargs)
            elif service.service == SERVICE_TOGGLE:
                yield from remote.async_toggle(**kwargs)
            elif service.service == SERVICE_SEND_COMMAND:
                yield from remote.async_send_command(**kwargs)
            else:
                yield from remote.async_turn_off(**kwargs)

            if not remote.should_poll:
                continue
            update_tasks.append(remote.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.async_add_job(
        load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))
    hass.services.async_register(DOMAIN,
                                 SERVICE_TURN_OFF,
                                 async_handle_remote_service,
                                 descriptions.get(SERVICE_TURN_OFF),
                                 schema=REMOTE_SERVICE_ACTIVITY_SCHEMA)
    hass.services.async_register(DOMAIN,
                                 SERVICE_TURN_ON,
                                 async_handle_remote_service,
                                 descriptions.get(SERVICE_TURN_ON),
                                 schema=REMOTE_SERVICE_ACTIVITY_SCHEMA)
    hass.services.async_register(DOMAIN,
                                 SERVICE_TOGGLE,
                                 async_handle_remote_service,
                                 descriptions.get(SERVICE_TOGGLE),
                                 schema=REMOTE_SERVICE_ACTIVITY_SCHEMA)
    hass.services.async_register(DOMAIN,
                                 SERVICE_SEND_COMMAND,
                                 async_handle_remote_service,
                                 descriptions.get(SERVICE_SEND_COMMAND),
                                 schema=REMOTE_SERVICE_SEND_COMMAND_SCHEMA)

    return True
Exemple #35
0
def async_setup(hass, config):
    """Track states and offer events for locks."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_LOCKS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_lock_service(service):
        """Handle calls to the lock services."""
        target_locks = component.async_extract_from_service(service)

        code = service.data.get(ATTR_CODE)

        for entity in target_locks:
            if service.service == SERVICE_LOCK:
                yield from entity.async_lock(code=code)
            else:
                yield from entity.async_unlock(code=code)

        update_tasks = []

        for entity in target_locks:
            if not entity.should_poll:
                continue

            update_coro = hass.loop.create_task(
                entity.async_update_ha_state(True))
            if hasattr(entity, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    hass.services.async_register(DOMAIN,
                                 SERVICE_UNLOCK,
                                 async_handle_lock_service,
                                 descriptions.get(SERVICE_UNLOCK),
                                 schema=LOCK_SERVICE_SCHEMA)
    hass.services.async_register(DOMAIN,
                                 SERVICE_LOCK,
                                 async_handle_lock_service,
                                 descriptions.get(SERVICE_LOCK),
                                 schema=LOCK_SERVICE_SCHEMA)

    return True
def async_setup(hass, config):
    """Track states and offer events for remotes."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_REMOTES)
    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_remote_service(service):
        """Handle calls to the remote services."""
        target_remotes = component.async_extract_from_service(service)
        kwargs = service.data.copy()

        update_tasks = []
        for remote in target_remotes:
            if service.service == SERVICE_TURN_ON:
                yield from remote.async_turn_on(**kwargs)
            elif service.service == SERVICE_TOGGLE:
                yield from remote.async_toggle(**kwargs)
            elif service.service == SERVICE_SEND_COMMAND:
                yield from remote.async_send_command(**kwargs)
            else:
                yield from remote.async_turn_off(**kwargs)

            if not remote.should_poll:
                continue
            update_tasks.append(remote.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.async_add_job(
        load_yaml_config_file, os.path.join(
            os.path.dirname(__file__), 'services.yaml'))
    hass.services.async_register(
        DOMAIN, SERVICE_TURN_OFF, async_handle_remote_service,
        descriptions.get(SERVICE_TURN_OFF),
        schema=REMOTE_SERVICE_ACTIVITY_SCHEMA)
    hass.services.async_register(
        DOMAIN, SERVICE_TURN_ON, async_handle_remote_service,
        descriptions.get(SERVICE_TURN_ON),
        schema=REMOTE_SERVICE_ACTIVITY_SCHEMA)
    hass.services.async_register(
        DOMAIN, SERVICE_TOGGLE, async_handle_remote_service,
        descriptions.get(SERVICE_TOGGLE),
        schema=REMOTE_SERVICE_ACTIVITY_SCHEMA)
    hass.services.async_register(
        DOMAIN, SERVICE_SEND_COMMAND, async_handle_remote_service,
        descriptions.get(SERVICE_SEND_COMMAND),
        schema=REMOTE_SERVICE_SEND_COMMAND_SCHEMA)

    return True
def async_setup(hass, config):
    """Track states and offer events for media_players."""
    component = EntityComponent(logging.getLogger(__name__), DOMAIN, hass,
                                SCAN_INTERVAL)
    """hass.http.register_view(MediaPlayerImageView(component.entities))"""

    yield from component.async_setup(config)

    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    @asyncio.coroutine
    def async_service_handler(service):
        """Map services to methods on MediaPlayerDevice."""
        method = SERVICE_TO_METHOD.get(service.service)
        if not method:
            return

        params = {}
        if service.service == SERVICE_CALL:
            params['dest'] = service.data.get(ATTR_PHONE_DESTINATION)
        target_phones = component.async_extract_from_service(service)

        update_tasks = []
        for phone in target_phones:
            yield from getattr(phone, method['method'])(**params)

        for phone in target_phones:
            if not phone.should_poll:
                continue

            update_coro = phone.async_update_ha_state(True)
            if hasattr(player, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    for service in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service].get('schema', PHONE_SCHEMA)
        hass.services.async_register(DOMAIN,
                                     service,
                                     async_service_handler,
                                     descriptions.get(service),
                                     schema=schema)

    return True
Exemple #38
0
def async_setup(hass, config: dict):
    """Expose fan control via statemachine and services."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_FANS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_fan_service(service):
        """Hande service call for fans."""
        method = SERVICE_TO_METHOD.get(service.service)
        params = service.data.copy()

        # Convert the entity ids to valid fan ids
        target_fans = component.async_extract_from_service(service)
        params.pop(ATTR_ENTITY_ID, None)

        for fan in target_fans:
            yield from getattr(fan, method['method'])(**params)

        update_tasks = []

        for fan in target_fans:
            if not fan.should_poll:
                continue

            update_coro = hass.loop.create_task(
                fan.async_update_ha_state(True))
            if hasattr(fan, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    # Listen for fan service calls.
    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    for service_name in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service_name].get('schema')
        hass.services.async_register(DOMAIN,
                                     service_name,
                                     async_handle_fan_service,
                                     descriptions.get(service_name),
                                     schema=schema)

    return True
Exemple #39
0
def async_setup(hass, config):
    """Track states and offer events for switches."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_SWITCHES)
    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_switch_service(service):
        """Handle calls to the switch services."""
        target_switches = component.async_extract_from_service(service)

        for switch in target_switches:
            if service.service == SERVICE_TURN_ON:
                yield from switch.async_turn_on()
            elif service.service == SERVICE_TOGGLE:
                yield from switch.async_toggle()
            else:
                yield from switch.async_turn_off()

        update_tasks = []
        for switch in target_switches:
            if not switch.should_poll:
                continue

            update_coro = hass.loop.create_task(
                switch.async_update_ha_state(True))
            if hasattr(switch, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file, os.path.join(
            os.path.dirname(__file__), 'services.yaml'))

    hass.services.async_register(
        DOMAIN, SERVICE_TURN_OFF, async_handle_switch_service,
        descriptions.get(SERVICE_TURN_OFF), schema=SWITCH_SERVICE_SCHEMA)
    hass.services.async_register(
        DOMAIN, SERVICE_TURN_ON, async_handle_switch_service,
        descriptions.get(SERVICE_TURN_ON), schema=SWITCH_SERVICE_SCHEMA)
    hass.services.async_register(
        DOMAIN, SERVICE_TOGGLE, async_handle_switch_service,
        descriptions.get(SERVICE_TOGGLE), schema=SWITCH_SERVICE_SCHEMA)

    return True
Exemple #40
0
def async_setup(hass, config):
    """Track states and offer events for covers."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_COVERS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_cover_service(service):
        """Handle calls to the cover services."""
        covers = component.async_extract_from_service(service)
        method = SERVICE_TO_METHOD.get(service.service)
        params = service.data.copy()
        params.pop(ATTR_ENTITY_ID, None)

        # call method
        for cover in covers:
            yield from getattr(cover, method['method'])(**params)

        update_tasks = []

        for cover in covers:
            if not cover.should_poll:
                continue

            update_coro = hass.loop.create_task(
                cover.async_update_ha_state(True))
            if hasattr(cover, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    for service_name in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service_name].get('schema',
                                                     COVER_SERVICE_SCHEMA)
        hass.services.async_register(DOMAIN,
                                     service_name,
                                     async_handle_cover_service,
                                     descriptions.get(service_name),
                                     schema=schema)

    return True
Exemple #41
0
def async_setup(hass, config: dict):
    """Expose fan control via statemachine and services."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_FANS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_fan_service(service):
        """Hande service call for fans."""
        method = SERVICE_TO_METHOD.get(service.service)
        params = service.data.copy()

        # Convert the entity ids to valid fan ids
        target_fans = component.async_extract_from_service(service)
        params.pop(ATTR_ENTITY_ID, None)

        for fan in target_fans:
            yield from getattr(fan, method['method'])(**params)

        update_tasks = []

        for fan in target_fans:
            if not fan.should_poll:
                continue

            update_coro = hass.async_add_job(
                fan.async_update_ha_state(True))
            if hasattr(fan, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    # Listen for fan service calls.
    descriptions = yield from hass.async_add_job(
        load_yaml_config_file, os.path.join(
            os.path.dirname(__file__), 'services.yaml'))

    for service_name in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service_name].get('schema')
        hass.services.async_register(
            DOMAIN, service_name, async_handle_fan_service,
            descriptions.get(service_name), schema=schema)

    return True
Exemple #42
0
def async_setup(hass, config):
    """Track states and offer events for locks."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_LOCKS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_lock_service(service):
        """Handle calls to the lock services."""
        target_locks = component.async_extract_from_service(service)

        code = service.data.get(ATTR_CODE)

        for entity in target_locks:
            if service.service == SERVICE_LOCK:
                yield from entity.async_lock(code=code)
            else:
                yield from entity.async_unlock(code=code)

        update_tasks = []

        for entity in target_locks:
            if not entity.should_poll:
                continue

            update_coro = hass.async_add_job(
                entity.async_update_ha_state(True))
            if hasattr(entity, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file, os.path.join(
            os.path.dirname(__file__), 'services.yaml'))

    hass.services.async_register(
        DOMAIN, SERVICE_UNLOCK, async_handle_lock_service,
        descriptions.get(SERVICE_UNLOCK), schema=LOCK_SERVICE_SCHEMA)
    hass.services.async_register(
        DOMAIN, SERVICE_LOCK, async_handle_lock_service,
        descriptions.get(SERVICE_LOCK), schema=LOCK_SERVICE_SCHEMA)

    return True
Exemple #43
0
def async_setup(hass, config):
    """Track states and offer events for locks."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_LOCKS)

    yield from component.async_setup(config)

    component.async_register_entity_service(SERVICE_UNLOCK,
                                            LOCK_SERVICE_SCHEMA,
                                            'async_unlock')
    component.async_register_entity_service(SERVICE_LOCK, LOCK_SERVICE_SCHEMA,
                                            'async_lock')
    component.async_register_entity_service(SERVICE_OPEN, LOCK_SERVICE_SCHEMA,
                                            'async_open')

    return True
Exemple #44
0
def test_parallel_updates_sync_platform(hass):
    """Warn we log when platform setup takes a long time."""
    platform = MockPlatform(setup_platform=lambda *args: None)

    loader.set_component('test_domain.platform', platform)

    component = EntityComponent(_LOGGER, DOMAIN, hass)
    component._platforms = {}

    yield from component.async_setup({DOMAIN: {
        'platform': 'platform',
    }})

    handle = list(component._platforms.values())[-1]

    assert handle.parallel_updates is not None
Exemple #45
0
def async_setup(hass, config):
    """Track states and offer events for covers."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_COVERS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_cover_service(service):
        """Handle calls to the cover services."""
        covers = component.async_extract_from_service(service)
        method = SERVICE_TO_METHOD.get(service.service)
        params = service.data.copy()
        params.pop(ATTR_ENTITY_ID, None)

        # call method
        for cover in covers:
            yield from getattr(cover, method['method'])(**params)

        update_tasks = []

        for cover in covers:
            if not cover.should_poll:
                continue

            update_coro = hass.async_add_job(
                cover.async_update_ha_state(True))
            if hasattr(cover, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file, os.path.join(
            os.path.dirname(__file__), 'services.yaml'))

    for service_name in SERVICE_TO_METHOD:
        schema = SERVICE_TO_METHOD[service_name].get(
            'schema', COVER_SERVICE_SCHEMA)
        hass.services.async_register(
            DOMAIN, service_name, async_handle_cover_service,
            descriptions.get(service_name), schema=schema)

    return True
Exemple #46
0
def async_setup(hass, config):
    """Track states and offer events for sensors."""
    component = EntityComponent(logging.getLogger(__name__), DOMAIN, hass,
                                SCAN_INTERVAL)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_alarm_service_handler(service):
        """Map services to methods on Alarm."""
        target_alarms = component.async_extract_from_service(service)

        code = service.data.get(ATTR_CODE)

        method = "async_{}".format(SERVICE_TO_METHOD[service.service])

        for alarm in target_alarms:
            yield from getattr(alarm, method)(code)

        update_tasks = []
        for alarm in target_alarms:
            if not alarm.should_poll:
                continue

            update_coro = hass.loop.create_task(
                alarm.async_update_ha_state(True))
            if hasattr(alarm, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    for service in SERVICE_TO_METHOD:
        hass.services.async_register(DOMAIN,
                                     service,
                                     async_alarm_service_handler,
                                     descriptions.get(service),
                                     schema=ALARM_SERVICE_SCHEMA)

    return True
Exemple #47
0
def async_setup(hass, config):
    """Set up the vacuum component."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_VACUUMS)

    yield from component.async_setup(config)

    component.async_register_entity_service(SERVICE_TURN_ON,
                                            VACUUM_SERVICE_SCHEMA,
                                            'async_turn_on')
    component.async_register_entity_service(SERVICE_TURN_OFF,
                                            VACUUM_SERVICE_SCHEMA,
                                            'async_turn_off')
    component.async_register_entity_service(SERVICE_TOGGLE,
                                            VACUUM_SERVICE_SCHEMA,
                                            'async_toggle')
    component.async_register_entity_service(SERVICE_START_PAUSE,
                                            VACUUM_SERVICE_SCHEMA,
                                            'async_start_pause')
    component.async_register_entity_service(SERVICE_START,
                                            VACUUM_SERVICE_SCHEMA,
                                            'async_start')
    component.async_register_entity_service(SERVICE_PAUSE,
                                            VACUUM_SERVICE_SCHEMA,
                                            'async_pause')
    component.async_register_entity_service(SERVICE_RETURN_TO_BASE,
                                            VACUUM_SERVICE_SCHEMA,
                                            'async_return_to_base')
    component.async_register_entity_service(SERVICE_CLEAN_SPOT,
                                            VACUUM_SERVICE_SCHEMA,
                                            'async_clean_spot')
    component.async_register_entity_service(SERVICE_LOCATE,
                                            VACUUM_SERVICE_SCHEMA,
                                            'async_locate')
    component.async_register_entity_service(SERVICE_STOP,
                                            VACUUM_SERVICE_SCHEMA,
                                            'async_stop')
    component.async_register_entity_service(
        SERVICE_SET_FAN_SPEED, VACUUM_SET_FAN_SPEED_SERVICE_SCHEMA,
        'async_set_fan_speed')
    component.async_register_entity_service(
        SERVICE_SEND_COMMAND, VACUUM_SEND_COMMAND_SERVICE_SCHEMA,
        'async_send_command')

    return True
Exemple #48
0
def async_setup(hass, config):
    """Set up the camera component."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)

    hass.http.register_view(CameraImageView(component.entities))
    hass.http.register_view(CameraMjpegStream(component.entities))

    yield from component.async_setup(config)

    @callback
    def update_tokens(time):
        """Update tokens of the entities."""
        for entity in component.entities.values():
            entity.async_update_token()
            hass.async_add_job(entity.async_update_ha_state())

    async_track_time_interval(hass, update_tokens, TOKEN_CHANGE_INTERVAL)
    return True
Exemple #49
0
def async_setup(hass, config):
    """Set up the camera component."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)

    hass.http.register_view(CameraImageView(component.entities))
    hass.http.register_view(CameraMjpegStream(component.entities))

    yield from component.async_setup(config)

    @callback
    def update_tokens(time):
        """Update tokens of the entities."""
        for entity in component.entities.values():
            entity.async_update_token()
            hass.async_add_job(entity.async_update_ha_state())

    async_track_time_interval(hass, update_tokens, TOKEN_CHANGE_INTERVAL)
    return True
def test_pararell_updates_sync_platform(hass):
    """Warn we log when platform setup takes a long time."""
    platform = MockPlatform()

    loader.set_component('test_domain.platform', platform)

    component = EntityComponent(_LOGGER, DOMAIN, hass)
    component._platforms = {}

    yield from component.async_setup({
        DOMAIN: {
            'platform': 'platform',
        }
    })

    handle = list(component._platforms.values())[-1]

    assert handle.parallel_updates is not None
def async_setup(hass, config):
    """Track states and offer events for locks."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL,
                                GROUP_NAME_ALL_LOCKS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_lock_service(service):
        """Handle calls to the lock services."""
        target_locks = component.async_extract_from_service(service)

        code = service.data.get(ATTR_CODE)

        update_tasks = []
        for entity in target_locks:
            if service.service == SERVICE_LOCK:
                yield from entity.async_lock(code=code)
            elif service.service == SERVICE_OPEN:
                yield from entity.async_open(code=code)
            else:
                yield from entity.async_unlock(code=code)

            if not entity.should_poll:
                continue
            update_tasks.append(entity.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    hass.services.async_register(DOMAIN,
                                 SERVICE_UNLOCK,
                                 async_handle_lock_service,
                                 schema=LOCK_SERVICE_SCHEMA)
    hass.services.async_register(DOMAIN,
                                 SERVICE_LOCK,
                                 async_handle_lock_service,
                                 schema=LOCK_SERVICE_SCHEMA)
    hass.services.async_register(DOMAIN,
                                 SERVICE_OPEN,
                                 async_handle_lock_service,
                                 schema=LOCK_SERVICE_SCHEMA)

    return True
Exemple #52
0
def async_setup(hass, config):
    """Track states and offer events for sensors."""
    component = EntityComponent(
        logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_alarm_service_handler(service):
        """Map services to methods on Alarm."""
        target_alarms = component.async_extract_from_service(service)

        code = service.data.get(ATTR_CODE)

        method = "async_{}".format(SERVICE_TO_METHOD[service.service])

        for alarm in target_alarms:
            yield from getattr(alarm, method)(code)

        update_tasks = []
        for alarm in target_alarms:
            if not alarm.should_poll:
                continue

            update_coro = hass.async_add_job(
                alarm.async_update_ha_state(True))
            if hasattr(alarm, 'async_update'):
                update_tasks.append(update_coro)
            else:
                yield from update_coro

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    descriptions = yield from hass.loop.run_in_executor(
        None, load_yaml_config_file, os.path.join(
            os.path.dirname(__file__), 'services.yaml'))

    for service in SERVICE_TO_METHOD:
        hass.services.async_register(
            DOMAIN, service, async_alarm_service_handler,
            descriptions.get(service), schema=ALARM_SERVICE_SCHEMA)

    return True
Exemple #53
0
def test_platform_not_ready(hass):
    """Test that we retry when platform not ready."""
    platform1_setup = Mock(side_effect=[PlatformNotReady, PlatformNotReady,
                                        None])
    loader.set_component(hass, 'test_domain.mod1',
                         MockPlatform(platform1_setup))

    component = EntityComponent(_LOGGER, DOMAIN, hass)

    yield from component.async_setup({
        DOMAIN: {
            'platform': 'mod1'
        }
    })

    assert len(platform1_setup.mock_calls) == 1
    assert 'test_domain.mod1' not in hass.config.components

    utcnow = dt_util.utcnow()

    with patch('homeassistant.util.dt.utcnow', return_value=utcnow):
        # Should not trigger attempt 2
        async_fire_time_changed(hass, utcnow + timedelta(seconds=29))
        yield from hass.async_block_till_done()
        assert len(platform1_setup.mock_calls) == 1

        # Should trigger attempt 2
        async_fire_time_changed(hass, utcnow + timedelta(seconds=30))
        yield from hass.async_block_till_done()
        assert len(platform1_setup.mock_calls) == 2
        assert 'test_domain.mod1' not in hass.config.components

        # This should not trigger attempt 3
        async_fire_time_changed(hass, utcnow + timedelta(seconds=59))
        yield from hass.async_block_till_done()
        assert len(platform1_setup.mock_calls) == 2

        # Trigger attempt 3, which succeeds
        async_fire_time_changed(hass, utcnow + timedelta(seconds=60))
        yield from hass.async_block_till_done()
        assert len(platform1_setup.mock_calls) == 3
        assert 'test_domain.mod1' in hass.config.components
def async_setup(hass, config):
    """Track states and offer events for locks."""
    component = EntityComponent(
        _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_LOCKS)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_lock_service(service):
        """Handle calls to the lock services."""
        target_locks = component.async_extract_from_service(service)

        code = service.data.get(ATTR_CODE)

        update_tasks = []
        for entity in target_locks:
            if service.service == SERVICE_LOCK:
                yield from entity.async_lock(code=code)
            elif service.service == SERVICE_OPEN:
                yield from entity.async_open(code=code)
            else:
                yield from entity.async_unlock(code=code)

            if not entity.should_poll:
                continue
            update_tasks.append(entity.async_update_ha_state(True))

        if update_tasks:
            yield from asyncio.wait(update_tasks, loop=hass.loop)

    hass.services.async_register(
        DOMAIN, SERVICE_UNLOCK, async_handle_lock_service,
        schema=LOCK_SERVICE_SCHEMA)
    hass.services.async_register(
        DOMAIN, SERVICE_LOCK, async_handle_lock_service,
        schema=LOCK_SERVICE_SCHEMA)
    hass.services.async_register(
        DOMAIN, SERVICE_OPEN, async_handle_lock_service,
        schema=LOCK_SERVICE_SCHEMA)

    return True
Exemple #55
0
def async_setup(hass, config):
    """Set up image processing."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_scan_service(service):
        """Service handler for scan."""
        image_entities = component.async_extract_from_service(service)

        update_task = [entity.async_update_ha_state(True) for
                       entity in image_entities]
        if update_task:
            yield from asyncio.wait(update_task, loop=hass.loop)

    hass.services.async_register(
        DOMAIN, SERVICE_SCAN, async_scan_service,
        schema=SERVICE_SCAN_SCHEMA)

    return True
Exemple #56
0
def async_setup(hass, config):
    """Set up the scenes."""
    logger = logging.getLogger(__name__)
    component = EntityComponent(logger, DOMAIN, hass)

    yield from component.async_setup(config)

    @asyncio.coroutine
    def async_handle_scene_service(service):
        """Handle calls to the switch services."""
        target_scenes = component.async_extract_from_service(service)

        tasks = [scene.async_activate() for scene in target_scenes]
        if tasks:
            yield from asyncio.wait(tasks, loop=hass.loop)

    hass.services.async_register(
        DOMAIN, SERVICE_TURN_ON, async_handle_scene_service,
        schema=SCENE_SERVICE_SCHEMA)

    return True
def test_platform_error_slow_setup(hass, caplog):
    """Don't block startup more than SLOW_SETUP_MAX_WAIT."""
    with patch.object(entity_component, 'SLOW_SETUP_MAX_WAIT', 0):
        called = []

        @asyncio.coroutine
        def setup_platform(*args):
            called.append(1)
            yield from asyncio.sleep(1, loop=hass.loop)

        platform = MockPlatform(async_setup_platform=setup_platform)
        component = EntityComponent(_LOGGER, DOMAIN, hass)
        loader.set_component('test_domain.test_platform', platform)
        yield from component.async_setup({
            DOMAIN: {
                'platform': 'test_platform',
            }
        })
        assert len(called) == 1
        assert 'test_domain.test_platform' not in hass.config.components
        assert 'test_platform is taking longer than 0 seconds' in caplog.text