Play a URI.

        Playing a URI will replace what was playing with the stream given by
        the URI. For some streams at least a title is required as metadata.
        This can be provided using the `meta` argument or the `title` argument.
        If the `title` argument is provided minimal metadata will be generated.
        If `meta` argument is provided the `title` argument is ignored.

        Args:
            uri (str): URI of the stream to be played.
            meta (str): The metadata to show in the player, DIDL format.
            title (str): The title to show in the player (if no meta).
            start (bool): If the URI that has been set should start playing.
            force_radio (bool): forces a uri to play as a radio stream.

        On a Sonos controller music is shown with one of the following display
        formats and controls:

        * Radio format: Shows the name of the radio station and other available
          data. No seek, next, previous, or voting capability.
          Examples: TuneIn, radioPup
        * Smart Radio:  Shows track name, artist, and album. Limited seek, next
          and sometimes voting capability depending on the Music Service.
          Examples: Amazon Prime Stations, Pandora Radio Stations.
        * Track format: Shows track name, artist, and album the same as when
          playing from a queue. Full seek, next and previous capabilities.
          Examples: Spotify, Napster, Rhapsody.

        How it is displayed is determined by the URI prefix:
        `x-sonosapi-stream:`, `x-sonosapi-radio:`, `x-rincon-mp3radio:`,
        `hls-radio:` default to radio or smart radio format depending on the
        stream. Others default to track format: `x-file-cifs:`, `aac:`,
        `http:`, `https:`, `x-sonos-spotify:` (used by Spotify),
        `x-sonosapi-hls-static:` (Amazon Prime),
        `x-sonos-http:` (Google Play & Napster).

        Some URIs that default to track format could be radio streams,
        typically `http:`, `https:` or `aac:`.
        To force display and controls to Radio format set `force_radio=True`

        .. note:: Other URI prefixes exist but are less common.
           If you have information on these please add to this doc string.

        .. note:: A change in Sonos® (as of at least version 6.4.2) means that
           the devices no longer accepts ordinary `http:` and `https:` URIs for
           radio stations. This method has the option to replaces these
           prefixes with the one that Sonos® expects: `x-rincon-mp3radio:` by
           using the "force_radio=True" parameter.
           A few streams may fail if not forced to to Radio format.

def play_uri(self, uri='', meta='', title='', start=True,
                 force_radio=False):
        """Play a URI.

        Playing a URI will replace what was playing with the stream given by
        the URI. For some streams at least a title is required as metadata.
        This can be provided using the `meta` argument or the `title` argument.
        If the `title` argument is provided minimal metadata will be generated.
        If `meta` argument is provided the `title` argument is ignored.

        Args:
            uri (str): URI of the stream to be played.
            meta (str): The metadata to show in the player, DIDL format.
            title (str): The title to show in the player (if no meta).
            start (bool): If the URI that has been set should start playing.
            force_radio (bool): forces a uri to play as a radio stream.

        On a Sonos controller music is shown with one of the following display
        formats and controls:

        * Radio format: Shows the name of the radio station and other available
          data. No seek, next, previous, or voting capability.
          Examples: TuneIn, radioPup
        * Smart Radio:  Shows track name, artist, and album. Limited seek, next
          and sometimes voting capability depending on the Music Service.
          Examples: Amazon Prime Stations, Pandora Radio Stations.
        * Track format: Shows track name, artist, and album the same as when
          playing from a queue. Full seek, next and previous capabilities.
          Examples: Spotify, Napster, Rhapsody.

        How it is displayed is determined by the URI prefix:
        `x-sonosapi-stream:`, `x-sonosapi-radio:`, `x-rincon-mp3radio:`,
        `hls-radio:` default to radio or smart radio format depending on the
        stream. Others default to track format: `x-file-cifs:`, `aac:`,
        `http:`, `https:`, `x-sonos-spotify:` (used by Spotify),
        `x-sonosapi-hls-static:` (Amazon Prime),
        `x-sonos-http:` (Google Play & Napster).

        Some URIs that default to track format could be radio streams,
        typically `http:`, `https:` or `aac:`.
        To force display and controls to Radio format set `force_radio=True`

        .. note:: Other URI prefixes exist but are less common.
           If you have information on these please add to this doc string.

        .. note:: A change in Sonos® (as of at least version 6.4.2) means that
           the devices no longer accepts ordinary `http:` and `https:` URIs for
           radio stations. This method has the option to replaces these
           prefixes with the one that Sonos® expects: `x-rincon-mp3radio:` by
           using the "force_radio=True" parameter.
           A few streams may fail if not forced to to Radio format.
        """
        if meta == '' and title != '':
            meta_template = '<DIDL-Lite xmlns:dc="http://purl.org/dc/elements'\
                '/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" '\
                'xmlns:r="urn:schemas-rinconnetworks-com:metadata-1-0/" '\
                'xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">'\
                '<item id="R:0/0/0" parentID="R:0/0" restricted="true">'\
                '<dc:title>{title}</dc:title><upnp:class>'\
                'object.item.audioItem.audioBroadcast</upnp:class><desc '\
                'id="cdudn" nameSpace="urn:schemas-rinconnetworks-com:'\
                'metadata-1-0/">{service}</desc></item></DIDL-Lite>'
            tunein_service = 'SA_RINCON65031_'
            # Radio stations need to have at least a title to play
            meta = meta_template.format(
                title=escape(title),
                service=tunein_service)

        # change uri prefix to force radio style display and commands
        if force_radio:
            colon = uri.find(':')
            if colon > 0:
                uri = 'x-rincon-mp3radio{0}'.format(uri[colon:])

        self.avTransport.SetAVTransportURI([
            ('InstanceID', 0),
            ('CurrentURI', uri),
            ('CurrentURIMetaData', meta)
        ])
        # The track is enqueued, now play it if needed
        if start:
            return self.play()
        return False