Remove global variable from arduino (#33718)

* Remove global variable from arduino

* Run isort
This commit is contained in:
springstan 2020-04-05 23:27:58 +02:00 committed by GitHub
parent 983ed8b8b4
commit 40ce8f8c9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 21 deletions

View file

@ -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]