Make mysensors updates and platform setup async (#13603)

* Use async updates but keep methods that interact with mysensors
  gateway thread, eg turn_on and turn_off, non async.
* Use Python 3.5 async syntax.
This commit is contained in:
Martin Hjelmare authored and Paulus Schoutsen committed 2018-04-01 08:36:26 -07:00
1 parent ff9f500c51
commit c8f2810fac
9 files changed
+65 -57

No files matched your search

@@ -6,15 +6,15 @@ https://home-assistant.io/components/device_tracker.mysensors/
"""
from homeassistant.components import mysensors
from homeassistant.components.device_tracker import DOMAIN
from homeassistant.helpers.dispatcher import dispatcher_connect
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util import slugify
def setup_scanner(hass, config, see, discovery_info=None):
async def async_setup_scanner(hass, config, async_see, discovery_info=None):
"""Set up the MySensors device scanner."""
new_devices = mysensors.setup_mysensors_platform(
hass, DOMAIN, discovery_info, MySensorsDeviceScanner,
device_args=(see, ))
device_args=(async_see, ))
if not new_devices:
return False
@@ -22,9 +22,9 @@ def setup_scanner(hass, config, see, discovery_info=None):
dev_id = (
id(device.gateway), device.node_id, device.child_id,
device.value_type)
dispatcher_connect(
async_dispatcher_connect(
hass, mysensors.SIGNAL_CALLBACK.format(*dev_id),
device.update_callback)
device.async_update_callback)
return True
@@ -32,20 +32,20 @@ def setup_scanner(hass, config, see, discovery_info=None):
class MySensorsDeviceScanner(mysensors.MySensorsDevice):
"""Represent a MySensors scanner."""
def __init__(self, see, *args):
def __init__(self, async_see, *args):
"""Set up instance."""
super().__init__(*args)
self.see = see
self.async_see = async_see
def update_callback(self):
async def async_update_callback(self):
"""Update the device."""
self.update()
await self.async_update()
node = self.gateway.sensors[self.node_id]
child = node.children[self.child_id]
position = child.values[self.value_type]
latitude, longitude, _ = position.split(',')
self.see(
await self.async_see(
dev_id=slugify(self.name),
host_name=self.name,
gps=(latitude, longitude),