custom_components/xiaomi_miot_raw/media_player.py
async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
    """Set up the fan from config."""

    if DATA_KEY not in hass.data:
        hass.data[DATA_KEY] = {}

    host = config.get(CONF_HOST)
    token = config.get(CONF_TOKEN)
    mapping = config.get(CONF_MAPPING)
    params = config.get(CONF_CONTROL_PARAMS)

    mappingnew = {}

    main_mi_type = None
    other_mi_type = []

    for t in MAP[TYPE]:
        if mapping.get(t):
            other_mi_type.append(t)
        if 'main' in (params.get(t) or ""):
            main_mi_type = t

    try:
        other_mi_type.remove(main_mi_type)
    except:
        pass

    if main_mi_type or type(params) == OrderedDict:
        for k,v in mapping.items():
            for kk,vv in v.items():
                mappingnew[f"{k[:10]}_{kk}"] = vv

        if 'a_l' not in mapping.keys():
            persistent_notification.async_create(
                hass,
                f"为了支持更多种类小爱，配置参数有变动，\n"
                f"请删除您的 **{config.get(CONF_NAME)}** 重新配置。谢谢\n",
                "Xiaomi MIoT")

        _LOGGER.info("Initializing %s with host %s (token %s...)", config.get(CONF_NAME), host, token[:5])

        try:
            if type(params) == OrderedDict:
                miio_device = MiotDevice(ip=host, token=token, mapping=mapping)
            else:
                miio_device = MiotDevice(ip=host, token=token, mapping=mappingnew)
            device_info = dev_info(host,token,"","")
            device = MiotMediaPlayer(miio_device, config, device_info, hass, main_mi_type)
        except DeviceException as de:
            _LOGGER.warn(de)
            raise PlatformNotReady

        _LOGGER.info(f"{main_mi_type} is the main device of {host}.")
        hass.data[DOMAIN]['miot_main_entity'][config['config_entry'].entry_id] = device
        hass.data[DOMAIN]['entities'][device.unique_id] = device
        async_add_devices([device], update_before_add=True)

    else:
        _LOGGER.error("media player只能作为主设备！")
        pass
Set up the fan from config.
Set up the fan from config.
Set up the fan from config .
async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
    if DATA_KEY not in hass.data:
        hass.data[DATA_KEY] = {}
    host = config.get(CONF_HOST)
    token = config.get(CONF_TOKEN)
    mapping = config.get(CONF_MAPPING)
    params = config.get(CONF_CONTROL_PARAMS)
    mappingnew = {}
    main_mi_type = None
    other_mi_type = []
    for t in MAP[TYPE]:
        if mapping.get(t):
            other_mi_type.append(t)
        if 'main' in (params.get(t) or ""):
            main_mi_type = t
    try:
        other_mi_type.remove(main_mi_type)
    except:
        pass
    if main_mi_type or type(params) == OrderedDict:
        for k,v in mapping.items():
            for kk,vv in v.items():
                mappingnew[f"{k[:10]}_{kk}"] = vv
        if 'a_l' not in mapping.keys():
            persistent_notification.async_create(
                hass,
                f"为了支持更多种类小爱，配置参数有变动，\n"
                f"请删除您的 **{config.get(CONF_NAME)}** 重新配置。谢谢\n",
                "Xiaomi MIoT")
        _LOGGER.info("Initializing %s with host %s (token %s...)", config.get(CONF_NAME), host, token[:5])
        try:
            if type(params) == OrderedDict:
                miio_device = MiotDevice(ip=host, token=token, mapping=mapping)
            else:
                miio_device = MiotDevice(ip=host, token=token, mapping=mappingnew)
            device_info = dev_info(host,token,"","")
            device = MiotMediaPlayer(miio_device, config, device_info, hass, main_mi_type)
        except DeviceException as de:
            _LOGGER.warn(de)
            raise PlatformNotReady
        _LOGGER.info(f"{main_mi_type} is the main device of {host}.")
        hass.data[DOMAIN]['miot_main_entity'][config['config_entry'].entry_id] = device
        hass.data[DOMAIN]['entities'][device.unique_id] = device
        async_add_devices([device], update_before_add=True)
    else:
        _LOGGER.error("media player只能作为主设备！")
        pass
async def async_setup_platform ( hass , config , async_add_devices , discovery_info = None ) : if DATA_KEY not in hass . data : hass . data [ DATA_KEY ] = { } host = config . get ( CONF_HOST ) token = config . get ( CONF_TOKEN ) mapping = config . get ( CONF_MAPPING ) params = config . get ( CONF_CONTROL_PARAMS ) mappingnew = { } main_mi_type = None other_mi_type = [ ] for t in MAP [ TYPE ] : if mapping . get ( t ) : other_mi_type . append ( t ) if 'main' in ( params . get ( t ) or "" ) : main_mi_type = t try : other_mi_type . remove ( main_mi_type ) except : pass if main_mi_type or type ( params ) == OrderedDict : for k , v in mapping . items ( ) : for kk , vv in v . items ( ) : mappingnew [ f"{k[:10]}_{kk}" ] = vv if 'a_l' not in mapping . keys ( ) : persistent_notification . async_create ( hass , f"为了支持更多种类小爱，配置参数有变动，\n" f"请删除您的 **{config.get(CONF_NAME)}** 重新配置。谢谢\n", "Xiaomi MIoT" ) _LOGGER . info ( "Initializing %s with host %s (token %s...)" , config . get ( CONF_NAME ) , host , token [ : 5 ] ) try : if type ( params ) == OrderedDict : miio_device = MiotDevice ( ip = host , token = token , mapping = mapping ) else : miio_device = MiotDevice ( ip = host , token = token , mapping = mappingnew ) device_info = dev_info ( host , token , "" , "" ) device = MiotMediaPlayer ( miio_device , config , device_info , hass , main_mi_type ) except DeviceException as de : _LOGGER . warn ( de ) raise PlatformNotReady _LOGGER . info ( f"{main_mi_type} is the main device of {host}." ) hass . data [ DOMAIN ] [ 'miot_main_entity' ] [ config [ 'config_entry' ] . entry_id ] = device hass . data [ DOMAIN ] [ 'entities' ] [ device . unique_id ] = device async_add_devices ( [ device ] , update_before_add = True ) else : _LOGGER . error ( "media player只能作为主设备！") pass
Set up the fan from config.
Set up the fan from config .
"""Set up the fan from config."""
hass
config
async_add_devices
discovery_info