1
mirror of https://github.com/home-assistant/core synced 2024-09-06 10:29:55 +02:00

Use async_create_task (#15633)

* Use async_create_task

* Fix test
This commit is contained in:
Paulus Schoutsen 2018-07-23 14:05:38 +02:00 committed by GitHub
parent 50b6c5948d
commit ea2ff6aae3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 50 additions and 49 deletions

View File

@ -167,7 +167,7 @@ def async_setup(hass, config):
def async_handle_core_service(call):
"""Service handler for handling core services."""
if call.service == SERVICE_HOMEASSISTANT_STOP:
hass.async_add_job(hass.async_stop())
hass.async_create_task(hass.async_stop())
return
try:
@ -183,7 +183,7 @@ def async_setup(hass, config):
return
if call.service == SERVICE_HOMEASSISTANT_RESTART:
hass.async_add_job(hass.async_stop(RESTART_EXIT_CODE))
hass.async_create_task(hass.async_stop(RESTART_EXIT_CODE))
hass.services.async_register(
ha.DOMAIN, SERVICE_HOMEASSISTANT_STOP, async_handle_core_service)

View File

@ -187,7 +187,7 @@ class AlarmControlPanel(Entity):
This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(self.alarm_disarm, code)
return self.hass.async_add_executor_job(self.alarm_disarm, code)
def alarm_arm_home(self, code=None):
"""Send arm home command."""
@ -198,7 +198,7 @@ class AlarmControlPanel(Entity):
This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(self.alarm_arm_home, code)
return self.hass.async_add_executor_job(self.alarm_arm_home, code)
def alarm_arm_away(self, code=None):
"""Send arm away command."""
@ -209,7 +209,7 @@ class AlarmControlPanel(Entity):
This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(self.alarm_arm_away, code)
return self.hass.async_add_executor_job(self.alarm_arm_away, code)
def alarm_arm_night(self, code=None):
"""Send arm night command."""
@ -220,7 +220,7 @@ class AlarmControlPanel(Entity):
This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(self.alarm_arm_night, code)
return self.hass.async_add_executor_job(self.alarm_arm_night, code)
def alarm_trigger(self, code=None):
"""Send alarm trigger command."""
@ -231,7 +231,7 @@ class AlarmControlPanel(Entity):
This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(self.alarm_trigger, code)
return self.hass.async_add_executor_job(self.alarm_trigger, code)
def alarm_arm_custom_bypass(self, code=None):
"""Send arm custom bypass command."""
@ -242,7 +242,8 @@ class AlarmControlPanel(Entity):
This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(self.alarm_arm_custom_bypass, code)
return self.hass.async_add_executor_job(
self.alarm_arm_custom_bypass, code)
@property
def state_attributes(self):

View File

@ -68,7 +68,7 @@ def turn_on(hass, entity_id):
def async_turn_on(hass, entity_id):
"""Async reset the alert."""
data = {ATTR_ENTITY_ID: entity_id}
hass.async_add_job(
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data))
@ -81,7 +81,7 @@ def turn_off(hass, entity_id):
def async_turn_off(hass, entity_id):
"""Async acknowledge the alert."""
data = {ATTR_ENTITY_ID: entity_id}
hass.async_add_job(
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data))
@ -94,7 +94,7 @@ def toggle(hass, entity_id):
def async_toggle(hass, entity_id):
"""Async toggle acknowledgement of alert."""
data = {ATTR_ENTITY_ID: entity_id}
hass.async_add_job(
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TOGGLE, data))
@ -217,7 +217,7 @@ class Alert(ToggleEntity):
else:
yield from self._schedule_notify()
self.hass.async_add_job(self.async_update_ha_state)
self.async_schedule_update_ha_state()
@asyncio.coroutine
def end_alerting(self):
@ -228,7 +228,7 @@ class Alert(ToggleEntity):
self._firing = False
if self._done_message and self._send_done_message:
yield from self._notify_done_message()
self.hass.async_add_job(self.async_update_ha_state)
self.async_schedule_update_ha_state()
@asyncio.coroutine
def _schedule_notify(self):

View File

