Guard client connection asserts with if TYPE_CHECKING (#659)

This commit is contained in:
J. Nick Koston 2023-11-22 21:54:01 +01:00 committed by GitHub
parent d8cace0b26
commit 77a7c5eaf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -388,6 +388,7 @@ class APIClient:
login: bool = False, login: bool = False,
) -> None: ) -> None:
"""Finish connecting to the device.""" """Finish connecting to the device."""
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
try: try:
await self._connection.finish_connection(login=login) await self._connection.finish_connection(login=login)
@ -423,6 +424,7 @@ class APIClient:
async def device_info(self) -> DeviceInfo: async def device_info(self) -> DeviceInfo:
self._check_authenticated() self._check_authenticated()
connection = self._connection connection = self._connection
if TYPE_CHECKING:
assert connection is not None assert connection is not None
resp = await connection.send_message_await_response( resp = await connection.send_message_await_response(
DeviceInfoRequest(), DeviceInfoResponse DeviceInfoRequest(), DeviceInfoResponse
@ -449,6 +451,7 @@ class APIClient:
def do_stop(msg: message.Message) -> bool: def do_stop(msg: message.Message) -> bool:
return isinstance(msg, ListEntitiesDoneResponse) return isinstance(msg, ListEntitiesDoneResponse)
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
resp = await self._connection.send_messages_await_response_complex( resp = await self._connection.send_messages_await_response_complex(
(ListEntitiesRequest(),), do_append, do_stop, msg_types, 60 (ListEntitiesRequest(),), do_append, do_stop, msg_types, 60
@ -491,6 +494,7 @@ class APIClient:
del image_stream[msg_key] del image_stream[msg_key]
on_state(CameraState(key=msg.key, data=image_data)) # type: ignore[call-arg] on_state(CameraState(key=msg.key, data=image_data)) # type: ignore[call-arg]
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message_callback_response( self._connection.send_message_callback_response(
SubscribeStatesRequest(), _on_state_msg, msg_types SubscribeStatesRequest(), _on_state_msg, msg_types
@ -508,6 +512,7 @@ class APIClient:
req.level = log_level req.level = log_level
if dump_config is not None: if dump_config is not None:
req.dump_config = dump_config req.dump_config = dump_config
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message_callback_response( self._connection.send_message_callback_response(
req, on_log, (SubscribeLogsResponse,) req, on_log, (SubscribeLogsResponse,)
@ -523,6 +528,7 @@ class APIClient:
) -> None: ) -> None:
on_service_call(HomeassistantServiceCall.from_pb(msg)) on_service_call(HomeassistantServiceCall.from_pb(msg))
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message_callback_response( self._connection.send_message_callback_response(
SubscribeHomeassistantServicesRequest(), SubscribeHomeassistantServicesRequest(),
@ -563,6 +569,7 @@ class APIClient:
) -> message.Message: ) -> message.Message:
self._check_authenticated() self._check_authenticated()
msg_types = (response_type, BluetoothGATTErrorResponse) msg_types = (response_type, BluetoothGATTErrorResponse)
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
message_filter = partial(self._filter_bluetooth_message, address, handle) message_filter = partial(self._filter_bluetooth_message, address, handle)
@ -586,6 +593,7 @@ class APIClient:
) -> None: ) -> None:
on_bluetooth_le_advertisement(BluetoothLEAdvertisement.from_pb(msg)) # type: ignore[misc] on_bluetooth_le_advertisement(BluetoothLEAdvertisement.from_pb(msg)) # type: ignore[misc]
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
unsub_callback = self._connection.send_message_callback_response( unsub_callback = self._connection.send_message_callback_response(
SubscribeBluetoothLEAdvertisementsRequest(flags=0), SubscribeBluetoothLEAdvertisementsRequest(flags=0),
@ -608,6 +616,7 @@ class APIClient:
self._check_authenticated() self._check_authenticated()
msg_types = (BluetoothLERawAdvertisementsResponse,) msg_types = (BluetoothLERawAdvertisementsResponse,)
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
def _on_ble_raw_advertisement_response( def _on_ble_raw_advertisement_response(
@ -643,6 +652,7 @@ class APIClient:
) -> None: ) -> None:
on_bluetooth_connections_free_update(msg.free, msg.limit) on_bluetooth_connections_free_update(msg.free, msg.limit)
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
return self._connection.send_message_callback_response( return self._connection.send_message_callback_response(
SubscribeBluetoothConnectionsFreeRequest(), SubscribeBluetoothConnectionsFreeRequest(),
@ -687,7 +697,9 @@ class APIClient:
debug = _LOGGER.isEnabledFor(logging.DEBUG) debug = _LOGGER.isEnabledFor(logging.DEBUG)
connect_future: asyncio.Future[None] = self._loop.create_future() connect_future: asyncio.Future[None] = self._loop.create_future()
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
if has_cache: if has_cache:
# REMOTE_CACHING feature with cache: requestor has services and mtu cached # REMOTE_CACHING feature with cache: requestor has services and mtu cached
request_type = BluetoothDeviceRequestType.CONNECT_V3_WITH_CACHE request_type = BluetoothDeviceRequestType.CONNECT_V3_WITH_CACHE
@ -854,6 +866,7 @@ class APIClient:
timeout: float, timeout: float,
) -> message.Message: ) -> message.Message:
self._check_authenticated() self._check_authenticated()
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
[response] = await self._connection.send_messages_await_response_complex( [response] = await self._connection.send_messages_await_response_complex(
( (
@ -887,6 +900,7 @@ class APIClient:
def do_stop(msg: message.Message) -> bool: def do_stop(msg: message.Message) -> bool:
return isinstance(msg, stop_types) and msg.address == address return isinstance(msg, stop_types) and msg.address == address
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
resp = await self._connection.send_messages_await_response_complex( resp = await self._connection.send_messages_await_response_complex(
(BluetoothGATTGetServicesRequest(address=address),), (BluetoothGATTGetServicesRequest(address=address),),
@ -932,6 +946,7 @@ class APIClient:
req.data = data req.data = data
if not response: if not response:
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
return return
@ -993,6 +1008,7 @@ class APIClient:
req.data = data req.data = data
if not wait_for_response: if not wait_for_response:
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
return return
@ -1035,6 +1051,7 @@ class APIClient:
if address == msg.address and handle == msg.handle: if address == msg.address and handle == msg.handle:
on_bluetooth_gatt_notify(handle, bytearray(msg.data)) on_bluetooth_gatt_notify(handle, bytearray(msg.data))
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
remove_callback = self._connection.add_message_callback( remove_callback = self._connection.add_message_callback(
_on_bluetooth_gatt_notify_data_response, (BluetoothGATTNotifyDataResponse,) _on_bluetooth_gatt_notify_data_response, (BluetoothGATTNotifyDataResponse,)
@ -1064,6 +1081,7 @@ class APIClient:
) -> None: ) -> None:
on_state_sub(msg.entity_id, msg.attribute) on_state_sub(msg.entity_id, msg.attribute)
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message_callback_response( self._connection.send_message_callback_response(
SubscribeHomeAssistantStatesRequest(), SubscribeHomeAssistantStatesRequest(),
@ -1076,6 +1094,7 @@ class APIClient:
) -> None: ) -> None:
self._check_authenticated() self._check_authenticated()
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message( self._connection.send_message(
HomeAssistantStateResponse( HomeAssistantStateResponse(
@ -1116,6 +1135,7 @@ class APIClient:
elif position == 0.0: elif position == 0.0:
req.legacy_command = LegacyCoverCommand.CLOSE req.legacy_command = LegacyCoverCommand.CLOSE
req.has_legacy_command = True req.has_legacy_command = True
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1147,10 +1167,11 @@ class APIClient:
if direction is not None: if direction is not None:
req.has_direction = True req.has_direction = True
req.direction = direction req.direction = direction
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
async def light_command( async def light_command( # pylint: disable=too-many-branches
self, self,
key: int, key: int,
state: bool | None = None, state: bool | None = None,
@ -1208,6 +1229,7 @@ class APIClient:
if effect is not None: if effect is not None:
req.has_effect = True req.has_effect = True
req.effect = effect req.effect = effect
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1217,6 +1239,7 @@ class APIClient:
req = SwitchCommandRequest() req = SwitchCommandRequest()
req.key = key req.key = key
req.state = state req.state = state
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1269,6 +1292,7 @@ class APIClient:
if custom_preset is not None: if custom_preset is not None:
req.has_custom_preset = True req.has_custom_preset = True
req.custom_preset = custom_preset req.custom_preset = custom_preset
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1278,6 +1302,7 @@ class APIClient:
req = NumberCommandRequest() req = NumberCommandRequest()
req.key = key req.key = key
req.state = state req.state = state
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1287,6 +1312,7 @@ class APIClient:
req = SelectCommandRequest() req = SelectCommandRequest()
req.key = key req.key = key
req.state = state req.state = state
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1314,6 +1340,7 @@ class APIClient:
if duration is not None: if duration is not None:
req.duration = duration req.duration = duration
req.has_duration = True req.has_duration = True
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1322,6 +1349,7 @@ class APIClient:
req = ButtonCommandRequest() req = ButtonCommandRequest()
req.key = key req.key = key
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1338,6 +1366,7 @@ class APIClient:
req.command = command req.command = command
if code is not None: if code is not None:
req.code = code req.code = code
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1362,6 +1391,7 @@ class APIClient:
if media_url is not None: if media_url is not None:
req.media_url = media_url req.media_url = media_url
req.has_media_url = True req.has_media_url = True
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1371,6 +1401,7 @@ class APIClient:
req = TextCommandRequest() req = TextCommandRequest()
req.key = key req.key = key
req.state = state req.state = state
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1409,6 +1440,7 @@ class APIClient:
args.append(arg) args.append(arg)
# pylint: disable=no-member # pylint: disable=no-member
req.args.extend(args) req.args.extend(args)
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1418,6 +1450,7 @@ class APIClient:
req = CameraImageRequest() req = CameraImageRequest()
req.single = single req.single = single
req.stream = stream req.stream = stream
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1478,6 +1511,7 @@ class APIClient:
self._background_tasks.add(stop_task) self._background_tasks.add(stop_task)
stop_task.add_done_callback(self._background_tasks.discard) stop_task.add_done_callback(self._background_tasks.discard)
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(SubscribeVoiceAssistantRequest(subscribe=True)) self._connection.send_message(SubscribeVoiceAssistantRequest(subscribe=True))
@ -1517,6 +1551,7 @@ class APIClient:
# pylint: disable=no-member # pylint: disable=no-member
req.data.extend(data_args) req.data.extend(data_args)
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)
@ -1533,5 +1568,6 @@ class APIClient:
req.command = command req.command = command
if code is not None: if code is not None:
req.code = code req.code = code
if TYPE_CHECKING:
assert self._connection is not None assert self._connection is not None
self._connection.send_message(req) self._connection.send_message(req)