Remove global variable from arduino (#33718)
* Remove global variable from arduino * Run isort
This commit is contained in:
parent
983ed8b8b4
commit
40ce8f8c9c
3 changed files with 27 additions and 21 deletions
|
@ -14,8 +14,6 @@ import homeassistant.helpers.config_validation as cv
|
|||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
BOARD = None
|
||||
|
||||
DOMAIN = "arduino"
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
|
@ -28,15 +26,15 @@ def setup(hass, config):
|
|||
|
||||
port = config[DOMAIN][CONF_PORT]
|
||||
|
||||
global BOARD # pylint: disable=global-statement
|
||||
board = None
|
||||
try:
|
||||
BOARD = ArduinoBoard(port)
|
||||
board = ArduinoBoard(port)
|
||||
except (serial.serialutil.SerialException, FileNotFoundError):
|
||||
_LOGGER.error("Your port %s is not accessible", port)
|
||||
return False
|
||||
|
||||
try:
|
||||
if BOARD.get_firmata()[1] <= 2:
|
||||
if board.get_firmata()[1] <= 2:
|
||||
_LOGGER.error("The StandardFirmata sketch should be 2.2 or newer")
|
||||
return False
|
||||
except IndexError:
|
||||
|
@ -47,13 +45,14 @@ def setup(hass, config):
|
|||
|
||||
def stop_arduino(event):
|
||||
"""Stop the Arduino service."""
|
||||
BOARD.disconnect()
|
||||
board.disconnect()
|
||||
|
||||
def start_arduino(event):
|
||||
"""Start the Arduino service."""
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_arduino)
|
||||
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_arduino)
|
||||
hass.data[DOMAIN] = board
|
||||
|
||||
return True
|
||||
|
||||
|
|
|
@ -3,12 +3,13 @@ import logging
|
|||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import arduino
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.const import CONF_NAME
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
from . import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_PINS = "pins"
|
||||
|
@ -23,7 +24,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the Arduino platform."""
|
||||
if arduino.BOARD is None:
|
||||
board = hass.data[DOMAIN]
|
||||
|
||||
if board is None:
|
||||
_LOGGER.error("A connection has not been made to the Arduino board")
|
||||
return False
|
||||
|
||||
|
@ -31,14 +34,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
|
||||
sensors = []
|
||||
for pinnum, pin in pins.items():
|
||||
sensors.append(ArduinoSensor(pin.get(CONF_NAME), pinnum, CONF_TYPE))
|
||||
sensors.append(ArduinoSensor(pin.get(CONF_NAME), pinnum, CONF_TYPE, board))
|
||||
add_entities(sensors)
|
||||
|
||||
|
||||
class ArduinoSensor(Entity):
|
||||
"""Representation of an Arduino Sensor."""
|
||||
|
||||
def __init__(self, name, pin, pin_type):
|
||||
def __init__(self, name, pin, pin_type, board):
|
||||
"""Initialize the sensor."""
|
||||
self._pin = pin
|
||||
self._name = name
|
||||
|
@ -46,7 +49,8 @@ class ArduinoSensor(Entity):
|
|||
self.direction = "in"
|
||||
self._value = None
|
||||
|
||||
arduino.BOARD.set_mode(self._pin, self.direction, self.pin_type)
|
||||
board.set_mode(self._pin, self.direction, self.pin_type)
|
||||
self._board = board
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
|
@ -60,4 +64,4 @@ class ArduinoSensor(Entity):
|
|||
|
||||
def update(self):
|
||||
"""Get the latest value from the pin."""
|
||||
self._value = arduino.BOARD.get_analog_inputs()[self._pin][1]
|
||||
self._value = self._board.get_analog_inputs()[self._pin][1]
|
||||
|
|
|
@ -3,11 +3,12 @@ import logging
|
|||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import arduino
|
||||
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
||||
from homeassistant.const import CONF_NAME
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from . import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_PINS = "pins"
|
||||
|
@ -30,8 +31,10 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the Arduino platform."""
|
||||
board = hass.data[DOMAIN]
|
||||
|
||||
# Verify that Arduino board is present
|
||||
if arduino.BOARD is None:
|
||||
if board is None:
|
||||
_LOGGER.error("A connection has not been made to the Arduino board")
|
||||
return False
|
||||
|
||||
|
@ -39,14 +42,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
|
||||
switches = []
|
||||
for pinnum, pin in pins.items():
|
||||
switches.append(ArduinoSwitch(pinnum, pin))
|
||||
switches.append(ArduinoSwitch(pinnum, pin, board))
|
||||
add_entities(switches)
|
||||
|
||||
|
||||
class ArduinoSwitch(SwitchDevice):
|
||||
"""Representation of an Arduino switch."""
|
||||
|
||||
def __init__(self, pin, options):
|
||||
def __init__(self, pin, options, board):
|
||||
"""Initialize the Pin."""
|
||||
self._pin = pin
|
||||
self._name = options.get(CONF_NAME)
|
||||
|
@ -56,13 +59,13 @@ class ArduinoSwitch(SwitchDevice):
|
|||
self._state = options.get(CONF_INITIAL)
|
||||
|
||||
if options.get(CONF_NEGATE):
|
||||
self.turn_on_handler = arduino.BOARD.set_digital_out_low
|
||||
self.turn_off_handler = arduino.BOARD.set_digital_out_high
|
||||
self.turn_on_handler = board.set_digital_out_low
|
||||
self.turn_off_handler = board.set_digital_out_high
|
||||
else:
|
||||
self.turn_on_handler = arduino.BOARD.set_digital_out_high
|
||||
self.turn_off_handler = arduino.BOARD.set_digital_out_low
|
||||
self.turn_on_handler = board.set_digital_out_high
|
||||
self.turn_off_handler = board.set_digital_out_low
|
||||
|
||||
arduino.BOARD.set_mode(self._pin, self.direction, self.pin_type)
|
||||
board.set_mode(self._pin, self.direction, self.pin_type)
|
||||
(self.turn_on_handler if self._state else self.turn_off_handler)(pin)
|
||||
|
||||
@property
|
||||
|
|
Loading…
Add table
Reference in a new issue