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 voluptuous for Acer projector switch #3077

Merged
merged 3 commits into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
88 changes: 51 additions & 37 deletions homeassistant/components/switch/acer_projector.py
Original file line number Diff line number Diff line change
@@ -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
their RS232 serial communication protocol.
For more details about this component, please refer to the documentation
at https://home-assistant.io/components/switch.acer_projector/
"""
import logging
import re

from homeassistant.components.switch import SwitchDevice
from homeassistant.const import (STATE_ON, STATE_OFF, STATE_UNKNOWN,
CONF_NAME, CONF_FILENAME)
import voluptuous as vol

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'
MODEL = 'Model'

ICON = 'mdi:projector'

INPUT_SOURCE = 'Input Source'

LAMP = 'Lamp'
LAMP_HOURS = 'Lamp Hours'

MODEL = 'Model'

# Commands known to the projector
CMD_DICT = {LAMP: '* 0 Lamp ?\r',
Expand All @@ -26,38 +45,34 @@
STATE_ON: '* 0 IR 001\r',
STATE_OFF: '* 0 IR 002\r'}

_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['pyserial<=3.1']
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_FILENAME): cv.isfile,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
vol.Optional(CONF_WRITE_TIMEOUT, default=DEFAULT_WRITE_TIMEOUT):
cv.positive_int,
})

ICON = 'mdi:projector'


# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Connect with serial port and return Acer Projector."""
serial_port = config.get(CONF_FILENAME, None)
name = config.get(CONF_NAME, 'Projector')
timeout = config.get('timeout', 1)
write_timeout = config.get('write_timeout', 1)

if not serial_port:
_LOGGER.error('Missing path of serial device')
return
serial_port = config.get(CONF_FILENAME)
name = config.get(CONF_NAME)
timeout = config.get(CONF_TIMEOUT)
write_timeout = config.get(CONF_WRITE_TIMEOUT)

devices = []
devices.append(AcerSwitch(serial_port, name, timeout, write_timeout))
add_devices_callback(devices)
add_devices([AcerSwitch(serial_port, name, timeout, write_timeout)])


class AcerSwitch(SwitchDevice):
"""Represents an Acer Projector as an switch."""

def __init__(self, serial_port, name='Projector',
timeout=1, write_timeout=1, **kwargs):
def __init__(self, serial_port, name, timeout, write_timeout, **kwargs):
"""Init of the Acer projector."""
import serial
self.ser = serial.Serial(port=serial_port, timeout=timeout,
write_timeout=write_timeout, **kwargs)
self.ser = serial.Serial(
port=serial_port, timeout=timeout, write_timeout=write_timeout,
**kwargs)
self._serial_port = serial_port
self._name = name
self._state = False
Expand All @@ -73,18 +88,17 @@ def _write_read(self, msg):
"""Write to the projector and read the return."""
import serial
ret = ""
# Sometimes the projector won't answer for no reason,
# or the projector was disconnected during runtime.
# Thisway the projector can be reconnected and will still
# work
# Sometimes the projector won't answer for no reason or the projector
# was disconnected during runtime.
# This way the projector can be reconnected and will still work
try:
if not self.ser.is_open:
self.ser.open()
msg = msg.encode('utf-8')
self.ser.write(msg)
# size is an experience value there is no real limit.
# AFAIK there is no limit and no end character so
# we will usually need to wait for timeout
# Size is an experience value there is no real limit.
# AFAIK there is no limit and no end character so we will usually
# need to wait for timeout
ret = self.ser.read_until(size=20).decode('utf-8')
except serial.SerialException:
_LOGGER.error('Problem comunicating with %s', self._serial_port)
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ pynx584==0.2
pyowm==2.3.2

# homeassistant.components.switch.acer_projector
pyserial<=3.1
pyserial==3.1.1

# homeassistant.components.device_tracker.snmp
# homeassistant.components.sensor.snmp
Expand Down