From b3380261d78c29611dafde57a4d9a63f4193ceae Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 26 Jan 2023 17:20:52 +0100 Subject: [PATCH] Add hints to get_service in azure service bus (#86694) --- .../components/azure_service_bus/notify.py | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/azure_service_bus/notify.py b/homeassistant/components/azure_service_bus/notify.py index 53873373011..4005460ecae 100644 --- a/homeassistant/components/azure_service_bus/notify.py +++ b/homeassistant/components/azure_service_bus/notify.py @@ -1,9 +1,11 @@ """Support for azure service bus notification.""" +from __future__ import annotations + import json import logging from azure.servicebus import ServiceBusMessage -from azure.servicebus.aio import ServiceBusClient +from azure.servicebus.aio import ServiceBusClient, ServiceBusSender from azure.servicebus.exceptions import ( MessagingEntityNotFoundError, ServiceBusConnectionError, @@ -19,7 +21,9 @@ from homeassistant.components.notify import ( BaseNotificationService, ) from homeassistant.const import CONTENT_TYPE_JSON +from homeassistant.core import HomeAssistant import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType CONF_CONNECTION_STRING = "connection_string" CONF_QUEUE_NAME = "queue" @@ -47,11 +51,15 @@ PLATFORM_SCHEMA = vol.All( _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.""" - connection_string = config[CONF_CONNECTION_STRING] - queue_name = config.get(CONF_QUEUE_NAME) - topic_name = config.get(CONF_TOPIC_NAME) + connection_string: str = config[CONF_CONNECTION_STRING] + queue_name: str | None = config.get(CONF_QUEUE_NAME) + topic_name: str | None = config.get(CONF_TOPIC_NAME) # Library can do synchronous IO when creating the clients. # 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 ) + client: ServiceBusSender | None = None try: if queue_name: client = servicebus.get_queue_sender(queue_name) - else: + elif topic_name: client = servicebus.get_topic_sender(topic_name) except (ServiceBusConnectionError, MessagingEntityNotFoundError) as err: _LOGGER.error( @@ -72,7 +81,7 @@ def get_service(hass, config, discovery_info=None): ) return None - return ServiceBusNotificationService(client) + return ServiceBusNotificationService(client) if client else None class ServiceBusNotificationService(BaseNotificationService):