From 81c4d95afe9577001f4f12ef2b5a5d3ccf4dc396 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Wed, 21 Jul 2021 05:31:50 -0400 Subject: [PATCH] Use entity class attributes for arduino (#52677) * Use entity class attributes for arduino * Revert state * tweak * tweak --- homeassistant/components/arduino/sensor.py | 19 +++-------------- homeassistant/components/arduino/switch.py | 24 ++++++---------------- 2 files changed, 9 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/arduino/sensor.py b/homeassistant/components/arduino/sensor.py index 588a652660a..fa624a7d167 100644 --- a/homeassistant/components/arduino/sensor.py +++ b/homeassistant/components/arduino/sensor.py @@ -35,24 +35,11 @@ class ArduinoSensor(SensorEntity): def __init__(self, name, pin, pin_type, board): """Initialize the sensor.""" self._pin = pin - self._name = name - self.pin_type = pin_type - self.direction = "in" - self._value = None + self._attr_name = name - board.set_mode(self._pin, self.direction, self.pin_type) + board.set_mode(self._pin, "in", pin_type) self._board = board - @property - def state(self): - """Return the state of the sensor.""" - return self._value - - @property - def name(self): - """Get the name of the sensor.""" - return self._name - def update(self): """Get the latest value from the pin.""" - self._value = self._board.get_analog_inputs()[self._pin][1] + self._attr_state = self._board.get_analog_inputs()[self._pin][1] diff --git a/homeassistant/components/arduino/switch.py b/homeassistant/components/arduino/switch.py index 6ee742fd506..9368426b38e 100644 --- a/homeassistant/components/arduino/switch.py +++ b/homeassistant/components/arduino/switch.py @@ -43,11 +43,9 @@ class ArduinoSwitch(SwitchEntity): def __init__(self, pin, options, board): """Initialize the Pin.""" self._pin = pin - self._name = options[CONF_NAME] - self.pin_type = CONF_TYPE - self.direction = "out" + self._attr_name = options[CONF_NAME] - self._state = options[CONF_INITIAL] + self._attr_is_on = options[CONF_INITIAL] if options[CONF_NEGATE]: self.turn_on_handler = board.set_digital_out_low @@ -56,25 +54,15 @@ class ArduinoSwitch(SwitchEntity): self.turn_on_handler = board.set_digital_out_high self.turn_off_handler = board.set_digital_out_low - board.set_mode(self._pin, self.direction, self.pin_type) - (self.turn_on_handler if self._state else self.turn_off_handler)(pin) - - @property - def name(self): - """Get the name of the pin.""" - return self._name - - @property - def is_on(self): - """Return true if pin is high/on.""" - return self._state + board.set_mode(pin, "out", CONF_TYPE) + (self.turn_on_handler if self.is_on else self.turn_off_handler)(pin) def turn_on(self, **kwargs): """Turn the pin to high/on.""" - self._state = True + self._attr_is_on = True self.turn_on_handler(self._pin) def turn_off(self, **kwargs): """Turn the pin to low/off.""" - self._state = False + self._attr_is_on = False self.turn_off_handler(self._pin)