homeassistant-config/custom_components/lirc_network/__init__.py

63 lines
2.2 KiB
Python
Raw Normal View History

"""Integration of lirc network service"""
import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.const import CONF_HOST, CONF_PORT
from ..reconnecting_client import ReconnectingClient
2019-11-30 16:56:49 +01:00
EVENT_IR_COMMAND_RECEIVED = "ir_command_received"
BUTTON_NAME = "button_name"
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'lirc_network'
REMOTE_NAME = 'remote'
REPEAT_COUNTER = 'repeat_counter'
LIRC_HOST = 'host'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema(vol.All([{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_PORT, default=8765): cv.port,
}]))
}, extra=vol.ALLOW_EXTRA)
async def async_setup(hass, config):
for config_element in config[DOMAIN]:
2019-11-30 16:56:49 +01:00
_LOGGER.warning(f'Connecting to {config_element[CONF_HOST]} at {config_element[CONF_PORT]}')
connection = LircConnection(hass, config_element)
await connection.start()
2019-11-30 16:56:49 +01:00
_LOGGER.warning("About to return true in async_setup")
return True
class LircConnection(ReconnectingClient):
def __init__(self, hass, config):
super().__init__(hass, config[CONF_HOST], config[CONF_PORT], "lirc_network",
receive_line_callback=self._process_line,
connection_status_changed_callback=self._connection_state_changed)
async def _connection_state_changed(self, _):
pass
async def _process_line(self, line):
# Example msg:
# 0000000000001795 00 Down Hauppauge_350
# 0000000000001795 01 Down Hauppauge_350
splitted_line = line.split()
if len(splitted_line) != 4:
_LOGGER.warning(f'Ignoring LIRC message from host {self._host}: "{line}"')
return
else:
code, repeat_counter, key_name, remote_name = splitted_line
repeat_counter = int(repeat_counter, 16) # repeat code is hexadecimal
key_name = key_name.lower()
data = {BUTTON_NAME: key_name,
REMOTE_NAME: remote_name,
REPEAT_COUNTER: repeat_counter,
LIRC_HOST: self._host}
_LOGGER.info(f"Got new LIRC network code {data}")
self.hass.bus.fire(EVENT_IR_COMMAND_RECEIVED, data)