mirror of
https://github.com/home-assistant/developers.home-assistant.git
synced 2025-11-15 22:10:14 +00:00
Code block improvements (#382)
* Use f-strings instead of .format() * Code block language marker fixes * Make example code blocks syntactically valid Python * Run all python code blocks through black https://github.com/scop/misc/blob/master/black_markdown.py * Add some missing code block language markers * Use shell language consistently to mark shell code blocks * Undo folding of some example dicts * Remove outdated OrderedDict comments per Python 3.7, replace with plain dict
This commit is contained in:
@@ -16,14 +16,16 @@ To make a component async, implement an async_setup.
|
||||
|
||||
```python
|
||||
def setup(hass, config):
|
||||
# Setup your component outside of the event loop.
|
||||
"""Set up component."""
|
||||
# Code for setting up your component outside of the event loop.
|
||||
```
|
||||
|
||||
Will turn into:
|
||||
|
||||
```python
|
||||
async def async_setup(hass, config):
|
||||
# Setup your component inside of the event loop.
|
||||
"""Set up component."""
|
||||
# Code for setting up your component inside of the event loop.
|
||||
```
|
||||
|
||||
## Implementing an async platform
|
||||
@@ -31,16 +33,17 @@ async def async_setup(hass, config):
|
||||
For platforms we support async setup. Instead of setup_platform you need to have a coroutine async_setup_platform.
|
||||
|
||||
```python
|
||||
setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
# Setup your platform outside of the event loop.
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up platform."""
|
||||
# Code for setting up your platform outside of the event loop.
|
||||
```
|
||||
|
||||
Will turn into:
|
||||
|
||||
```python
|
||||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
# Setup your platform inside of the event loop
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up platform."""
|
||||
# Code for setting up your platform inside of the event loop.
|
||||
```
|
||||
|
||||
The only difference with the original parameters is that the `add_entities` function has been replaced by the async friendly callback `async_add_entities`.
|
||||
@@ -76,12 +79,15 @@ In the following example, `say_hello` will schedule `async_say_hello` and block
|
||||
```python
|
||||
import asyncio
|
||||
|
||||
|
||||
def say_hello(hass, target):
|
||||
return asyncio.run_coroutine_threadsafe(
|
||||
async_say_hello(hass, target), hass.loop).result()
|
||||
async_say_hello(hass, target), hass.loop
|
||||
).result()
|
||||
|
||||
|
||||
async def async_say_hello(hass, target):
|
||||
return "Hello {}!".format(target)
|
||||
return f"Hello {target}!"
|
||||
```
|
||||
|
||||
## Calling sync functions from async
|
||||
|
||||
Reference in New Issue
Block a user