hass-core/homeassistant/components/ps4/config_flow.py
ktnrg45 72ef9670e6 Add component media player.ps4 (#21074)
* Added PS4/ __init__.py

* Create en.json

* Create config_flow.py

* Create const.py

* Create media_player.py

* Create services.yaml

* Create strings.json

* Create __init__.py

* Add test_config_flow.py/ Finished adding PS4 files

* Rewrote for loop into short-hand

* bumped pyps4 to 0.2.8

* Pass in helper()

* Rewrite func

* Fixed test

* Added import in init

* bump to 0.2.9

* bump to 0.3.0

* Removed line

* lint

* Add ps4 to flows list

* Added pyps4-homeassistant with script

* Added pyps4

* Added pypys4 to test

* removed list def

* reformatted service call dicts

* removed config from device class

* typo

* removed line

* reformatted .. format

* redefined property

* reformat load games func

* Add __init__ and media_player.py to coveragerc

* Fix for test

* remove init

* remove blank line

* remove mock_coro

* Revert "remove init"

This reverts commit b68996aa34699bf38781e153acdd597579e8131f.

* Correct permissions

* fixes

* fixes
2019-02-17 15:41:55 -05:00

123 lines
4 KiB
Python

"""Config Flow for PlayStation 4."""
from collections import OrderedDict
import logging
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components.ps4.const import (
DEFAULT_NAME, DEFAULT_REGION, DOMAIN, REGIONS)
from homeassistant.const import (
CONF_CODE, CONF_HOST, CONF_IP_ADDRESS, CONF_NAME, CONF_REGION, CONF_TOKEN)
_LOGGER = logging.getLogger(__name__)
UDP_PORT = 987
TCP_PORT = 997
PORT_MSG = {UDP_PORT: 'port_987_bind_error', TCP_PORT: 'port_997_bind_error'}
@config_entries.HANDLERS.register(DOMAIN)
class PlayStation4FlowHandler(config_entries.ConfigFlow):
"""Handle a PlayStation 4 config flow."""
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
def __init__(self):
"""Initialize the config flow."""
from pyps4_homeassistant import Helper
self.helper = Helper()
self.creds = None
self.name = None
self.host = None
self.region = None
self.pin = None
async def async_step_user(self, user_input=None):
"""Handle a user config flow."""
# Abort if device is configured.
if self.hass.config_entries.async_entries(DOMAIN):
return self.async_abort(reason='devices_configured')
# Check if able to bind to ports: UDP 987, TCP 997.
ports = PORT_MSG.keys()
failed = await self.hass.async_add_executor_job(
self.helper.port_bind, ports)
if failed in ports:
reason = PORT_MSG[failed]
return self.async_abort(reason=reason)
return await self.async_step_creds()
async def async_step_creds(self, user_input=None):
"""Return PS4 credentials from 2nd Screen App."""
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_link()
return self.async_abort(reason='credential_error')
return self.async_show_form(
step_id='creds')
async def async_step_link(self, user_input=None):
"""Prompt user input. Create or edit entry."""
errors = {}
# Search for device.
devices = await self.hass.async_add_executor_job(
self.helper.has_devices)
# 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]
# 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.host = user_input[CONF_IP_ADDRESS]
is_ready, is_login = await self.hass.async_add_executor_job(
self.helper.link, self.host, self.creds, self.pin)
if is_ready is False:
errors['base'] = 'not_ready'
elif is_login is False:
errors['base'] = 'login_failed'
else:
device = {
CONF_HOST: self.host,
CONF_NAME: self.name,
CONF_REGION: self.region
}
# Create entry.
return self.async_create_entry(
title='PlayStation 4',
data={
CONF_TOKEN: self.creds,
'devices': [device],
},
)
# 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_NAME, default=DEFAULT_NAME)] = str
return self.async_show_form(
step_id='link',
data_schema=vol.Schema(link_schema),
errors=errors,
)