-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a binary_sensor using the gpiozero
library
#12105
Conversation
btn = Button( | ||
port, | ||
pull_up=(pull_mode.upper() == 'UP'), | ||
bounce_time=float(bouncetime) / 1e3, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing whitespace
raise ValueError("invalid bouncetime %s", bouncetime) | ||
|
||
pin_factory = get_pinfactory(hostport) | ||
if pin_factory == None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comparison to None should be 'if cond is None:'
|
||
if pull_mode.upper() not in ('UP', 'DOWN'): | ||
raise ValueError("invalid pull_mode %s", pull_mode) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blank line contains whitespace
:param hostport: the remote host/port, None for local. | ||
""" | ||
from gpiozero import Button | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blank line contains whitespace
pin_factory = _LOCAL_FACTORY | ||
return pin_factory | ||
|
||
def setup_button(port, pull_mode, bouncetime, hostport): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected 2 blank lines, found 1
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, prepare_gpiozero) | ||
return True | ||
|
||
def close_remote_pinfactory(hostport): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected 2 blank lines, found 1
def cleanup_gpiozero(event): | ||
"""Stuff to do before stopping.""" | ||
for dev in _DEVICES: | ||
try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing whitespace
# Make the default pin factory 'mock' so that | ||
# it other pin factories can be loaded after import | ||
os.environ['GPIOZERO_PIN_FACTORY'] = 'mock' | ||
import gpiozero |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'gpiozero' imported but unused
_LOCAL_FACTORY = None | ||
|
||
# pylint: disable=no-member | ||
def setup(hass, config): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected 2 blank lines, found 1
self._state = False # if would be preferable to use None here | ||
|
||
_LOGGER.info("%s has been updated to state %s", | ||
self._name, self._state) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continuation line under-indented for visual indent
606cf59
to
d1e6983
Compare
_LOGGER.exception("%s has failed to update", self._name) | ||
self._reset() | ||
else: | ||
self._state = False # if would be preferable to use None here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at least two spaces before inline comment
|
||
@property | ||
def available(self): | ||
return self.btn != None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comparison to None should be 'if cond is not None:'
"""Read state from GPIO.""" | ||
self._state = device.is_pressed | ||
_LOGGER.info("%s has changed to %s", | ||
self._name, self._state) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continuation line under-indented for visual indent
|
||
if self._btn is None: | ||
_LOGGER.error("failed to create button %s on port %s", | ||
self._name, self._port) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continuation line under-indented for visual indent
if self._btn is None and self._hostport: | ||
|
||
_LOGGER.debug("creating button %s on port %s", | ||
self._name, self._port) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continuation line under-indented for visual indent
"""Represent a binary sensor that uses Raspberry Pi GPIO via gpiozero""" | ||
|
||
def __init__(self, name, port, pull_mode, bouncetime, invert_logic, | ||
hostport): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continuation line under-indented for visual indent
_LOGGER.exception("%s has failed to update", self._name) | ||
self._reset() | ||
else: | ||
self._state = False # if would be preferable to use None here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at least two spaces before inline comment
|
||
@property | ||
def available(self): | ||
return self.btn != None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comparison to None should be 'if cond is not None:'
"""Read state from GPIO.""" | ||
self._state = device.is_pressed | ||
_LOGGER.info("%s has changed to %s", | ||
self._name, self._state) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continuation line under-indented for visual indent
|
||
if self._btn is None: | ||
_LOGGER.error("failed to create button %s on port %s", | ||
self._name, self._port) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continuation line under-indented for visual indent
if self._btn is None and self._hostport: | ||
|
||
_LOGGER.debug("creating button %s on port %s", | ||
self._name, self._port) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continuation line under-indented for visual indent
"""Represent a binary sensor that uses Raspberry Pi GPIO via gpiozero""" | ||
|
||
def __init__(self, name, port, pull_mode, bouncetime, invert_logic, | ||
hostport): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continuation line under-indented for visual indent
d1e6983
to
c863b44
Compare
The `gpiozero` library provides a unified interface for both local GPIO pins and remote GPIO pins (via `pigpiod`). This allows it to replace the standard `rpi_gpio` component while also adding new capabilities to easily interact with GPIO pins on networked Raspberry Pis.
c863b44
to
db86205
Compare
This comment has been minimized.
This comment has been minimized.
Component names are based on what they control, not on the name of the lib. So if this is a replacement for |
However, I'm a bit confused. |
So the standard `rpi_gpio` component talks only to local pins. I wanted to
be able to read GPIO pins on a remote Raspberry PI. This requires some
form of daemon running on the remote PI and a component within home
assistant to talk to that remote daemon.
For the daemon, I chose to use `pigpiod` because it is a included with
Raspbian and can easily be enabled with `raspi-config`. I implemented the
component using `gpiozero` because it provided a high-level API that worked
both with local and remote pins; the objective would be that eventually
this implementation (once feature complete) could replace `rpi_gpio` so
that only one component needed to be maintained.
I'm not partial to the name itself and only used that name to disambiguate
it from `rpi_gpio`. If you would prefer that I finish the implementation
so it is a drop-in replacement to `rpi_gpio` I can work down that path and
submit a new pull request.
…On Wed, May 2, 2018 at 2:17 PM, Paulus Schoutsen ***@***.***> wrote:
However, I'm a bit confused. rpi_gpio seems to talk to the pins, this PR
just talks to a daemon ?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#12105 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAOCIgiK8lGyL06INygSpaajsI0zKJKcks5tufg0gaJpZM4R0_a5>
.
|
Hmm. I wonder if we would want this at all or that we should just wait for #13876 to land so that someone can just connect Home Assistant on the remote Pi too. I wouldn't want to replace the current implementation because it would mean extra dependencies and usage of resources for no extra gain. |
This PR seems to gone stale. Please re-open it when you want to proceed. |
Description:
The
gpiozero
library provides a unified interface for both local GPIOpins and remote GPIO pins (via
pigpiod
). This allows it to replacethe standard
rpi_gpio
component while also adding new capabilities toeasily interact with GPIO pins on networked Raspberry Pis.
I chose to use
pigpiod
over other alternatives because it is the de-facto standard that is installed viaraspi-config
. Until this issue is resolved inpigpio
there is a possibility for the component to enter a deadlock state if network connectivity between Home Assistant andpigpiod
is lost. I have provided a pull request to address this issue; as a workaround my fork ofpigpio
can be installed instead.I chose to use
gpiozero
instead of directly using thepigpio
library so that one unified component could interact with both local and remote ports, reducing duplicate code.**Documentation has been started (here)[https://github.com/maihde/hass-rpi-gpiozero] and will be migrated to home-assistant.io when this component is accepted.
Example entry for
configuration.yaml
(if applicable):Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
tox
run successfully. Your PR cannot be merged unless tests passREQUIREMENTS
variable (example).requirements_all.txt
by runningscript/gen_requirements_all.py
..coveragerc
.If the code does not interact with devices:
tox
run successfully. Your PR cannot be merged unless tests pass