@ -214,11 +214,11 @@ def async_setup(hass, config):
CONF_PASSWORD: password
})
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'camera', 'mjpeg', mjpeg_camera, config))
if sensors:
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'sensor', DOMAIN, {
CONF_NAME: name,
CONF_HOST: host,
@ -226,7 +226,7 @@ def async_setup(hass, config):
}, config))
if switches:
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'switch', DOMAIN, {
CONF_NAME: name,
CONF_HOST: host,
@ -234,7 +234,7 @@ def async_setup(hass, config):
}, config))
if motion:
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'binary_sensor', DOMAIN, {
CONF_HOST: host,
CONF_NAME: name,

View File

@ -218,10 +218,10 @@ def _setup_atv(hass, atv_config):
ATTR_POWER: power
}
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'media_player', DOMAIN, atv_config))
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'remote', DOMAIN, atv_config))

View File

@ -14,7 +14,7 @@ async def async_setup(hass, config):
async def async_setup_entry(hass, entry):
"""Set up Cast from a config entry."""
hass.async_add_job(hass.config_entries.async_forward_entry_setup(
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
entry, 'media_player'))
return True
@ -23,7 +23,7 @@ async def _async_has_devices(hass):
"""Return if there are devices that can be discovered."""
from pychromecast.discovery import discover_chromecasts
return await hass.async_add_job(discover_chromecasts)
return await hass.async_add_executor_job(discover_chromecasts)
config_entry_flow.register_discovery_flow(

View File

@ -97,7 +97,7 @@ async def async_setup_entry(hass, config_entry):
hass.data[DATA_DECONZ_UNSUB] = []
for component in ['binary_sensor', 'light', 'scene', 'sensor']:
hass.async_add_job(hass.config_entries.async_forward_entry_setup(
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
config_entry, component))
@callback

View File

@ -143,12 +143,12 @@ async def async_setup(hass, config):
# No users, cannot continue
return False
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'sensor', DOMAIN, {
CONF_SENSORS: sensors,
}, config))
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'binary_sensor', DOMAIN, {
CONF_BINARY_SENSORS: binary_sensors,
}, config))

View File

