mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Ps4 move send_command service to init (#25094)
* Move services from media_player * Move services to init * add COMMANDS to const * change service handler to sync
This commit is contained in:
parent
69cc6affd5
commit
bbe45cbd4b
@ -1,16 +1,27 @@
|
|||||||
"""Support for PlayStation 4 consoles."""
|
"""Support for PlayStation 4 consoles."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.const import (
|
||||||
|
ATTR_COMMAND, ATTR_ENTITY_ID, CONF_REGION, CONF_TOKEN)
|
||||||
from homeassistant.core import split_entity_id
|
from homeassistant.core import split_entity_id
|
||||||
from homeassistant.const import CONF_REGION, CONF_TOKEN
|
from homeassistant.helpers import entity_registry, config_validation as cv
|
||||||
from homeassistant.helpers import entity_registry
|
from homeassistant.helpers.typing import HomeAssistantType
|
||||||
from homeassistant.util import location
|
from homeassistant.util import location
|
||||||
|
|
||||||
from .config_flow import PlayStation4FlowHandler # noqa: pylint: disable=unused-import
|
from .config_flow import PlayStation4FlowHandler # noqa: pylint: disable=unused-import
|
||||||
from .const import DOMAIN, PS4_DATA # noqa: pylint: disable=unused-import
|
from .const import COMMANDS, DOMAIN, PS4_DATA
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
SERVICE_COMMAND = 'send_command'
|
||||||
|
|
||||||
|
PS4_COMMAND_SCHEMA = vol.Schema({
|
||||||
|
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
|
||||||
|
vol.Required(ATTR_COMMAND): vol.In(list(COMMANDS))
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
class PS4Data():
|
class PS4Data():
|
||||||
"""Init Data Class."""
|
"""Init Data Class."""
|
||||||
@ -30,6 +41,7 @@ async def async_setup(hass, config):
|
|||||||
transport, protocol = await async_create_ddp_endpoint()
|
transport, protocol = await async_create_ddp_endpoint()
|
||||||
hass.data[PS4_DATA].protocol = protocol
|
hass.data[PS4_DATA].protocol = protocol
|
||||||
_LOGGER.debug("PS4 DDP endpoint created: %s, %s", transport, protocol)
|
_LOGGER.debug("PS4 DDP endpoint created: %s, %s", transport, protocol)
|
||||||
|
service_handle(hass)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -124,3 +136,18 @@ def format_unique_id(creds, mac_address):
|
|||||||
"""Use last 4 Chars of credential as suffix. Unique ID per PSN user."""
|
"""Use last 4 Chars of credential as suffix. Unique ID per PSN user."""
|
||||||
suffix = creds[-4:]
|
suffix = creds[-4:]
|
||||||
return "{}_{}".format(mac_address, suffix)
|
return "{}_{}".format(mac_address, suffix)
|
||||||
|
|
||||||
|
|
||||||
|
def service_handle(hass: HomeAssistantType):
|
||||||
|
"""Handle for services."""
|
||||||
|
async def async_service_command(call):
|
||||||
|
"""Service for sending commands."""
|
||||||
|
entity_ids = call.data[ATTR_ENTITY_ID]
|
||||||
|
command = call.data[ATTR_COMMAND]
|
||||||
|
for device in hass.data[PS4_DATA].devices:
|
||||||
|
if device.entity_id in entity_ids:
|
||||||
|
await device.async_send_command(command)
|
||||||
|
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN, SERVICE_COMMAND, async_service_command,
|
||||||
|
schema=PS4_COMMAND_SCHEMA)
|
||||||
|
@ -5,5 +5,8 @@ DEFAULT_ALIAS = 'Home-Assistant'
|
|||||||
DOMAIN = 'ps4'
|
DOMAIN = 'ps4'
|
||||||
PS4_DATA = 'ps4_data'
|
PS4_DATA = 'ps4_data'
|
||||||
|
|
||||||
|
COMMANDS = (
|
||||||
|
'up', 'down', 'right', 'left', 'enter', 'back', 'option', 'ps')
|
||||||
|
|
||||||
# Deprecated used for logger/backwards compatibility from 0.89
|
# Deprecated used for logger/backwards compatibility from 0.89
|
||||||
REGIONS = ['R1', 'R2', 'R3', 'R4', 'R5']
|
REGIONS = ['R1', 'R2', 'R3', 'R4', 'R5']
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
import voluptuous as vol
|
|
||||||
|
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.components.media_player import (
|
from homeassistant.components.media_player import (
|
||||||
ENTITY_IMAGE_URL, MediaPlayerDevice)
|
ENTITY_IMAGE_URL, MediaPlayerDevice)
|
||||||
@ -12,9 +10,8 @@ from homeassistant.components.media_player.const import (
|
|||||||
SUPPORT_PAUSE, SUPPORT_STOP, SUPPORT_TURN_OFF, SUPPORT_TURN_ON)
|
SUPPORT_PAUSE, SUPPORT_STOP, SUPPORT_TURN_OFF, SUPPORT_TURN_ON)
|
||||||
from homeassistant.components.ps4 import format_unique_id
|
from homeassistant.components.ps4 import format_unique_id
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_COMMAND, ATTR_ENTITY_ID, CONF_HOST, CONF_NAME, CONF_REGION,
|
CONF_HOST, CONF_NAME, CONF_REGION,
|
||||||
CONF_TOKEN, STATE_IDLE, STATE_OFF, STATE_PLAYING)
|
CONF_TOKEN, STATE_IDLE, STATE_OFF, STATE_PLAYING)
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
from homeassistant.helpers import device_registry, entity_registry
|
from homeassistant.helpers import device_registry, entity_registry
|
||||||
from homeassistant.util.json import load_json, save_json
|
from homeassistant.util.json import load_json, save_json
|
||||||
|
|
||||||
@ -30,24 +27,6 @@ ICON = 'mdi:playstation'
|
|||||||
GAMES_FILE = '.ps4-games.json'
|
GAMES_FILE = '.ps4-games.json'
|
||||||
MEDIA_IMAGE_DEFAULT = None
|
MEDIA_IMAGE_DEFAULT = None
|
||||||
|
|
||||||
COMMANDS = (
|
|
||||||
'up',
|
|
||||||
'down',
|
|
||||||
'right',
|
|
||||||
'left',
|
|
||||||
'enter',
|
|
||||||
'back',
|
|
||||||
'option',
|
|
||||||
'ps',
|
|
||||||
)
|
|
||||||
|
|
||||||
SERVICE_COMMAND = 'send_command'
|
|
||||||
|
|
||||||
PS4_COMMAND_SCHEMA = vol.Schema({
|
|
||||||
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
|
|
||||||
vol.Required(ATTR_COMMAND): vol.In(list(COMMANDS))
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up PS4 from a config entry."""
|
"""Set up PS4 from a config entry."""
|
||||||
@ -55,21 +34,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
await async_setup_platform(
|
await async_setup_platform(
|
||||||
hass, config, async_add_entities, discovery_info=None)
|
hass, config, async_add_entities, discovery_info=None)
|
||||||
|
|
||||||
async def async_service_handle(hass):
|
|
||||||
"""Handle for services."""
|
|
||||||
async def async_service_command(call):
|
|
||||||
entity_ids = call.data[ATTR_ENTITY_ID]
|
|
||||||
command = call.data[ATTR_COMMAND]
|
|
||||||
for device in hass.data[PS4_DATA].devices:
|
|
||||||
if device.entity_id in entity_ids:
|
|
||||||
await device.async_send_command(command)
|
|
||||||
|
|
||||||
hass.services.async_register(
|
|
||||||
PS4_DOMAIN, SERVICE_COMMAND, async_service_command,
|
|
||||||
schema=PS4_COMMAND_SCHEMA)
|
|
||||||
|
|
||||||
await async_service_handle(hass)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
hass, config, async_add_entities, discovery_info=None):
|
hass, config, async_add_entities, discovery_info=None):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user