def setup_platform(hass, config, add_devices, discovery_info=None):
    'Set up the Yeelight bulbs.'
    from yeelight.enums import PowerMode
    if (DATA_KEY not in hass.data):
        hass.data[DATA_KEY] = {}
    lights = []
    if (discovery_info is not None):
        _LOGGER.debug('Adding autodetected %s', discovery_info['hostname'])
        device_type = discovery_info['device_type']
        device_type = LEGACY_DEVICE_TYPE_MAP.get(device_type, device_type)
        name = ('yeelight_%s_%s' % (device_type, discovery_info['properties']['mac']))
        host = discovery_info['host']
        device = {'name': name, 'ipaddr': host}
        light = YeelightLight(device, DEVICE_SCHEMA({}))
        lights.append(light)
        hass.data[DATA_KEY][host] = light
    else:
        for (host, device_config) in config[CONF_DEVICES].items():
            device = {'name': device_config[CONF_NAME], 'ipaddr': host}
            light = YeelightLight(device, device_config)
            lights.append(light)
            hass.data[DATA_KEY][host] = light
    add_devices(lights, True)

    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 = service.data.get(ATTR_ENTITY_ID)
        if entity_ids:
            target_devices = [dev for dev in hass.data[DATA_KEY].values() if (dev.entity_id in entity_ids)]
        else:
            target_devices = hass.data[DATA_KEY].values()
        for target_device in target_devices:
            if (service.service == SERVICE_SET_MODE):
                target_device.set_mode(**params)
    service_schema_set_mode = YEELIGHT_SERVICE_SCHEMA.extend({vol.Required(ATTR_MODE): vol.In([mode.name.lower() for mode in PowerMode])})
    hass.services.register(DOMAIN, SERVICE_SET_MODE, service_handler, schema=service_schema_set_mode)
Set up the Yeelight bulbs.
def setup_platform(hass, config, add_devices, discovery_info=None):
    
    from yeelight.enums import PowerMode
    if (DATA_KEY not in hass.data):
        hass.data[DATA_KEY] = {}
    lights = []
    if (discovery_info is not None):
        _LOGGER.debug('Adding autodetected %s', discovery_info['hostname'])
        device_type = discovery_info['device_type']
        device_type = LEGACY_DEVICE_TYPE_MAP.get(device_type, device_type)
        name = ('yeelight_%s_%s' % (device_type, discovery_info['properties']['mac']))
        host = discovery_info['host']
        device = {'name': name, 'ipaddr': host}
        light = YeelightLight(device, DEVICE_SCHEMA({}))
        lights.append(light)
        hass.data[DATA_KEY][host] = light
    else:
        for (host, device_config) in config[CONF_DEVICES].items():
            device = {'name': device_config[CONF_NAME], 'ipaddr': host}
            light = YeelightLight(device, device_config)
            lights.append(light)
            hass.data[DATA_KEY][host] = light
    add_devices(lights, True)

    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 = service.data.get(ATTR_ENTITY_ID)
        if entity_ids:
            target_devices = [dev for dev in hass.data[DATA_KEY].values() if (dev.entity_id in entity_ids)]
        else:
            target_devices = hass.data[DATA_KEY].values()
        for target_device in target_devices:
            if (service.service == SERVICE_SET_MODE):
                target_device.set_mode(**params)
    service_schema_set_mode = YEELIGHT_SERVICE_SCHEMA.extend({vol.Required(ATTR_MODE): vol.In([mode.name.lower() for mode in PowerMode])})
    hass.services.register(DOMAIN, SERVICE_SET_MODE, service_handler, schema=service_schema_set_mode)