@ -167,21 +167,21 @@ def async_setup(hass, config):
# Load sub-components for Envisalink
if partitions:
hass.async_add_job(async_load_platform(
hass.async_create_task(async_load_platform(
hass, 'alarm_control_panel', 'envisalink', {
CONF_PARTITIONS: partitions,
CONF_CODE: code,
CONF_PANIC: panic_type
}, config
))
hass.async_add_job(async_load_platform(
hass.async_create_task(async_load_platform(
hass, 'sensor', 'envisalink', {
CONF_PARTITIONS: partitions,
CONF_CODE: code
}, config
))
if zones:
hass.async_add_job(async_load_platform(
hass.async_create_task(async_load_platform(
hass, 'binary_sensor', 'envisalink', {
CONF_ZONES: zones
}, config

View File

@ -117,7 +117,7 @@ class HomematicipHAP:
self.config_entry.data.get(HMIPC_HAPID))
for component in COMPONENTS:
self.hass.async_add_job(
self.hass.async_create_task(
self.hass.config_entries.async_forward_entry_setup(
self.config_entry, component)
)

View File

@ -78,7 +78,7 @@ class HueBridge:
host)
return False
hass.async_add_job(hass.config_entries.async_forward_entry_setup(
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
self.config_entry, 'light'))
hass.services.async_register(

View File

@ -148,7 +148,7 @@ def async_setup(hass, config):
device.states[state_key].name,
platform)
hass.async_add_job(
hass.async_create_task(
discovery.async_load_platform(
hass, platform, DOMAIN,
discovered={'address': device.address.id,

View File

@ -107,7 +107,7 @@ async def async_setup(hass, config):
('scene', 'Scene'),
('notify', 'Notification')):
found_devices = _get_devices(hass, discovery_type)
hass.async_add_job(
hass.async_create_task(
discovery.async_load_platform(hass, component, DOMAIN, {
ATTR_DISCOVER_DEVICES: found_devices
}, config))

View File

@ -63,8 +63,8 @@ def async_setup(hass, base_config):
_LOGGER.info("Connected to Lutron smartbridge at %s", config[CONF_HOST])
for component in LUTRON_CASETA_COMPONENTS:
hass.async_add_job(discovery.async_load_platform(hass, component,
DOMAIN, {}, config))
hass.async_create_task(discovery.async_load_platform(
hass, component, DOMAIN, {}, config))
return True

View File

@ -178,7 +178,7 @@ async def _discover_persistent_devices(hass, gateway):
@callback
def _discover_mysensors_platform(hass, platform, new_devices):
"""Discover a MySensors platform."""
task = hass.async_add_job(discovery.async_load_platform(
task = hass.async_create_task(discovery.async_load_platform(
hass, platform, DOMAIN,
{ATTR_DEVICES: new_devices, CONF_NAME: DOMAIN}))
return task

View File

@ -127,7 +127,7 @@ async def async_setup_entry(hass, entry):
return False
for component in 'climate', 'camera', 'sensor', 'binary_sensor':
hass.async_add_job(hass.config_entries.async_forward_entry_setup(
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
entry, component))
def set_mode(service):

View File

@ -151,7 +151,7 @@ async def async_setup(hass, config):
('sensor', conf[CONF_SENSORS]),
('switch', conf[CONF_SWITCHES]),
]:
hass.async_add_job(
hass.async_create_task(
discovery.async_load_platform(hass, component, DOMAIN, schema,
config))

View File

@ -134,7 +134,7 @@ def async_setup_sabnzbd(hass, sab_api, config, name):
if config.get(CONF_SENSORS):
hass.data[DATA_SABNZBD] = sab_api_data
hass.async_add_job(
hass.async_create_task(
discovery.async_load_platform(hass, 'sensor', DOMAIN, {}, config))
async def async_service_handler(service):

View File

@ -98,10 +98,10 @@ def async_setup(hass, config):
conf,
conf.get(CONF_ARM_HOME_MODE))
task_control_panel = hass.async_add_job(
task_control_panel = hass.async_create_task(
async_load_platform(hass, 'alarm_control_panel', DOMAIN, conf, config))
task_zones = hass.async_add_job(
task_zones = hass.async_create_task(
async_load_platform(hass, 'binary_sensor', DOMAIN,
{CONF_ZONES: zones}, config))

View File

@ -14,7 +14,7 @@ async def async_setup(hass, config):
async def async_setup_entry(hass, entry):
"""Set up Sonos from a config entry."""
hass.async_add_job(hass.config_entries.async_forward_entry_setup(
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
entry, 'media_player'))
return True
@ -23,7 +23,7 @@ async def _async_has_devices(hass):
"""Return if there are devices that can be discovered."""
import soco
return await hass.async_add_job(soco.discover)
return await hass.async_add_executor_job(soco.discover)
config_entry_flow.register_discovery_flow(DOMAIN, 'Sonos', _async_has_devices)

View File

@ -56,14 +56,14 @@ def async_setup(hass, config):
# add sensor devices for each zone (typically motion/fire/door sensors)
zones = yield from api.get_zones()
if zones:
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'binary_sensor', DOMAIN,
{ATTR_DISCOVER_DEVICES: zones}, config))
# create a separate alarm panel for each area
areas = yield from api.get_areas()
if areas:
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'alarm_control_panel', DOMAIN,
{ATTR_DISCOVER_AREAS: areas}, config))

View File

@ -166,8 +166,8 @@ async def _setup_gateway(hass, hass_config, host, identity, key,
return True
gateways[gateway_id] = gateway
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'light', DOMAIN, {'gateway': gateway_id}, hass_config))
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'sensor', DOMAIN, {'gateway': gateway_id}, hass_config))
return True

View File

@ -88,7 +88,7 @@ async def async_setup(hass, config):
service = device.find_first_service(IP_SERVICE)
if _service['serviceType'] == CIC_SERVICE:
unit = config.get(CONF_UNITS)
hass.async_add_job(discovery.async_load_platform(
hass.async_create_task(discovery.async_load_platform(
hass, 'sensor', DOMAIN, {'unit': unit}, config))
except UpnpSoapError as error:
_LOGGER.error(error)

View File

@ -39,7 +39,7 @@ async def async_setup(hass, config):
return False
for component in SUPPORTED_DOMAINS:
hass.async_add_job(
hass.async_create_task(
discovery.async_load_platform(hass, component, DOMAIN, {}, config))
return True

View File

@ -145,7 +145,7 @@ async def async_load_platform(hass, component, platform, discovered=None,
Use `listen_platform` to register a callback for these events.
Warning: Do not await this inside a setup method to avoid a dead lock.
Use `hass.async_add_job(async_load_platform(..))` instead.
Use `hass.async_create_task(async_load_platform(..))` instead.
This method is a coroutine.
"""

View File

@ -91,7 +91,7 @@ async def test_setup_entry_successful(hass):
"""Test setup entry is successful."""
entry = Mock()
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
with patch.object(hass, 'async_add_job') as mock_add_job, \
with patch.object(hass, 'async_create_task') as mock_add_job, \
patch.object(hass, 'config_entries') as mock_config_entries, \
patch('pydeconz.DeconzSession.async_load_parameters',
return_value=mock_coro(True)):