Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use async_create_task #15633

Merged
merged 2 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions homeassistant/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def async_handle_turn_service(service):
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:
Expand All @@ -183,7 +183,7 @@ def async_handle_core_service(call):
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)
Expand Down
13 changes: 7 additions & 6 deletions homeassistant/components/alarm_control_panel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def async_alarm_disarm(self, code=None):

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."""
Expand All @@ -198,7 +198,7 @@ def async_alarm_arm_home(self, code=None):

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."""
Expand All @@ -209,7 +209,7 @@ def async_alarm_arm_away(self, code=None):

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."""
Expand All @@ -220,7 +220,7 @@ def async_alarm_arm_night(self, code=None):

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."""
Expand All @@ -231,7 +231,7 @@ def async_alarm_trigger(self, code=None):

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."""
Expand All @@ -242,7 +242,8 @@ def async_alarm_arm_custom_bypass(self, code=None):

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):
Expand Down
10 changes: 5 additions & 5 deletions homeassistant/components/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand All @@ -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))


Expand All @@ -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))


Expand Down Expand Up @@ -217,7 +217,7 @@ def begin_alerting(self):
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):
Expand All @@ -228,7 +228,7 @@ def end_alerting(self):
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):
Expand Down
8 changes: 4 additions & 4 deletions homeassistant/components/android_ip_webcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,27 +214,27 @@ def async_update_data(now):
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,
CONF_SENSORS: sensors,
}, 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,
CONF_SWITCHES: switches,
}, 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,
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/apple_tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/cast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/deconz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def async_add_device_callback(device_type, device):
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
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/eight_sleep.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ async def async_update_user_data(now):
# 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))
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/envisalink.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,21 @@ def stop_envisalink(event):

# 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
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/homematicip_cloud/hap.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async def retry_setup(_now):
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)
)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/hue/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def retry_setup(_now):
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(
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/insteon_plm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def async_plm_new_device(device):
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,
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/knx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/lutron_caseta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/mysensors/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/nest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/rainmachine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/sabnzbd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/satel_integra.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ def _close():
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))

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/sonos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
4 changes: 2 additions & 2 deletions homeassistant/components/spc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/tradfri.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion homeassistant/components/upnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/velux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion homeassistant/helpers/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down