Async syntax 6, sensor (#17020)

This commit is contained in:
cdce8p authored and Paulus Schoutsen committed 2018-10-01 08:55:43 +02:00
1 parent 121dba659c
commit 9e4c8f45d6
45 files changed
+195 -319

No files matched your search

+7 -12
View File
@@ -4,7 +4,6 @@ Support for reading data from a serial port.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.serial/
"""
import asyncio
import logging
import json
@@ -35,9 +34,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
})
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the Serial sensor platform."""
name = config.get(CONF_NAME)
port = config.get(CONF_SERIAL_PORT)
@@ -67,20 +65,18 @@ class SerialSensor(Entity):
self._template = value_template
self._attributes = []
@asyncio.coroutine
def async_added_to_hass(self):
async def async_added_to_hass(self):
"""Handle when an entity is about to be added to Home Assistant."""
self._serial_loop_task = self.hass.loop.create_task(
self.serial_read(self._port, self._baudrate))
@asyncio.coroutine
def serial_read(self, device, rate, **kwargs):
async def serial_read(self, device, rate, **kwargs):
"""Read the data from the port."""
import serial_asyncio
reader, _ = yield from serial_asyncio.open_serial_connection(
reader, _ = await serial_asyncio.open_serial_connection(
url=device, baudrate=rate, **kwargs)
while True:
line = yield from reader.readline()
line = await reader.readline()
line = line.decode('utf-8').strip()
try:
@@ -98,8 +94,7 @@ class SerialSensor(Entity):
self._state = line
self.async_schedule_update_ha_state()
@asyncio.coroutine
def stop_serial_read(self):
async def stop_serial_read(self):
"""Close resources."""
if self._serial_loop_task:
self._serial_loop_task.cancel()