Add support for multiple devices for PS4 component (#21302)

* Support multiple devices.

* Revert "Support multiple devices."

This reverts commit 3f5d4462a98da13ebb1ab1c07d341dbd7020e6cc.

* Support multiple devices

* Bump to 0.3.3

* bump 0.3.4

* Add tests for multiple devices.

* Update Requirements

* Update config_flow.py

* Update config_flow.py

* fixed typo

* Reordered functions

* Added multiple flow implementation test.

* fix

* typo

* fix tests

* bump 0.4.0

* Bump 0.4.0

* 0.4.0

* bump version

* bump version

* bump version

* Add keep alive feature with multiple devices

* bump version

* bump version

* bump version

* bump 0.4.7

* bump 0.4.7

* bump 0.4.7

* Edited tests.

* bump/pylint

* pylint

* bump/pylint

* bump/pylint

* Change to add additional entry

* Changed to multiple entries

* pylint

* Corrections to manage multiple devices.

* lint

* comments

* Removed redundant for loop

* Shorthand correction

* Remove reference to private object

* Test fix

* Revert changes. Test failure.

* Test fix

* test fix

* unindent assertions

* pylint
This commit is contained in:
ktnrg45 authored and Martin Hjelmare committed 2019-03-05 01:48:25 +01:00
1 parent df25128923
commit efa5d5dfe3
6 files changed
+214 -34

No files matched your search

+1 -1
View File
@@ -11,7 +11,7 @@ from homeassistant.components.ps4.const import DOMAIN # noqa: pylint: disable=u
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['pyps4-homeassistant==0.3.0']
REQUIREMENTS = ['pyps4-homeassistant==0.4.8']
async def async_setup(hass, config):
+15 -4
View File
@@ -37,10 +37,6 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
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(
@@ -48,6 +44,9 @@ 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_link()
return await self.async_step_creds()
async def async_step_creds(self, user_input=None):
@@ -78,6 +77,18 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow):
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):
for entry in self.hass.config_entries.async_entries(DOMAIN):
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')
# Login to PS4 with user data.
if user_input is not None:
self.region = user_input[CONF_REGION]
+24 -2
View File
@@ -133,6 +133,7 @@ class PS4Device(MediaPlayerDevice):
self._retry = 0
self._info = None
self._unique_id = None
self._power_on = False
async def async_added_to_hass(self):
"""Subscribe PS4 events."""
@@ -144,6 +145,7 @@ class PS4Device(MediaPlayerDevice):
try:
status = self._ps4.get_status()
if self._info is None:
# Add entity to registry
self.get_device_info(status)
self._games = self.load_games()
if self._games is not None:
@@ -153,6 +155,17 @@ class PS4Device(MediaPlayerDevice):
if status is not None:
self._retry = 0
if status.get('status') == 'Ok':
# Check if only 1 device in Hass.
if len(self.hass.data[PS4_DATA].devices) == 1:
# Enable keep alive feature for PS4 Connection.
# Only 1 device is supported, Since have to use port 997.
self._ps4.keep_alive = True
else:
self._ps4.keep_alive = False
if self._power_on:
# Auto Login after Turn On.
self._ps4.open()
self._power_on = False
title_id = status.get('running-app-titleid')
name = status.get('running-app-name')
if title_id and name is not None:
@@ -268,6 +281,10 @@ class PS4Device(MediaPlayerDevice):
}
self._unique_id = status['host-id']
async def async_will_remove_from_hass(self):
"""Remove Entity from Hass."""
self.hass.data[PS4_DATA].devices.remove(self)
@property
def device_info(self):
"""Return information about the device."""
@@ -346,15 +363,16 @@ class PS4Device(MediaPlayerDevice):
def turn_on(self):
"""Turn on the media player."""
self._power_on = True
self._ps4.wakeup()
def media_pause(self):
"""Send keypress ps to return to menu."""
self._ps4.remote_control('ps')
self.send_remote_control('ps')
def media_stop(self):
"""Send keypress ps to return to menu."""
self._ps4.remote_control('ps')
self.send_remote_control('ps')
def select_source(self, source):
"""Select input source."""
@@ -369,4 +387,8 @@ class PS4Device(MediaPlayerDevice):
def send_command(self, command):
"""Send Button Command."""
self.send_remote_control(command)
def send_remote_control(self, command):
"""Send RC command."""
self._ps4.remote_control(command)