mirror of
https://github.com/home-assistant/core.git
synced 2025-12-26 14:08:21 +00:00
Fix ps4 not able to use different PSN accounts (#22799)
* Remove skipping of creds step. * Check for device added per account * typo * lint * Pylint * Fix test * Fix test * Typo * Add auto location * blank space * Add new identifier handling + fix select source * Add cred_timeout error * add credential timeout error * Fix Tests * patch decorator * Update test_config_flow.py * add test * Revert * Rename vars * fix tests * Add attr location * Bump 0.6.0 * Bump 0.6.0 * Bump 0.6.0 * Update handling exception * Update remove method * Update tests * Refactoring * Pylint * revert * chmod * 0.6.1 * 0.6.1 * 0.6.1 * Remove func * Add migration * Version 3 * Remove redefinition * Add format unique id * Add format unique id * pylint * pylint * 0.7.1 * 0.7.1 * 0.7.1 * Changes with media_art call * Add library exception * 0.7.2 * 0.7.2 * 0.7.2 * Version and entry_version update * Revert list comprehension * Corrected exception handling * Update media_player.py * Update media_player.py * white space
This commit is contained in:
@@ -9,6 +9,7 @@ from homeassistant.components.media_player import (
|
||||
from homeassistant.components.media_player.const import (
|
||||
MEDIA_TYPE_GAME, SUPPORT_SELECT_SOURCE, SUPPORT_STOP, SUPPORT_TURN_OFF,
|
||||
SUPPORT_TURN_ON)
|
||||
from homeassistant.components.ps4 import format_unique_id
|
||||
from homeassistant.const import (
|
||||
ATTR_COMMAND, ATTR_ENTITY_ID, CONF_HOST, CONF_NAME, CONF_REGION,
|
||||
CONF_TOKEN, STATE_IDLE, STATE_OFF, STATE_PLAYING)
|
||||
@@ -87,7 +88,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
name = device[CONF_NAME]
|
||||
ps4 = pyps4.Ps4(host, creds)
|
||||
device_list.append(PS4Device(
|
||||
name, host, region, ps4, games_file))
|
||||
name, host, region, ps4, creds, games_file))
|
||||
add_entities(device_list, True)
|
||||
|
||||
|
||||
@@ -102,21 +103,24 @@ class PS4Data():
|
||||
class PS4Device(MediaPlayerDevice):
|
||||
"""Representation of a PS4."""
|
||||
|
||||
def __init__(self, name, host, region, ps4, games_file):
|
||||
def __init__(self, name, host, region, ps4, creds, games_file):
|
||||
"""Initialize the ps4 device."""
|
||||
self._ps4 = ps4
|
||||
self._host = host
|
||||
self._name = name
|
||||
self._region = region
|
||||
self._creds = creds
|
||||
self._state = None
|
||||
self._games_filename = games_file
|
||||
self._media_content_id = None
|
||||
self._media_title = None
|
||||
self._media_image = None
|
||||
self._media_type = None
|
||||
self._source = None
|
||||
self._games = {}
|
||||
self._source_list = []
|
||||
self._retry = 0
|
||||
self._disconnected = False
|
||||
self._info = None
|
||||
self._unique_id = None
|
||||
self._power_on = False
|
||||
@@ -145,6 +149,7 @@ class PS4Device(MediaPlayerDevice):
|
||||
status = None
|
||||
if status is not None:
|
||||
self._retry = 0
|
||||
self._disconnected = False
|
||||
if status.get('status') == 'Ok':
|
||||
# Check if only 1 device in Hass.
|
||||
if len(self.hass.data[PS4_DATA].devices) == 1:
|
||||
@@ -187,7 +192,9 @@ class PS4Device(MediaPlayerDevice):
|
||||
"""Set states for state unknown."""
|
||||
self.reset_title()
|
||||
self._state = None
|
||||
_LOGGER.warning("PS4 could not be reached")
|
||||
if self._disconnected is False:
|
||||
_LOGGER.warning("PS4 could not be reached")
|
||||
self._disconnected = True
|
||||
self._retry = 0
|
||||
|
||||
def reset_title(self):
|
||||
@@ -198,19 +205,24 @@ class PS4Device(MediaPlayerDevice):
|
||||
|
||||
def get_title_data(self, title_id, name):
|
||||
"""Get PS Store Data."""
|
||||
from pyps4_homeassistant.errors import PSDataIncomplete
|
||||
app_name = None
|
||||
art = None
|
||||
try:
|
||||
app_name, art = self._ps4.get_ps_store_data(
|
||||
title = self._ps4.get_ps_store_data(
|
||||
name, title_id, self._region)
|
||||
except TypeError:
|
||||
except PSDataIncomplete:
|
||||
_LOGGER.error(
|
||||
"Could not find data in region: %s for PS ID: %s",
|
||||
self._region, title_id)
|
||||
else:
|
||||
app_name = title.name
|
||||
art = title.cover_art
|
||||
finally:
|
||||
self._media_title = app_name or name
|
||||
self._source = self._media_title
|
||||
self._media_image = art
|
||||
self._media_type = MEDIA_TYPE_GAME
|
||||
self.update_list()
|
||||
|
||||
def update_list(self):
|
||||
@@ -257,7 +269,7 @@ class PS4Device(MediaPlayerDevice):
|
||||
self.save_games(games)
|
||||
|
||||
def get_device_info(self, status):
|
||||
"""Return device info for registry."""
|
||||
"""Set device info for registry."""
|
||||
_sw_version = status['system-version']
|
||||
_sw_version = _sw_version[1:4]
|
||||
sw_version = "{}.{}".format(_sw_version[0], _sw_version[1:])
|
||||
@@ -270,12 +282,14 @@ class PS4Device(MediaPlayerDevice):
|
||||
'manufacturer': 'Sony Interactive Entertainment Inc.',
|
||||
'sw_version': sw_version
|
||||
}
|
||||
self._unique_id = status['host-id']
|
||||
|
||||
self._unique_id = format_unique_id(self._creds, status['host-id'])
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Remove Entity from Hass."""
|
||||
# Close TCP Socket
|
||||
await self.hass.async_add_executor_job(self._ps4.close)
|
||||
if self._ps4.connected:
|
||||
await self.hass.async_add_executor_job(self._ps4.close)
|
||||
self.hass.data[PS4_DATA].devices.remove(self)
|
||||
|
||||
@property
|
||||
@@ -321,7 +335,7 @@ class PS4Device(MediaPlayerDevice):
|
||||
@property
|
||||
def media_content_type(self):
|
||||
"""Content type of current playing media."""
|
||||
return MEDIA_TYPE_GAME
|
||||
return self._media_type
|
||||
|
||||
@property
|
||||
def media_image_url(self):
|
||||
@@ -370,13 +384,18 @@ class PS4Device(MediaPlayerDevice):
|
||||
def select_source(self, source):
|
||||
"""Select input source."""
|
||||
for title_id, game in self._games.items():
|
||||
if source == game:
|
||||
if source.lower().encode(encoding='utf-8') == \
|
||||
game.lower().encode(encoding='utf-8') \
|
||||
or source == title_id:
|
||||
_LOGGER.debug(
|
||||
"Starting PS4 game %s (%s) using source %s",
|
||||
game, title_id, source)
|
||||
self._ps4.start_title(
|
||||
title_id, running_id=self._media_content_id)
|
||||
return
|
||||
_LOGGER.warning(
|
||||
"Could not start title. '%s' is not in source list", source)
|
||||
return
|
||||
|
||||
def send_command(self, command):
|
||||
"""Send Button Command."""
|
||||
|
||||
Reference in New Issue
Block a user