1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +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:
ktnrg45
2019-04-23 16:32:36 -07:00
committed by Martin Hjelmare
parent d505f1c5f2
commit 68d3e624e6
9 changed files with 157 additions and 47 deletions

View File

@@ -7,6 +7,7 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import (
CONF_CODE, CONF_HOST, CONF_IP_ADDRESS, CONF_NAME, CONF_REGION, CONF_TOKEN)
from homeassistant.util import location
from .const import DEFAULT_NAME, DOMAIN
@@ -25,7 +26,7 @@ PORT_MSG = {UDP_PORT: 'port_987_bind_error', TCP_PORT: 'port_997_bind_error'}
class PlayStation4FlowHandler(config_entries.ConfigFlow):
"""Handle a PlayStation 4 config flow."""
VERSION = 2
VERSION = 3
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
def __init__(self):
@@ -39,6 +40,7 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
self.region = None
self.pin = None
self.m_device = None
self.location = None
self.device_list = []
async def async_step_user(self, user_input=None):
@@ -50,23 +52,25 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
if failed in ports:
reason = PORT_MSG[failed]
return self.async_abort(reason=reason)
# Skip Creds Step if a device is configured.
if self.hass.config_entries.async_entries(DOMAIN):
return await self.async_step_mode()
return await self.async_step_creds()
async def async_step_creds(self, user_input=None):
"""Return PS4 credentials from 2nd Screen App."""
from pyps4_homeassistant.errors import CredentialTimeout
errors = {}
if user_input is not None:
self.creds = await self.hass.async_add_executor_job(
self.helper.get_creds)
if self.creds is not None:
return await self.async_step_mode()
return self.async_abort(reason='credential_error')
try:
self.creds = await self.hass.async_add_executor_job(
self.helper.get_creds)
if self.creds is not None:
return await self.async_step_mode()
return self.async_abort(reason='credential_error')
except CredentialTimeout:
errors['base'] = 'credential_timeout'
return self.async_show_form(
step_id='creds')
step_id='creds',
errors=errors)
async def async_step_mode(self, user_input=None):
"""Prompt for mode."""
@@ -99,6 +103,7 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
"""Prompt user input. Create or edit entry."""
from pyps4_homeassistant.media_art import COUNTRIES
regions = sorted(COUNTRIES.keys())
default_region = None
errors = {}
if user_input is None:
@@ -112,26 +117,23 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
self.device_list = [device['host-ip'] for device in devices]
# If entry exists check that devices found aren't configured.
if self.hass.config_entries.async_entries(DOMAIN):
creds = {}
for entry in self.hass.config_entries.async_entries(DOMAIN):
# Retrieve creds from entry
creds['data'] = entry.data[CONF_TOKEN]
# Retrieve device data from entry
conf_devices = entry.data['devices']
for c_device in conf_devices:
if c_device['host'] in self.device_list:
# Remove configured device from search list.
self.device_list.remove(c_device['host'])
# Check that devices found aren't configured per account.
entries = self.hass.config_entries.async_entries(DOMAIN)
if entries:
# Retrieve device data from all entries if creds match.
conf_devices = [device for entry in entries
if self.creds == entry.data[CONF_TOKEN]
for device in entry.data['devices']]
# Remove configured device from search list.
for c_device in conf_devices:
if c_device['host'] in self.device_list:
# Remove configured device from search list.
self.device_list.remove(c_device['host'])
# If list is empty then all devices are configured.
if not self.device_list:
return self.async_abort(reason='devices_configured')
# Add existing creds for linking. Should be only 1.
if not creds:
# Abort if creds is missing.
return self.async_abort(reason='credential_error')
self.creds = creds['data']
# Login to PS4 with user data.
if user_input is not None:
@@ -163,11 +165,21 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
},
)
# Try to find region automatically.
if not self.location:
self.location = await self.hass.async_add_executor_job(
location.detect_location_info)
if self.location:
country = self.location.country_name
if country in COUNTRIES:
default_region = country
# Show User Input form.
link_schema = OrderedDict()
link_schema[vol.Required(CONF_IP_ADDRESS)] = vol.In(
list(self.device_list))
link_schema[vol.Required(CONF_REGION)] = vol.In(list(regions))
link_schema[vol.Required(
CONF_REGION, default=default_region)] = vol.In(list(regions))
link_schema[vol.Required(CONF_CODE)] = vol.All(
vol.Strip, vol.Length(min=8, max=8), vol.Coerce(int))
link_schema[vol.Required(CONF_NAME, default=DEFAULT_NAME)] = str