mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Improve PS4 media art fetching and config flow (#22167)
* improved config flow * Added errors, docs url * Added errors, docs url * Added manual config mode * Add tests for manual/auto host input * fix inline docs * fix inline docs * Changed region list * Added deprecated region message * removed DEFAULT_REGION * Added close method * Fixes * Update const.py * Update const.py * Update const.py * Update test_config_flow.py * Added invalid pin errors * Update strings.json * Update strings.json * bump pyps4 to 0.5.0 * Bump pyps4 0.5.0 * Bump pyps4 to 0.5.0 * test fixes * pylint * Change error reference * remove pin messages * remove pin messages * Update en.json * remove pin tests * fix tests * update vol * Vol fix * Update config_flow.py * Add migration for v1 entry * lint * fixes * typo * fix * Update config_flow.py * Fix vol * Executor job for io method. * Update __init__.py * blank line * Update __init__.py * Update tests/components/ps4/test_config_flow.py Co-Authored-By: ktnrg45 <38207570+ktnrg45@users.noreply.github.com>
This commit is contained in:
@@ -8,10 +8,14 @@ from homeassistant import config_entries
|
||||
from homeassistant.const import (
|
||||
CONF_CODE, CONF_HOST, CONF_IP_ADDRESS, CONF_NAME, CONF_REGION, CONF_TOKEN)
|
||||
|
||||
from .const import DEFAULT_NAME, DEFAULT_REGION, DOMAIN, REGIONS
|
||||
from .const import DEFAULT_NAME, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_MODE = 'Config Mode'
|
||||
CONF_AUTO = "Auto Discover"
|
||||
CONF_MANUAL = "Manual Entry"
|
||||
|
||||
UDP_PORT = 987
|
||||
TCP_PORT = 997
|
||||
PORT_MSG = {UDP_PORT: 'port_987_bind_error', TCP_PORT: 'port_997_bind_error'}
|
||||
@@ -21,7 +25,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 = 1
|
||||
VERSION = 2
|
||||
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
|
||||
|
||||
def __init__(self):
|
||||
@@ -34,6 +38,8 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
|
||||
self.host = None
|
||||
self.region = None
|
||||
self.pin = None
|
||||
self.m_device = None
|
||||
self.device_list = []
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""Handle a user config flow."""
|
||||
@@ -46,7 +52,7 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
|
||||
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_link()
|
||||
return await self.async_step_mode()
|
||||
return await self.async_step_creds()
|
||||
|
||||
async def async_step_creds(self, user_input=None):
|
||||
@@ -56,53 +62,82 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
|
||||
self.helper.get_creds)
|
||||
|
||||
if self.creds is not None:
|
||||
return await self.async_step_link()
|
||||
return await self.async_step_mode()
|
||||
return self.async_abort(reason='credential_error')
|
||||
|
||||
return self.async_show_form(
|
||||
step_id='creds')
|
||||
|
||||
async def async_step_mode(self, user_input=None):
|
||||
"""Prompt for mode."""
|
||||
errors = {}
|
||||
mode = [CONF_AUTO, CONF_MANUAL]
|
||||
|
||||
if user_input is not None:
|
||||
if user_input[CONF_MODE] == CONF_MANUAL:
|
||||
try:
|
||||
device = user_input[CONF_IP_ADDRESS]
|
||||
if device:
|
||||
self.m_device = device
|
||||
except KeyError:
|
||||
errors[CONF_IP_ADDRESS] = 'no_ipaddress'
|
||||
if not errors:
|
||||
return await self.async_step_link()
|
||||
|
||||
mode_schema = OrderedDict()
|
||||
mode_schema[vol.Required(
|
||||
CONF_MODE, default=CONF_AUTO)] = vol.In(list(mode))
|
||||
mode_schema[vol.Optional(CONF_IP_ADDRESS)] = str
|
||||
|
||||
return self.async_show_form(
|
||||
step_id='mode',
|
||||
data_schema=vol.Schema(mode_schema),
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def async_step_link(self, user_input=None):
|
||||
"""Prompt user input. Create or edit entry."""
|
||||
from pyps4_homeassistant.media_art import COUNTRIES
|
||||
regions = sorted(COUNTRIES.keys())
|
||||
errors = {}
|
||||
|
||||
# Search for device.
|
||||
devices = await self.hass.async_add_executor_job(
|
||||
self.helper.has_devices)
|
||||
if user_input is None:
|
||||
# Search for device.
|
||||
devices = await self.hass.async_add_executor_job(
|
||||
self.helper.has_devices, self.m_device)
|
||||
|
||||
# Abort if can't find device.
|
||||
if not devices:
|
||||
return self.async_abort(reason='no_devices_found')
|
||||
# Abort if can't find device.
|
||||
if not devices:
|
||||
return self.async_abort(reason='no_devices_found')
|
||||
|
||||
device_list = [
|
||||
device['host-ip'] for device in devices]
|
||||
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 device_list:
|
||||
# Remove configured device from search list.
|
||||
device_list.remove(c_device['host'])
|
||||
# If list is empty then all devices are configured.
|
||||
if not 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']
|
||||
# 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'])
|
||||
# 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:
|
||||
self.region = user_input[CONF_REGION]
|
||||
self.name = user_input[CONF_NAME]
|
||||
self.pin = user_input[CONF_CODE]
|
||||
self.pin = str(user_input[CONF_CODE])
|
||||
self.host = user_input[CONF_IP_ADDRESS]
|
||||
|
||||
is_ready, is_login = await self.hass.async_add_executor_job(
|
||||
@@ -130,10 +165,11 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
|
||||
|
||||
# Show User Input form.
|
||||
link_schema = OrderedDict()
|
||||
link_schema[vol.Required(CONF_IP_ADDRESS)] = vol.In(list(device_list))
|
||||
link_schema[vol.Required(
|
||||
CONF_REGION, default=DEFAULT_REGION)] = vol.In(list(REGIONS))
|
||||
link_schema[vol.Required(CONF_CODE)] = str
|
||||
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_CODE)] = vol.All(
|
||||
vol.Strip, vol.Length(min=8, max=8), vol.Coerce(int))
|
||||
link_schema[vol.Required(CONF_NAME, default=DEFAULT_NAME)] = str
|
||||
|
||||
return self.async_show_form(
|
||||
|
||||
Reference in New Issue
Block a user