Skip to content
Snippets Groups Projects
Unverified Commit 25f15c11 authored by epenet's avatar epenet Committed by GitHub
Browse files

Use short-hand attributes in remote-rpi-gpio (#140263)

parent e831b1b2
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
from gpiozero import DigitalInputDevice
import requests import requests
import voluptuous as vol import voluptuous as vol
...@@ -48,10 +49,10 @@ def setup_platform( ...@@ -48,10 +49,10 @@ def setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the Raspberry PI GPIO devices.""" """Set up the Raspberry PI GPIO devices."""
address = config["host"] address = config[CONF_HOST]
invert_logic = config[CONF_INVERT_LOGIC] invert_logic = config[CONF_INVERT_LOGIC]
pull_mode = config[CONF_PULL_MODE] pull_mode = config[CONF_PULL_MODE]
ports = config["ports"] ports = config[CONF_PORTS]
bouncetime = config[CONF_BOUNCETIME] / 1000 bouncetime = config[CONF_BOUNCETIME] / 1000
devices = [] devices = []
...@@ -71,9 +72,11 @@ class RemoteRPiGPIOBinarySensor(BinarySensorEntity): ...@@ -71,9 +72,11 @@ class RemoteRPiGPIOBinarySensor(BinarySensorEntity):
_attr_should_poll = False _attr_should_poll = False
def __init__(self, name, sensor, invert_logic): def __init__(
self, name: str | None, sensor: DigitalInputDevice, invert_logic: bool
) -> None:
"""Initialize the RPi binary sensor.""" """Initialize the RPi binary sensor."""
self._name = name self._attr_name = name
self._invert_logic = invert_logic self._invert_logic = invert_logic
self._state = False self._state = False
self._sensor = sensor self._sensor = sensor
...@@ -90,20 +93,10 @@ class RemoteRPiGPIOBinarySensor(BinarySensorEntity): ...@@ -90,20 +93,10 @@ class RemoteRPiGPIOBinarySensor(BinarySensorEntity):
self._sensor.when_activated = read_gpio self._sensor.when_activated = read_gpio
@property @property
def name(self): def is_on(self) -> bool:
"""Return the name of the sensor."""
return self._name
@property
def is_on(self):
"""Return the state of the entity.""" """Return the state of the entity."""
return self._state != self._invert_logic return self._state != self._invert_logic
@property
def device_class(self):
"""Return the class of this sensor, from DEVICE_CLASSES."""
return
def update(self) -> None: def update(self) -> None:
"""Update the GPIO state.""" """Update the GPIO state."""
try: try:
......
...@@ -4,6 +4,7 @@ from __future__ import annotations ...@@ -4,6 +4,7 @@ from __future__ import annotations
from typing import Any from typing import Any
from gpiozero import LED
import voluptuous as vol import voluptuous as vol
from homeassistant.components.switch import ( from homeassistant.components.switch import (
...@@ -57,37 +58,23 @@ def setup_platform( ...@@ -57,37 +58,23 @@ def setup_platform(
class RemoteRPiGPIOSwitch(SwitchEntity): class RemoteRPiGPIOSwitch(SwitchEntity):
"""Representation of a Remote Raspberry Pi GPIO.""" """Representation of a Remote Raspberry Pi GPIO."""
_attr_assumed_state = True
_attr_should_poll = False _attr_should_poll = False
def __init__(self, name, led): def __init__(self, name: str | None, led: LED) -> None:
"""Initialize the pin.""" """Initialize the pin."""
self._name = name or DEVICE_DEFAULT_NAME self._attr_name = name or DEVICE_DEFAULT_NAME
self._state = False self._attr_is_on = False
self._switch = led self._switch = led
@property
def name(self):
"""Return the name of the switch."""
return self._name
@property
def assumed_state(self):
"""If unable to access real state of the entity."""
return True
@property
def is_on(self):
"""Return true if device is on."""
return self._state
def turn_on(self, **kwargs: Any) -> None: def turn_on(self, **kwargs: Any) -> None:
"""Turn the device on.""" """Turn the device on."""
write_output(self._switch, 1) write_output(self._switch, 1)
self._state = True self._attr_is_on = True
self.schedule_update_ha_state() self.schedule_update_ha_state()
def turn_off(self, **kwargs: Any) -> None: def turn_off(self, **kwargs: Any) -> None:
"""Turn the device off.""" """Turn the device off."""
write_output(self._switch, 0) write_output(self._switch, 0)
self._state = False self._attr_is_on = False
self.schedule_update_ha_state() self.schedule_update_ha_state()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment