Skip to content

Commit

Permalink
Merge pull request #1568 from hbldh/release/v0.22.1
Browse files Browse the repository at this point in the history
Release/v0.22.1
  • Loading branch information
dlech authored May 7, 2024
2 parents 93d88e0 + e285de5 commit 181467f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
`Unreleased`_
=============

`0.22.0`_ (2024-04-04)
`0.22.1`_ (2024-05-07)
======================

Added
-----
* Added ``bleak.backends.winrt.util.allow_sta()`` method to allow integration
with graphical user interfaces on Windows. Fixes #1565.

`0.22.0`_ (2024-05-04)
======================

Added
Expand Down Expand Up @@ -1014,7 +1022,8 @@ Fixed
* Bleak created.


.. _Unreleased: https://github.com/hbldh/bleak/compare/v0.22.0...develop
.. _Unreleased: https://github.com/hbldh/bleak/compare/v0.22.1...develop
.. _0.22.1: https://github.com/hbldh/bleak/compare/v0.22.0...v0.22.1
.. _0.22.0: https://github.com/hbldh/bleak/compare/v0.21.1...v0.22.0
.. _0.21.1: https://github.com/hbldh/bleak/compare/v0.21.0...v0.21.1
.. _0.21.0: https://github.com/hbldh/bleak/compare/v0.20.2...v0.21.0
Expand Down
21 changes: 21 additions & 0 deletions bleak/backends/winrt/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def assert_mta() -> None:
.. versionadded:: 0.22
"""
if hasattr(allow_sta, "_allowed"):
return

try:
apt_type, _ = _get_apartment_type()
if apt_type != _AptType.MTA:
Expand All @@ -81,6 +84,24 @@ def assert_mta() -> None:
raise


def allow_sta():
"""
Suppress check for MTA thread type and allow STA.
Bleak will hang forever if the current thread is not MTA - unless there is
a Windows event loop running that is properly integrated with asyncio in
Python.
If your program meets that condition, you must call this function do disable
the check for MTA. If your program doesn't have a graphical user interface
you probably shouldn't call this function. and use ``uninitialize_sta()``
instead.
.. versionadded:: 0.22.1
"""
allow_sta._allowed = True


def uninitialize_sta():
"""
Uninitialize the COM library on the current thread if it was not initialized
Expand Down
23 changes: 20 additions & 3 deletions docs/troubleshooting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,28 @@ Bleak should detect this and raise an exception with a message similar to::

The current thread apartment type is not MTA: STA.

To work around this, you can use a utility function provided by Bleak to
uninitialize the threading model after importing an offending package::
To work around this, you can use one of the utility functions provided by Bleak.

If your program has a graphical user interface and the UI framework *and* it is
properly integrated with asyncio *and* Bleak is not running on a background
thread then call ``allow_sta()`` before calling any other Bleak APis::

try:
from bleak.backends.winrt.util import allow_sta
# tell Bleak we are using a graphical user interface that has been properly
# configured to work with asyncio
allow_sta()
except ImportError:
# other OSes and older versions of Bleak will raise ImportError which we
# can safely ignore
pass

The more typical case, though, is that some library has imported something like
``pywin32`` which breaks Bleak. In this case, you can uninitialize the threading
model like this::

import win32com # this sets current thread to STA :-(
from bleak.backends.winrt.utils import uninitialize_sta
from bleak.backends.winrt.util import uninitialize_sta

uninitialize_sta() # undo the unwanted side effect

Expand Down
13 changes: 10 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "bleak"
version = "0.22.0"
version = "0.22.1"
description = "Bluetooth Low Energy platform Agnostic Klient"
authors = ["Henrik Blidh <henrik.blidh@nedomkull.com>"]
license = "MIT"
Expand Down

0 comments on commit 181467f

Please sign in to comment.