Add hints to get_service in azure service bus (#86694)

This commit is contained in:
epenet 2023-01-26 17:20:52 +01:00 committed by GitHub
parent eb5d63237c
commit b3380261d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,11 @@
"""Support for azure service bus notification.""" """Support for azure service bus notification."""
from __future__ import annotations
import json import json
import logging import logging
from azure.servicebus import ServiceBusMessage from azure.servicebus import ServiceBusMessage
from azure.servicebus.aio import ServiceBusClient from azure.servicebus.aio import ServiceBusClient, ServiceBusSender
from azure.servicebus.exceptions import ( from azure.servicebus.exceptions import (
MessagingEntityNotFoundError, MessagingEntityNotFoundError,
ServiceBusConnectionError, ServiceBusConnectionError,
@ -19,7 +21,9 @@ from homeassistant.components.notify import (
BaseNotificationService, BaseNotificationService,
) )
from homeassistant.const import CONTENT_TYPE_JSON from homeassistant.const import CONTENT_TYPE_JSON
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
CONF_CONNECTION_STRING = "connection_string" CONF_CONNECTION_STRING = "connection_string"
CONF_QUEUE_NAME = "queue" CONF_QUEUE_NAME = "queue"
@ -47,11 +51,15 @@ PLATFORM_SCHEMA = vol.All(
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
def get_service(hass, config, discovery_info=None): def get_service(
hass: HomeAssistant,
config: ConfigType,
discovery_info: DiscoveryInfoType | None = None,
) -> ServiceBusNotificationService | None:
"""Get the notification service.""" """Get the notification service."""
connection_string = config[CONF_CONNECTION_STRING] connection_string: str = config[CONF_CONNECTION_STRING]
queue_name = config.get(CONF_QUEUE_NAME) queue_name: str | None = config.get(CONF_QUEUE_NAME)
topic_name = config.get(CONF_TOPIC_NAME) topic_name: str | None = config.get(CONF_TOPIC_NAME)
# Library can do synchronous IO when creating the clients. # Library can do synchronous IO when creating the clients.
# Passes in loop here, but can't run setup on the event loop. # Passes in loop here, but can't run setup on the event loop.
@ -59,10 +67,11 @@ def get_service(hass, config, discovery_info=None):
connection_string, loop=hass.loop connection_string, loop=hass.loop
) )
client: ServiceBusSender | None = None
try: try:
if queue_name: if queue_name:
client = servicebus.get_queue_sender(queue_name) client = servicebus.get_queue_sender(queue_name)
else: elif topic_name:
client = servicebus.get_topic_sender(topic_name) client = servicebus.get_topic_sender(topic_name)
except (ServiceBusConnectionError, MessagingEntityNotFoundError) as err: except (ServiceBusConnectionError, MessagingEntityNotFoundError) as err:
_LOGGER.error( _LOGGER.error(
@ -72,7 +81,7 @@ def get_service(hass, config, discovery_info=None):
) )
return None return None
return ServiceBusNotificationService(client) return ServiceBusNotificationService(client) if client else None
class ServiceBusNotificationService(BaseNotificationService): class ServiceBusNotificationService(BaseNotificationService):