Add baudrate option to Serial sensor (#10439)
* Add baudrate option Baudrate is essential! * line too long line too long (82 > 79 characters) * trailing whitespace * Rename const * Fix the missing one
This commit is contained in:
parent
119fb08198
commit
a5aa111893
1 changed files with 11 additions and 5 deletions
|
@ -19,11 +19,15 @@ REQUIREMENTS = ['pyserial-asyncio==0.4']
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
CONF_SERIAL_PORT = 'serial_port'
|
CONF_SERIAL_PORT = 'serial_port'
|
||||||
|
CONF_BAUDRATE = 'baudrate'
|
||||||
|
|
||||||
DEFAULT_NAME = "Serial Sensor"
|
DEFAULT_NAME = "Serial Sensor"
|
||||||
|
DEFAULT_BAUDRATE = 9600
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_SERIAL_PORT): cv.string,
|
vol.Required(CONF_SERIAL_PORT): cv.string,
|
||||||
|
vol.Optional(CONF_BAUDRATE, default=DEFAULT_BAUDRATE):
|
||||||
|
cv.positive_int,
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -33,8 +37,9 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||||
"""Set up the Serial sensor platform."""
|
"""Set up the Serial sensor platform."""
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
port = config.get(CONF_SERIAL_PORT)
|
port = config.get(CONF_SERIAL_PORT)
|
||||||
|
baudrate = config.get(CONF_BAUDRATE)
|
||||||
|
|
||||||
sensor = SerialSensor(name, port)
|
sensor = SerialSensor(name, port, baudrate)
|
||||||
|
|
||||||
hass.bus.async_listen_once(
|
hass.bus.async_listen_once(
|
||||||
EVENT_HOMEASSISTANT_STOP, sensor.stop_serial_read())
|
EVENT_HOMEASSISTANT_STOP, sensor.stop_serial_read())
|
||||||
|
@ -44,25 +49,26 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||||
class SerialSensor(Entity):
|
class SerialSensor(Entity):
|
||||||
"""Representation of a Serial sensor."""
|
"""Representation of a Serial sensor."""
|
||||||
|
|
||||||
def __init__(self, name, port):
|
def __init__(self, name, port, baudrate):
|
||||||
"""Initialize the Serial sensor."""
|
"""Initialize the Serial sensor."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._state = None
|
self._state = None
|
||||||
self._port = port
|
self._port = port
|
||||||
|
self._baudrate = baudrate
|
||||||
self._serial_loop_task = None
|
self._serial_loop_task = None
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_added_to_hass(self):
|
def async_added_to_hass(self):
|
||||||
"""Handle when an entity is about to be added to Home Assistant."""
|
"""Handle when an entity is about to be added to Home Assistant."""
|
||||||
self._serial_loop_task = self.hass.loop.create_task(
|
self._serial_loop_task = self.hass.loop.create_task(
|
||||||
self.serial_read(self._port))
|
self.serial_read(self._port, self._baudrate))
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def serial_read(self, device, **kwargs):
|
def serial_read(self, device, rate, **kwargs):
|
||||||
"""Read the data from the port."""
|
"""Read the data from the port."""
|
||||||
import serial_asyncio
|
import serial_asyncio
|
||||||
reader, _ = yield from serial_asyncio.open_serial_connection(
|
reader, _ = yield from serial_asyncio.open_serial_connection(
|
||||||
url=device, **kwargs)
|
url=device, baudrate=rate, **kwargs)
|
||||||
while True:
|
while True:
|
||||||
line = yield from reader.readline()
|
line = yield from reader.readline()
|
||||||
self._state = line.decode('utf-8').strip()
|
self._state = line.decode('utf-8').strip()
|
||||||
|
|
Loading…
Add table
Reference in a new issue