77from homeassistant .components .notify import ATTR_TARGET , BaseNotificationService
88from homeassistant .core import HomeAssistant
99from homeassistant .helpers .typing import ConfigType , DiscoveryInfoType
10+ from psnawp_api .core .psnawp_exceptions import PSNAWPNotFound
1011
1112from .const import DOMAIN , PSN_API
1213
@@ -23,15 +24,16 @@ def get_service(
2324 return None
2425
2526 data = hass .data [DOMAIN ][discovery_info .get ("entry_id" )][PSN_API ]
26- return PsnNotificationService (data )
27+ return PsnNotificationService (data , hass )
2728
2829
2930class PsnNotificationService (BaseNotificationService ):
3031 """Implement the notification service for the PSN Network."""
3132
32- def __init__ (self , psn ) -> None :
33+ def __init__ (self , psn , hass ) -> None :
3334 """Initialize the service."""
3435 self .psn = psn
36+ self .hass = hass
3537
3638 async def async_send_message (self , message = "" , ** kwargs ):
3739 """Send a message."""
@@ -42,9 +44,19 @@ async def async_send_message(self, message="", **kwargs):
4244 raise ValueError ("Missing required argument: target" )
4345
4446 for user in users :
45- individual = await self .psn .user (online_id = user )
46- user_list .append (individual )
47-
48- group = await self .psn .group (users_list = user_list )
49- response = await group .send_message (message )
47+ try :
48+ individual = await self .hass .async_add_executor_job (
49+ lambda : self .psn .user (online_id = user )
50+ )
51+ user_list .append (individual )
52+ except PSNAWPNotFound as err :
53+ _LOGGER .error ("User not found: %s" , user )
54+ raise err
55+
56+ group = await self .hass .async_add_executor_job (
57+ lambda : self .psn .group (users_list = user_list )
58+ )
59+ response = await self .hass .async_add_executor_job (
60+ lambda : group .send_message (message )
61+ )
5062 _LOGGER .debug ("Send Message Response: %s" , response )
0 commit comments