Use voluptuous for Acer projector switch (#3077)

🐬
This commit is contained in:
Fabian Affolter 2016-09-02 15:42:38 +02:00 committed by Teagan Glenn
parent dedc4a129c
commit e5ef548f10
2 changed files with 52 additions and 38 deletions

View File

@ -1,21 +1,40 @@
""" """
Use serial protocol of acer projector to obtain state of the projector. Use serial protocol of Acer projector to obtain state of the projector.
This component allows to control almost all projectors from acer using For more details about this component, please refer to the documentation
their RS232 serial communication protocol. at https://home-assistant.io/components/switch.acer_projector/
""" """
import logging import logging
import re import re
from homeassistant.components.switch import SwitchDevice import voluptuous as vol
from homeassistant.const import (STATE_ON, STATE_OFF, STATE_UNKNOWN,
CONF_NAME, CONF_FILENAME) from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import (
STATE_ON, STATE_OFF, STATE_UNKNOWN, CONF_NAME, CONF_FILENAME)
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['pyserial==3.1.1']
_LOGGER = logging.getLogger(__name__)
CONF_TIMEOUT = 'timeout'
CONF_WRITE_TIMEOUT = 'write_timeout'
DEFAULT_NAME = 'Acer Projector'
DEFAULT_TIMEOUT = 1
DEFAULT_WRITE_TIMEOUT = 1
LAMP_HOURS = 'Lamp Hours'
INPUT_SOURCE = 'Input Source'
ECO_MODE = 'ECO Mode' ECO_MODE = 'ECO Mode'
MODEL = 'Model'
ICON = 'mdi:projector'
INPUT_SOURCE = 'Input Source'
LAMP = 'Lamp' LAMP = 'Lamp'
LAMP_HOURS = 'Lamp Hours'
MODEL = 'Model'
# Commands known to the projector # Commands known to the projector
CMD_DICT = {LAMP: '* 0 Lamp ?\r', CMD_DICT = {LAMP: '* 0 Lamp ?\r',
@ -26,38 +45,34 @@ CMD_DICT = {LAMP: '* 0 Lamp ?\r',
STATE_ON: '* 0 IR 001\r', STATE_ON: '* 0 IR 001\r',
STATE_OFF: '* 0 IR 002\r'} STATE_OFF: '* 0 IR 002\r'}
_LOGGER = logging.getLogger(__name__) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
REQUIREMENTS = ['pyserial<=3.1'] vol.Required(CONF_FILENAME): cv.isfile,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
ICON = 'mdi:projector' vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
vol.Optional(CONF_WRITE_TIMEOUT, default=DEFAULT_WRITE_TIMEOUT):
cv.positive_int,
})
# pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None):
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Connect with serial port and return Acer Projector.""" """Connect with serial port and return Acer Projector."""
serial_port = config.get(CONF_FILENAME, None) serial_port = config.get(CONF_FILENAME)
name = config.get(CONF_NAME, 'Projector') name = config.get(CONF_NAME)
timeout = config.get('timeout', 1) timeout = config.get(CONF_TIMEOUT)
write_timeout = config.get('write_timeout', 1) write_timeout = config.get(CONF_WRITE_TIMEOUT)
if not serial_port: add_devices([AcerSwitch(serial_port, name, timeout, write_timeout)])
_LOGGER.error('Missing path of serial device')
return
devices = []
devices.append(AcerSwitch(serial_port, name, timeout, write_timeout))
add_devices_callback(devices)
class AcerSwitch(SwitchDevice): class AcerSwitch(SwitchDevice):
"""Represents an Acer Projector as an switch.""" """Represents an Acer Projector as an switch."""
def __init__(self, serial_port, name='Projector', def __init__(self, serial_port, name, timeout, write_timeout, **kwargs):
timeout=1, write_timeout=1, **kwargs):
"""Init of the Acer projector.""" """Init of the Acer projector."""
import serial import serial
self.ser = serial.Serial(port=serial_port, timeout=timeout, self.ser = serial.Serial(
write_timeout=write_timeout, **kwargs) port=serial_port, timeout=timeout, write_timeout=write_timeout,
**kwargs)
self._serial_port = serial_port self._serial_port = serial_port
self._name = name self._name = name
self._state = False self._state = False
@ -73,18 +88,17 @@ class AcerSwitch(SwitchDevice):
"""Write to the projector and read the return.""" """Write to the projector and read the return."""
import serial import serial
ret = "" ret = ""
# Sometimes the projector won't answer for no reason, # Sometimes the projector won't answer for no reason or the projector
# or the projector was disconnected during runtime. # was disconnected during runtime.
# Thisway the projector can be reconnected and will still # This way the projector can be reconnected and will still work
# work
try: try:
if not self.ser.is_open: if not self.ser.is_open:
self.ser.open() self.ser.open()
msg = msg.encode('utf-8') msg = msg.encode('utf-8')
self.ser.write(msg) self.ser.write(msg)
# size is an experience value there is no real limit. # Size is an experience value there is no real limit.
# AFAIK there is no limit and no end character so # AFAIK there is no limit and no end character so we will usually
# we will usually need to wait for timeout # need to wait for timeout
ret = self.ser.read_until(size=20).decode('utf-8') ret = self.ser.read_until(size=20).decode('utf-8')
except serial.SerialException: except serial.SerialException:
_LOGGER.error('Problem comunicating with %s', self._serial_port) _LOGGER.error('Problem comunicating with %s', self._serial_port)

View File

@ -346,7 +346,7 @@ pynx584==0.2
pyowm==2.4.0 pyowm==2.4.0
# homeassistant.components.switch.acer_projector # homeassistant.components.switch.acer_projector
pyserial<=3.1 pyserial==3.1.1
# homeassistant.components.device_tracker.snmp # homeassistant.components.device_tracker.snmp
# homeassistant.components.sensor.snmp # homeassistant.components.sensor.snmp