mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Make the rpi_rf component thread-safe using an RLock (#11487)
* Make the rpi_rf component thread-safe The previous implementation suffered from race conditions when two rpi_rf switches are triggered at the same time. This implementation uses an RLock to give one thread at a time exclusive access to the rfdevice object. * cleanup * fix lint
This commit is contained in:
parent
d2b6660881
commit
cf612c3d5b
@ -48,18 +48,20 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Find and return switches controlled by a generic RF device via GPIO."""
|
"""Find and return switches controlled by a generic RF device via GPIO."""
|
||||||
import rpi_rf
|
import rpi_rf
|
||||||
|
from threading import RLock
|
||||||
|
|
||||||
gpio = config.get(CONF_GPIO)
|
gpio = config.get(CONF_GPIO)
|
||||||
rfdevice = rpi_rf.RFDevice(gpio)
|
rfdevice = rpi_rf.RFDevice(gpio)
|
||||||
|
rfdevice_lock = RLock()
|
||||||
switches = config.get(CONF_SWITCHES)
|
switches = config.get(CONF_SWITCHES)
|
||||||
|
|
||||||
devices = []
|
devices = []
|
||||||
for dev_name, properties in switches.items():
|
for dev_name, properties in switches.items():
|
||||||
devices.append(
|
devices.append(
|
||||||
RPiRFSwitch(
|
RPiRFSwitch(
|
||||||
hass,
|
|
||||||
properties.get(CONF_NAME, dev_name),
|
properties.get(CONF_NAME, dev_name),
|
||||||
rfdevice,
|
rfdevice,
|
||||||
|
rfdevice_lock,
|
||||||
properties.get(CONF_PROTOCOL),
|
properties.get(CONF_PROTOCOL),
|
||||||
properties.get(CONF_PULSELENGTH),
|
properties.get(CONF_PULSELENGTH),
|
||||||
properties.get(CONF_SIGNAL_REPETITIONS),
|
properties.get(CONF_SIGNAL_REPETITIONS),
|
||||||
@ -79,13 +81,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
class RPiRFSwitch(SwitchDevice):
|
class RPiRFSwitch(SwitchDevice):
|
||||||
"""Representation of a GPIO RF switch."""
|
"""Representation of a GPIO RF switch."""
|
||||||
|
|
||||||
def __init__(self, hass, name, rfdevice, protocol, pulselength,
|
def __init__(self, name, rfdevice, lock, protocol, pulselength,
|
||||||
signal_repetitions, code_on, code_off):
|
signal_repetitions, code_on, code_off):
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
self._hass = hass
|
|
||||||
self._name = name
|
self._name = name
|
||||||
self._state = False
|
self._state = False
|
||||||
self._rfdevice = rfdevice
|
self._rfdevice = rfdevice
|
||||||
|
self._lock = lock
|
||||||
self._protocol = protocol
|
self._protocol = protocol
|
||||||
self._pulselength = pulselength
|
self._pulselength = pulselength
|
||||||
self._code_on = code_on
|
self._code_on = code_on
|
||||||
@ -109,9 +111,10 @@ class RPiRFSwitch(SwitchDevice):
|
|||||||
|
|
||||||
def _send_code(self, code_list, protocol, pulselength):
|
def _send_code(self, code_list, protocol, pulselength):
|
||||||
"""Send the code(s) with a specified pulselength."""
|
"""Send the code(s) with a specified pulselength."""
|
||||||
_LOGGER.info("Sending code(s): %s", code_list)
|
with self._lock:
|
||||||
for code in code_list:
|
_LOGGER.info("Sending code(s): %s", code_list)
|
||||||
self._rfdevice.tx_code(code, protocol, pulselength)
|
for code in code_list:
|
||||||
|
self._rfdevice.tx_code(code, protocol, pulselength)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def turn_on(self):
|
def turn_on(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user