* Upgrade pylint to 1.8.1 * Fix no-else-return * Fix bad-whitespace * Fix too-many-nested-blocks * Fix raising-format-tuple See https://github.com/PyCQA/pylint/blob/master/doc/whatsnew/1.8.rst * Fix len-as-condition * Fix logging-not-lazy Not sure about that TEMP_CELSIUS though, but internally it's probably just like if you concatenated any other (variable) string * Fix stop-iteration-return * Fix useless-super-delegation * Fix trailing-comma-tuple Both of these seem to simply be bugs: * Nest: The value of self._humidity never seems to be used anywhere * Dovado: The called API method seems to expect a "normal" number * Fix redefined-argument-from-local * Fix consider-using-enumerate * Fix wrong-import-order * Fix arguments-differ * Fix missed no-else-return * Fix no-member and related * Fix signatures-differ * Revert "Upgrade pylint to 1.8.1" This reverts commit af78aa00f125a7d34add97b9d50c14db48412211. * Fix arguments-differ * except for device_tracker * Cleanup * Fix test using positional argument * Fix line too long I forgot to run flake8 - shame on me... 🙃 * Fix bad-option-value for 1.6.5 * Fix arguments-differ for device_tracker * Upgrade pylint to 1.8.2 * 👕 Fix missed no-member
91 lines
2.6 KiB
Python
91 lines
2.6 KiB
Python
"""
|
|
LIRC interface to receive signals from an infrared remote control.
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
https://home-assistant.io/components/lirc/
|
|
"""
|
|
# pylint: disable=import-error,no-member
|
|
import threading
|
|
import time
|
|
import logging
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.const import (
|
|
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_START)
|
|
|
|
REQUIREMENTS = ['python-lirc==1.2.3']
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
BUTTON_NAME = 'button_name'
|
|
|
|
DOMAIN = 'lirc'
|
|
|
|
EVENT_IR_COMMAND_RECEIVED = 'ir_command_received'
|
|
|
|
ICON = 'mdi:remote'
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
DOMAIN: vol.Schema({}),
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
def setup(hass, config):
|
|
"""Set up the LIRC capability."""
|
|
import lirc
|
|
|
|
# blocking=True gives unexpected behavior (multiple responses for 1 press)
|
|
# also by not blocking, we allow hass to shut down the thread gracefully
|
|
# on exit.
|
|
lirc.init('home-assistant', blocking=False)
|
|
lirc_interface = LircInterface(hass)
|
|
|
|
def _start_lirc(_event):
|
|
lirc_interface.start()
|
|
|
|
def _stop_lirc(_event):
|
|
lirc_interface.stopped.set()
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, _start_lirc)
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _stop_lirc)
|
|
|
|
return True
|
|
|
|
|
|
class LircInterface(threading.Thread):
|
|
"""
|
|
This interfaces with the lirc daemon to read IR commands.
|
|
|
|
When using lirc in blocking mode, sometimes repeated commands get produced
|
|
in the next read of a command so we use a thread here to just wait
|
|
around until a non-empty response is obtained from lirc.
|
|
"""
|
|
|
|
def __init__(self, hass):
|
|
"""Construct a LIRC interface object."""
|
|
threading.Thread.__init__(self)
|
|
self.daemon = True
|
|
self.stopped = threading.Event()
|
|
self.hass = hass
|
|
|
|
def run(self):
|
|
"""Run the loop of the LIRC interface thread."""
|
|
import lirc
|
|
_LOGGER.debug("LIRC interface thread started")
|
|
while not self.stopped.isSet():
|
|
try:
|
|
code = lirc.nextcode() # list; empty if no buttons pressed
|
|
except lirc.NextCodeError:
|
|
_LOGGER.warning("Error reading next code from LIRC")
|
|
code = None
|
|
# interpret result from python-lirc
|
|
if code:
|
|
code = code[0]
|
|
_LOGGER.info("Got new LIRC code %s", code)
|
|
self.hass.bus.fire(
|
|
EVENT_IR_COMMAND_RECEIVED, {BUTTON_NAME: code})
|
|
else:
|
|
time.sleep(0.2)
|
|
lirc.deinit()
|
|
_LOGGER.debug('LIRC interface thread stopped')
|