'Refresh key device data.'

def refresh(self, device, session):

    if session:
        self._session = session
    if device:
        self._device = device
        self._session = None
    if self._device:
        self._machine_identifier = self._convert_na_to_none(self._device.machineIdentifier)
        self._name = (self._convert_na_to_none(self._device.title) or DEVICE_DEFAULT_NAME)
        self._device_protocol_capabilities = self._device.protocolCapabilities
    if (self._device and self.plex_sessions.get(self._device.machineIdentifier, None)):
        self._session = self._convert_na_to_none(self.plex_sessions.get(self._device.machineIdentifier, None))
    if self._session:
        self._media_position = self._convert_na_to_none(self._session.viewOffset)
        self._media_content_id = self._convert_na_to_none(self._session.ratingKey)
        self._media_content_rating = self._convert_na_to_none(self._session.contentRating)
    else:
        self._media_position = None
        self._media_content_id = None
    if (self._session and self._session.player):
        self._is_player_available = True
        self._machine_identifier = self._convert_na_to_none(self._session.player.machineIdentifier)
        self._name = self._convert_na_to_none(self._session.player.title)
        self._player_state = self._session.player.state
        self._session_username = self._convert_na_to_none(self._session.username)
        self._make = self._convert_na_to_none(self._session.player.device)
    else:
        self._is_player_available = False
    if (self._player_state == 'playing'):
        self._is_player_active = True
        self._state = STATE_PLAYING
    elif (self._player_state == 'paused'):
        self._is_player_active = True
        self._state = STATE_PAUSED
    elif self.device:
        self._is_player_active = False
        self._state = STATE_IDLE
    else:
        self._is_player_active = False
        self._state = STATE_OFF
    if (self._is_player_active and (self._session is not None)):
        self._session_type = self._session.type
        self._media_duration = self._convert_na_to_none(self._session.duration)
    else:
        self._session_type = None
        self._media_duration = None
    if (self._session_type == 'clip'):
        _LOGGER.debug('Clip    content    type    detected,    compatibility    may    vary:    %s', self.entity_id)
        self._media_content_type = MEDIA_TYPE_TVSHOW
    elif (self._session_type == 'episode'):
        self._media_content_type = MEDIA_TYPE_TVSHOW
    elif (self._session_type == 'movie'):
        self._media_content_type = MEDIA_TYPE_VIDEO
    elif (self._session_type == 'track'):
        self._media_content_type = MEDIA_TYPE_MUSIC
    else:
        self._media_content_type = None
    if self._session:
        self._media_title = self._convert_na_to_none(self._session.title)
    if ((self.media_content_type == MEDIA_TYPE_VIDEO) and (self._convert_na_to_none(self._session.year) is not None)):
        self._media_title += (('    (' + str(self._session.year)) + ')')
    if (self._is_player_active and (self._media_content_type is MEDIA_TYPE_TVSHOW)):
        if callable(self._convert_na_to_none(self._session.seasons)):
            self._media_season = self._convert_na_to_none(self._session.seasons()[0].index).zfill(2)
        elif (self._convert_na_to_none(self._session.parentIndex) is not None):
            self._media_season = self._session.parentIndex.zfill(2)
        else:
            self._media_season = None
        self._media_series_title = self._convert_na_to_none(self._session.grandparentTitle)
        if (self._convert_na_to_none(self._session.index) is not None):
            self._media_episode = str(self._session.index).zfill(2)
    else:
        self._media_season = None
        self._media_series_title = None
        self._media_episode = None
    if (self._is_player_active and (self._media_content_type == MEDIA_TYPE_MUSIC)):
        self._media_album_name = self._convert_na_to_none(self._session.parentTitle)
        self._media_album_artist = self._convert_na_to_none(self._session.grandparentTitle)
        self._media_track = self._convert_na_to_none(self._session.index)
        self._media_artist = self._convert_na_to_none(self._session.originalTitle)
        if (self._media_artist is None):
            _LOGGER.debug('Using    album    artist    because    track    artist    was    not    found:    %s', self.entity_id)
            self._media_artist = self._media_album_artist
    else:
        self._media_album_name = None
        self._media_album_artist = None
        self._media_track = None
        self._media_artist = None
    if ((self._session is not None) and (self._session.librarySectionID is not None)):
        self._app_name = self._convert_na_to_none(self._session.server.library.sectionByID(self._session.librarySectionID).title)
    else:
        self._app_name = ''
    if (self._session is not None):
        thumb_url = self._get_thumbnail_url(self._session.thumb)
        if ((self.media_content_type is MEDIA_TYPE_TVSHOW) and (not self.config.get(CONF_USE_EPISODE_ART))):
            thumb_url = self._get_thumbnail_url(self._session.grandparentThumb)
        if (thumb_url is None):
            _LOGGER.debug('Using    media    art    because    media    thumb    was    not    found:    %s', self.entity_id)
            thumb_url = self._get_thumbnail_url(self._session.art)
        self._media_image_url = thumb_url
    else:
        self._media_image_url = None
