From aec885a46704baa71537e30d01e4efe52af9faa5 Mon Sep 17 00:00:00 2001
From: Paulus Schoutsen <balloob@gmail.com>
Date: Fri, 29 Jul 2022 00:53:08 -0700
Subject: [PATCH] Fix Roon media player being set up before hass.data set up
 (#75904)

---
 homeassistant/components/roon/__init__.py | 11 ++++++++++-
 homeassistant/components/roon/server.py   |  9 +--------
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/homeassistant/components/roon/__init__.py b/homeassistant/components/roon/__init__.py
index 9e5c38f0211..9969b694895 100644
--- a/homeassistant/components/roon/__init__.py
+++ b/homeassistant/components/roon/__init__.py
@@ -1,12 +1,14 @@
 """Roon (www.roonlabs.com) component."""
 from homeassistant.config_entries import ConfigEntry
-from homeassistant.const import CONF_HOST
+from homeassistant.const import CONF_HOST, Platform
 from homeassistant.core import HomeAssistant
 from homeassistant.helpers import device_registry as dr
 
 from .const import CONF_ROON_NAME, DOMAIN
 from .server import RoonServer
 
+PLATFORMS = [Platform.MEDIA_PLAYER]
+
 
 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
     """Set up a roonserver from a config entry."""
@@ -28,10 +30,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
         manufacturer="Roonlabs",
         name=f"Roon Core ({name})",
     )
+
+    # initialize media_player platform
+    await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
+
     return True
 
 
 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
     """Unload a config entry."""
+    if not await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
+        return False
+
     roonserver = hass.data[DOMAIN].pop(entry.entry_id)
     return await roonserver.async_reset()
diff --git a/homeassistant/components/roon/server.py b/homeassistant/components/roon/server.py
index df9dec3d9af..997db44583d 100644
--- a/homeassistant/components/roon/server.py
+++ b/homeassistant/components/roon/server.py
@@ -4,7 +4,7 @@ import logging
 
 from roonapi import RoonApi, RoonDiscovery
 
-from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT, Platform
+from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT
 from homeassistant.helpers.dispatcher import async_dispatcher_send
 from homeassistant.util.dt import utcnow
 
@@ -13,7 +13,6 @@ from .const import CONF_ROON_ID, ROON_APPINFO
 _LOGGER = logging.getLogger(__name__)
 INITIAL_SYNC_INTERVAL = 5
 FULL_SYNC_INTERVAL = 30
-PLATFORMS = [Platform.MEDIA_PLAYER]
 
 
 class RoonServer:
@@ -53,7 +52,6 @@ class RoonServer:
             (host, port) = get_roon_host()
             return RoonApi(ROON_APPINFO, token, host, port, blocking_init=True)
 
-        hass = self.hass
         core_id = self.config_entry.data.get(CONF_ROON_ID)
 
         self.roonapi = await self.hass.async_add_executor_job(get_roon_api)
@@ -67,11 +65,6 @@ class RoonServer:
             core_id if core_id is not None else self.config_entry.data[CONF_HOST]
         )
 
-        # initialize media_player platform
-        await hass.config_entries.async_forward_entry_setups(
-            self.config_entry, PLATFORMS
-        )
-
         # Initialize Roon background polling
         asyncio.create_task(self.async_do_loop())