88from typing import Any
99
1010from aiohasupervisor import SupervisorError
11+ from aiohasupervisor .models import Discovery
1112from aiohttp import web
1213from aiohttp .web_exceptions import HTTPServiceUnavailable
1314
1920from homeassistant .helpers import discovery_flow
2021from homeassistant .helpers .dispatcher import async_dispatcher_connect
2122
22- from .const import ATTR_ADDON , ATTR_CONFIG , ATTR_DISCOVERY , ATTR_UUID , DOMAIN
23- from .handler import HassIO , HassioAPIError , get_supervisor_client
23+ from .const import ATTR_ADDON , ATTR_UUID , DOMAIN
24+ from .handler import HassIO , get_supervisor_client
2425
2526_LOGGER = logging .getLogger (__name__ )
2627
@@ -39,20 +40,21 @@ class HassioServiceInfo(BaseServiceInfo):
3940def async_setup_discovery_view (hass : HomeAssistant , hassio : HassIO ) -> None :
4041 """Discovery setup."""
4142 hassio_discovery = HassIODiscovery (hass , hassio )
43+ supervisor_client = get_supervisor_client (hass )
4244 hass .http .register_view (hassio_discovery )
4345
4446 # Handle exists discovery messages
4547 async def _async_discovery_start_handler (event : Event ) -> None :
4648 """Process all exists discovery on startup."""
4749 try :
48- data = await hassio . retrieve_discovery_messages ()
49- except HassioAPIError as err :
50+ data = await supervisor_client . discovery . list ()
51+ except SupervisorError as err :
5052 _LOGGER .error ("Can't read discover info: %s" , err )
5153 return
5254
5355 jobs = [
5456 asyncio .create_task (hassio_discovery .async_process_new (discovery ))
55- for discovery in data [ ATTR_DISCOVERY ]
57+ for discovery in data
5658 ]
5759 if jobs :
5860 await asyncio .wait (jobs )
@@ -95,8 +97,8 @@ async def post(self, request: web.Request, uuid: str) -> web.Response:
9597 """Handle new discovery requests."""
9698 # Fetch discovery data and prevent injections
9799 try :
98- data = await self .hassio . get_discovery_message (uuid )
99- except HassioAPIError as err :
100+ data = await self ._supervisor_client . discovery . get (uuid )
101+ except SupervisorError as err :
100102 _LOGGER .error ("Can't read discovery data: %s" , err )
101103 raise HTTPServiceUnavailable from None
102104
@@ -113,52 +115,50 @@ async def delete(self, request: web.Request, uuid: str) -> web.Response:
113115 async def async_rediscover (self , uuid : str ) -> None :
114116 """Rediscover add-on when config entry is removed."""
115117 try :
116- data = await self .hassio . get_discovery_message (uuid )
117- except HassioAPIError as err :
118+ data = await self ._supervisor_client . discovery . get (uuid )
119+ except SupervisorError as err :
118120 _LOGGER .debug ("Can't read discovery data: %s" , err )
119121 else :
120122 await self .async_process_new (data )
121123
122- async def async_process_new (self , data : dict [ str , Any ] ) -> None :
124+ async def async_process_new (self , data : Discovery ) -> None :
123125 """Process add discovery entry."""
124- service : str = data [ATTR_SERVICE ]
125- config_data : dict [str , Any ] = data [ATTR_CONFIG ]
126- slug : str = data [ATTR_ADDON ]
127- uuid : str = data [ATTR_UUID ]
128-
129126 # Read additional Add-on info
130127 try :
131- addon_info = await self ._supervisor_client .addons .addon_info (slug )
128+ addon_info = await self ._supervisor_client .addons .addon_info (data . addon )
132129 except SupervisorError as err :
133130 _LOGGER .error ("Can't read add-on info: %s" , err )
134131 return
135132
136- config_data [ATTR_ADDON ] = addon_info .name
133+ data . config [ATTR_ADDON ] = addon_info .name
137134
138135 # Use config flow
139136 discovery_flow .async_create_flow (
140137 self .hass ,
141- service ,
138+ data . service ,
142139 context = {"source" : config_entries .SOURCE_HASSIO },
143140 data = HassioServiceInfo (
144- config = config_data , name = addon_info .name , slug = slug , uuid = uuid
141+ config = data .config ,
142+ name = addon_info .name ,
143+ slug = data .addon ,
144+ uuid = data .uuid ,
145145 ),
146146 discovery_key = discovery_flow .DiscoveryKey (
147147 domain = DOMAIN ,
148- key = data [ ATTR_UUID ] ,
148+ key = data . uuid ,
149149 version = 1 ,
150150 ),
151151 )
152152
153153 async def async_process_del (self , data : dict [str , Any ]) -> None :
154154 """Process remove discovery entry."""
155- service = data [ATTR_SERVICE ]
156- uuid = data [ATTR_UUID ]
155+ service : str = data [ATTR_SERVICE ]
156+ uuid : str = data [ATTR_UUID ]
157157
158158 # Check if really deletet / prevent injections
159159 try :
160- data = await self .hassio . get_discovery_message (uuid )
161- except HassioAPIError :
160+ data = await self ._supervisor_client . discovery . get (uuid )
161+ except SupervisorError :
162162 pass
163163 else :
164164 _LOGGER .warning ("Retrieve wrong unload for %s" , service )
0 commit comments