mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
PS4 move load_games and save_games helpers to init from media_player (#25127)
* Add constant for games_file * move load and save games to init from media_player * Move save and load games to init * Missed arg * missed arg
This commit is contained in:
parent
a147a189ca
commit
b77d060304
@ -2,6 +2,8 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
from pyps4_homeassistant.ddp import async_create_ddp_endpoint
|
||||||
|
from pyps4_homeassistant.media_art import COUNTRIES
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_COMMAND, ATTR_ENTITY_ID, CONF_REGION, CONF_TOKEN)
|
ATTR_COMMAND, ATTR_ENTITY_ID, CONF_REGION, CONF_TOKEN)
|
||||||
@ -9,9 +11,10 @@ from homeassistant.core import split_entity_id
|
|||||||
from homeassistant.helpers import entity_registry, config_validation as cv
|
from homeassistant.helpers import entity_registry, config_validation as cv
|
||||||
from homeassistant.helpers.typing import HomeAssistantType
|
from homeassistant.helpers.typing import HomeAssistantType
|
||||||
from homeassistant.util import location
|
from homeassistant.util import location
|
||||||
|
from homeassistant.util.json import load_json, save_json
|
||||||
|
|
||||||
from .config_flow import PlayStation4FlowHandler # noqa: pylint: disable=unused-import
|
from .config_flow import PlayStation4FlowHandler # noqa: pylint: disable=unused-import
|
||||||
from .const import COMMANDS, DOMAIN, PS4_DATA
|
from .const import COMMANDS, DOMAIN, GAMES_FILE, PS4_DATA
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -34,8 +37,6 @@ class PS4Data():
|
|||||||
|
|
||||||
async def async_setup(hass, config):
|
async def async_setup(hass, config):
|
||||||
"""Set up the PS4 Component."""
|
"""Set up the PS4 Component."""
|
||||||
from pyps4_homeassistant.ddp import async_create_ddp_endpoint
|
|
||||||
|
|
||||||
hass.data[PS4_DATA] = PS4Data()
|
hass.data[PS4_DATA] = PS4Data()
|
||||||
|
|
||||||
transport, protocol = await async_create_ddp_endpoint()
|
transport, protocol = await async_create_ddp_endpoint()
|
||||||
@ -61,8 +62,6 @@ async def async_unload_entry(hass, entry):
|
|||||||
|
|
||||||
async def async_migrate_entry(hass, entry):
|
async def async_migrate_entry(hass, entry):
|
||||||
"""Migrate old entry."""
|
"""Migrate old entry."""
|
||||||
from pyps4_homeassistant.media_art import COUNTRIES
|
|
||||||
|
|
||||||
config_entries = hass.config_entries
|
config_entries = hass.config_entries
|
||||||
data = entry.data
|
data = entry.data
|
||||||
version = entry.version
|
version = entry.version
|
||||||
@ -138,6 +137,32 @@ def format_unique_id(creds, mac_address):
|
|||||||
return "{}_{}".format(mac_address, suffix)
|
return "{}_{}".format(mac_address, suffix)
|
||||||
|
|
||||||
|
|
||||||
|
def load_games(hass: HomeAssistantType) -> dict:
|
||||||
|
"""Load games for sources."""
|
||||||
|
g_file = hass.config.path(GAMES_FILE)
|
||||||
|
try:
|
||||||
|
games = load_json(g_file)
|
||||||
|
|
||||||
|
# If file does not exist, create empty file.
|
||||||
|
except FileNotFoundError:
|
||||||
|
games = {}
|
||||||
|
save_games(hass, games)
|
||||||
|
return games
|
||||||
|
|
||||||
|
|
||||||
|
def save_games(hass: HomeAssistantType, games: dict):
|
||||||
|
"""Save games to file."""
|
||||||
|
g_file = hass.config.path(GAMES_FILE)
|
||||||
|
try:
|
||||||
|
save_json(g_file, games)
|
||||||
|
except OSError as error:
|
||||||
|
_LOGGER.error("Could not save game list, %s", error)
|
||||||
|
|
||||||
|
# Retry loading file
|
||||||
|
if games is None:
|
||||||
|
load_games(hass)
|
||||||
|
|
||||||
|
|
||||||
def service_handle(hass: HomeAssistantType):
|
def service_handle(hass: HomeAssistantType):
|
||||||
"""Handle for services."""
|
"""Handle for services."""
|
||||||
async def async_service_command(call):
|
async def async_service_command(call):
|
||||||
|
@ -3,6 +3,7 @@ DEFAULT_NAME = "PlayStation 4"
|
|||||||
DEFAULT_REGION = "United States"
|
DEFAULT_REGION = "United States"
|
||||||
DEFAULT_ALIAS = 'Home-Assistant'
|
DEFAULT_ALIAS = 'Home-Assistant'
|
||||||
DOMAIN = 'ps4'
|
DOMAIN = 'ps4'
|
||||||
|
GAMES_FILE = '.ps4-games.json'
|
||||||
PS4_DATA = 'ps4_data'
|
PS4_DATA = 'ps4_data'
|
||||||
|
|
||||||
COMMANDS = (
|
COMMANDS = (
|
||||||
|
@ -11,12 +11,12 @@ from homeassistant.components.media_player import (
|
|||||||
from homeassistant.components.media_player.const import (
|
from homeassistant.components.media_player.const import (
|
||||||
MEDIA_TYPE_GAME, MEDIA_TYPE_APP, SUPPORT_SELECT_SOURCE,
|
MEDIA_TYPE_GAME, MEDIA_TYPE_APP, SUPPORT_SELECT_SOURCE,
|
||||||
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, load_games, save_games)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
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)
|
||||||
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 .const import (DEFAULT_ALIAS, DOMAIN as PS4_DOMAIN, PS4_DATA,
|
from .const import (DEFAULT_ALIAS, DOMAIN as PS4_DOMAIN, PS4_DATA,
|
||||||
REGIONS as deprecated_regions)
|
REGIONS as deprecated_regions)
|
||||||
@ -27,7 +27,6 @@ SUPPORT_PS4 = SUPPORT_TURN_OFF | SUPPORT_TURN_ON | \
|
|||||||
SUPPORT_PAUSE | SUPPORT_STOP | SUPPORT_SELECT_SOURCE
|
SUPPORT_PAUSE | SUPPORT_STOP | SUPPORT_SELECT_SOURCE
|
||||||
|
|
||||||
ICON = 'mdi:playstation'
|
ICON = 'mdi:playstation'
|
||||||
GAMES_FILE = '.ps4-games.json'
|
|
||||||
MEDIA_IMAGE_DEFAULT = None
|
MEDIA_IMAGE_DEFAULT = None
|
||||||
|
|
||||||
DEFAULT_RETRIES = 2
|
DEFAULT_RETRIES = 2
|
||||||
@ -43,7 +42,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
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):
|
||||||
"""Set up PS4 Platform."""
|
"""Set up PS4 Platform."""
|
||||||
games_file = hass.config.path(GAMES_FILE)
|
|
||||||
creds = config.data[CONF_TOKEN]
|
creds = config.data[CONF_TOKEN]
|
||||||
device_list = []
|
device_list = []
|
||||||
for device in config.data['devices']:
|
for device in config.data['devices']:
|
||||||
@ -52,14 +50,14 @@ async def async_setup_platform(
|
|||||||
name = device[CONF_NAME]
|
name = device[CONF_NAME]
|
||||||
ps4 = pyps4.Ps4Async(host, creds, device_name=DEFAULT_ALIAS)
|
ps4 = pyps4.Ps4Async(host, creds, device_name=DEFAULT_ALIAS)
|
||||||
device_list.append(PS4Device(
|
device_list.append(PS4Device(
|
||||||
config, name, host, region, ps4, creds, games_file))
|
config, name, host, region, ps4, creds))
|
||||||
async_add_entities(device_list, update_before_add=True)
|
async_add_entities(device_list, update_before_add=True)
|
||||||
|
|
||||||
|
|
||||||
class PS4Device(MediaPlayerDevice):
|
class PS4Device(MediaPlayerDevice):
|
||||||
"""Representation of a PS4."""
|
"""Representation of a PS4."""
|
||||||
|
|
||||||
def __init__(self, config, name, host, region, ps4, creds, games_file):
|
def __init__(self, config, name, host, region, ps4, creds):
|
||||||
"""Initialize the ps4 device."""
|
"""Initialize the ps4 device."""
|
||||||
self._entry_id = config.entry_id
|
self._entry_id = config.entry_id
|
||||||
self._ps4 = ps4
|
self._ps4 = ps4
|
||||||
@ -68,7 +66,6 @@ class PS4Device(MediaPlayerDevice):
|
|||||||
self._region = region
|
self._region = region
|
||||||
self._creds = creds
|
self._creds = creds
|
||||||
self._state = None
|
self._state = None
|
||||||
self._games_filename = games_file
|
|
||||||
self._media_content_id = None
|
self._media_content_id = None
|
||||||
self._media_title = None
|
self._media_title = None
|
||||||
self._media_image = None
|
self._media_image = None
|
||||||
@ -149,7 +146,7 @@ class PS4Device(MediaPlayerDevice):
|
|||||||
status = self._ps4.status
|
status = self._ps4.status
|
||||||
|
|
||||||
if status is not None:
|
if status is not None:
|
||||||
self._games = self.load_games()
|
self._games = load_games(self.hass)
|
||||||
if self._games is not None:
|
if self._games is not None:
|
||||||
self._source_list = list(sorted(self._games.values()))
|
self._source_list = list(sorted(self._games.values()))
|
||||||
self._retry = 0
|
self._retry = 0
|
||||||
@ -254,41 +251,17 @@ class PS4Device(MediaPlayerDevice):
|
|||||||
|
|
||||||
if self._media_content_id not in self._games:
|
if self._media_content_id not in self._games:
|
||||||
self.add_games(self._media_content_id, self._media_title)
|
self.add_games(self._media_content_id, self._media_title)
|
||||||
self._games = self.load_games()
|
self._games = load_games(self.hass)
|
||||||
|
|
||||||
self._source_list = list(sorted(self._games.values()))
|
self._source_list = list(sorted(self._games.values()))
|
||||||
|
|
||||||
def load_games(self):
|
|
||||||
"""Load games for sources."""
|
|
||||||
g_file = self._games_filename
|
|
||||||
try:
|
|
||||||
games = load_json(g_file)
|
|
||||||
|
|
||||||
# If file does not exist, create empty file.
|
|
||||||
except FileNotFoundError:
|
|
||||||
games = {}
|
|
||||||
self.save_games(games)
|
|
||||||
return games
|
|
||||||
|
|
||||||
def save_games(self, games):
|
|
||||||
"""Save games to file."""
|
|
||||||
g_file = self._games_filename
|
|
||||||
try:
|
|
||||||
save_json(g_file, games)
|
|
||||||
except OSError as error:
|
|
||||||
_LOGGER.error("Could not save game list, %s", error)
|
|
||||||
|
|
||||||
# Retry loading file
|
|
||||||
if games is None:
|
|
||||||
self.load_games()
|
|
||||||
|
|
||||||
def add_games(self, title_id, app_name):
|
def add_games(self, title_id, app_name):
|
||||||
"""Add games to list."""
|
"""Add games to list."""
|
||||||
games = self._games
|
games = self._games
|
||||||
if title_id is not None and title_id not in games:
|
if title_id is not None and title_id not in games:
|
||||||
game = {title_id: app_name}
|
game = {title_id: app_name}
|
||||||
games.update(game)
|
games.update(game)
|
||||||
self.save_games(games)
|
save_games(self.hass, games)
|
||||||
|
|
||||||
async def async_get_device_info(self, status):
|
async def async_get_device_info(self, status):
|
||||||
"""Set device info for registry."""
|
"""Set device info for registry."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user