mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Changed LIRC component so that it just fires events on the bus.
This commit is contained in:
parent
4e5b5f2204
commit
c1f96aabb0
@ -13,12 +13,13 @@ import threading
|
|||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.helpers.entity import Entity
|
|
||||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||||
|
|
||||||
REQUIREMENTS = ['python-lirc>=1.2.1']
|
REQUIREMENTS = ['python-lirc>=1.2.1']
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
ICON = 'mdi:remote'
|
ICON = 'mdi:remote'
|
||||||
|
EVENT_BUTTON_PRESSED = 'button_pressed'
|
||||||
|
BUTTON_NAME = 'button_name'
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
@ -31,43 +32,18 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
# blocking=True gives unexpected behavior (multiple responses for 1 press)
|
# 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.init('home-assistant', blocking=False)
|
||||||
sensor = LircSensor()
|
lirc_interface = LircInterface(hass)
|
||||||
add_devices([sensor])
|
lirc_interface.start()
|
||||||
|
|
||||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, sensor.stop)
|
def _stop_lirc(_event):
|
||||||
|
lirc_interface.stopped.set()
|
||||||
|
|
||||||
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _stop_lirc)
|
||||||
|
|
||||||
class LircSensor(Entity):
|
return True
|
||||||
"""Sensor entity for LIRC."""
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
"""Construct a LircSensor entity."""
|
|
||||||
_LOGGER.info('Initializing LIRC sensor')
|
|
||||||
Entity.__init__(self, *args, **kwargs)
|
|
||||||
self.last_key_pressed = ''
|
|
||||||
self._lirc_interface = LircInterface(self)
|
|
||||||
self._lirc_interface.start()
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Name of lirc sensor."""
|
|
||||||
return 'lirc'
|
|
||||||
|
|
||||||
@property
|
|
||||||
def state(self):
|
|
||||||
"""State of LIRC sensor."""
|
|
||||||
return self.last_key_pressed
|
|
||||||
|
|
||||||
def update_state(self, new_state):
|
|
||||||
"""Inform system of update when they occur."""
|
|
||||||
self.last_key_pressed = new_state
|
|
||||||
self.update_ha_state()
|
|
||||||
|
|
||||||
def stop(self, _event):
|
|
||||||
"""Kill the helper thread on stop."""
|
|
||||||
_LOGGER.info('Ending LIRC interface thread')
|
|
||||||
self._lirc_interface.stopped.set()
|
|
||||||
|
|
||||||
|
|
||||||
class LircInterface(threading.Thread):
|
class LircInterface(threading.Thread):
|
||||||
@ -79,28 +55,22 @@ class LircInterface(threading.Thread):
|
|||||||
around until a non-empty response is obtained from lirc.
|
around until a non-empty response is obtained from lirc.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, hass):
|
||||||
"""Construct a LIRC interface object."""
|
"""Construct a LIRC interface object."""
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
self.stopped = threading.Event()
|
self.stopped = threading.Event()
|
||||||
self._parent = parent
|
self.hass = hass
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
"""Main loop of LIRC interface thread."""
|
"""Main loop of LIRC interface thread."""
|
||||||
import lirc
|
import lirc
|
||||||
while not self.stopped.isSet():
|
while not self.stopped.isSet():
|
||||||
code = lirc.nextcode() # list; empty if no buttons pressed
|
code = lirc.nextcode() # list; empty if no buttons pressed
|
||||||
|
|
||||||
# interpret result from python-lirc
|
# interpret result from python-lirc
|
||||||
if code:
|
if code:
|
||||||
code = code[0]
|
code = code[0]
|
||||||
else:
|
|
||||||
code = ''
|
|
||||||
|
|
||||||
# update if changed.
|
|
||||||
if code != self._parent.state:
|
|
||||||
_LOGGER.info('Got new LIRC code %s', code)
|
_LOGGER.info('Got new LIRC code %s', code)
|
||||||
self._parent.update_state(code)
|
self.hass.bus.fire(EVENT_BUTTON_PRESSED, {BUTTON_NAME: code})
|
||||||
else:
|
else:
|
||||||
time.sleep(0.1) # avoid high CPU in this thread
|
time.sleep(0.1) # avoid high CPU in this thread
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user