boneIO 0.1.0.dev14__tar.gz → 0.1.0.dev15__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/PKG-INFO +2 -2
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/bonecli.py +2 -2
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/const.py +55 -30
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/helper/__init__.py +15 -1
- boneio-0.1.0.dev15/boneio/helper/gpio.py +79 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/helper/ha_discovery.py +20 -1
- boneio-0.1.0.dev15/boneio/input/__init__.py +5 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/input/gpio.py +9 -36
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/manager.py +57 -19
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/oled.py +2 -3
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/relay/gpio.py +2 -1
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/schema.yaml +38 -1
- boneio-0.1.0.dev15/boneio/sensor/__init__.py +5 -0
- boneio-0.1.0.dev15/boneio/sensor/gpio.py +32 -0
- boneio-0.1.0.dev15/boneio/version.py +2 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio.egg-info/PKG-INFO +2 -2
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio.egg-info/SOURCES.txt +1 -0
- boneio-0.1.0.dev14/boneio/helper/gpio.py +0 -38
- boneio-0.1.0.dev14/boneio/input/__init__.py +0 -5
- boneio-0.1.0.dev14/boneio/sensor/__init__.py +0 -4
- boneio-0.1.0.dev14/boneio/version.py +0 -2
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/MANIFEST.in +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/README.md +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/__init__.py +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/fonts/danube__.ttf +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/helper/exceptions.py +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/helper/oled.py +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/helper/stats.py +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/helper/yaml.py +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/mqtt_client.py +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/relay/__init__.py +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/relay/basic.py +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/relay/mcp.py +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio/sensor/lm75.py +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio.egg-info/dependency_links.txt +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio.egg-info/entry_points.txt +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio.egg-info/requires.txt +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/boneio.egg-info/top_level.txt +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/requirements.txt +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/setup.cfg +0 -0
- {boneio-0.1.0.dev14 → boneio-0.1.0.dev15}/setup.py +0 -0
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: boneio
|
|
3
|
-
Version: 0.1.0.
|
|
3
|
+
Version: 0.1.0.dev15
|
|
4
4
|
Summary: Python Helper for BoneIO
|
|
5
5
|
Home-page: https://github.com/maciejk1984/boneIO
|
|
6
6
|
License: GNU General Public License v3.0
|
|
7
|
-
Download-URL: https://github.com/maciejk1984/boneIO/archive/0.1.0.
|
|
7
|
+
Download-URL: https://github.com/maciejk1984/boneIO/archive/0.1.0.dev15.zip
|
|
8
8
|
Platform: UNKNOWN
|
|
9
9
|
Classifier: Programming Language :: Python
|
|
10
10
|
Classifier: Programming Language :: Python :: 3.7
|
|
@@ -8,7 +8,7 @@ from colorlog import ColoredFormatter
|
|
|
8
8
|
|
|
9
9
|
from boneio.const import (
|
|
10
10
|
ENABLED,
|
|
11
|
-
|
|
11
|
+
INPUT,
|
|
12
12
|
HA_DISCOVERY,
|
|
13
13
|
HOST,
|
|
14
14
|
MCP23017,
|
|
@@ -135,7 +135,7 @@ async def run(ctx, debug: int, config: str, mqttpassword: str = ""):
|
|
|
135
135
|
send_message=client.send_message,
|
|
136
136
|
topic_prefix=_config[MQTT][TOPIC_PREFIX],
|
|
137
137
|
relay_pins=_config.get(OUTPUT, []),
|
|
138
|
-
input_pins=_config.get(
|
|
138
|
+
input_pins=_config.get(INPUT, []),
|
|
139
139
|
ha_discovery=_config[MQTT][HA_DISCOVERY][ENABLED],
|
|
140
140
|
ha_discovery_prefix=_config[MQTT][HA_DISCOVERY][TOPIC_PREFIX],
|
|
141
141
|
mcp23017=_config.get(MCP23017, []),
|
|
@@ -1,31 +1,15 @@
|
|
|
1
1
|
# from typing import Literal
|
|
2
2
|
from datetime import timedelta
|
|
3
|
-
|
|
3
|
+
from Adafruit_BBIO.GPIO import HIGH, LOW, BOTH, FALLING
|
|
4
4
|
from typing_extensions import Literal
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
DELAY_DURATION = 0.1
|
|
9
|
-
SECOND_DELAY_DURATION = 0.3
|
|
6
|
+
|
|
7
|
+
# MISCELLANEOUS CONSTS
|
|
10
8
|
RELAY = "relay"
|
|
11
|
-
SINGLE = "single"
|
|
12
|
-
DOUBLE = "double"
|
|
13
|
-
LONG = "long"
|
|
14
|
-
ONLINE = "online"
|
|
15
9
|
ON = "ON"
|
|
16
10
|
OFF = "OFF"
|
|
17
11
|
STATE = "state"
|
|
18
|
-
PAHO = "paho.mqtt.client"
|
|
19
|
-
ClickTypes = Literal[SINGLE, DOUBLE, LONG]
|
|
20
|
-
MQTT = "mqtt"
|
|
21
|
-
HOST = "host"
|
|
22
|
-
USERNAME = "username"
|
|
23
|
-
PASSWORD = "password"
|
|
24
|
-
TOPIC_PREFIX = "topic_prefix"
|
|
25
|
-
HOMEASSISTANT = "homeassistant"
|
|
26
|
-
HA_DISCOVERY = "ha_discovery"
|
|
27
12
|
ENABLED = "enabled"
|
|
28
|
-
GPIO_INPUT = "gpio_input"
|
|
29
13
|
OUTPUT = "output"
|
|
30
14
|
PIN = "pin"
|
|
31
15
|
ID = "id"
|
|
@@ -34,17 +18,27 @@ GPIO = "gpio"
|
|
|
34
18
|
ACTIONS = "actions"
|
|
35
19
|
ACTION = "action"
|
|
36
20
|
OUTPUT = "output"
|
|
37
|
-
ADDRESS = "address"
|
|
38
|
-
MCP23017 = "mcp23017"
|
|
39
21
|
SWITCH = "switch"
|
|
40
22
|
CONFIG_PIN = "/usr/bin/config-pin"
|
|
41
|
-
|
|
42
|
-
|
|
23
|
+
|
|
24
|
+
# TIMINGS FOR BUTTONS
|
|
25
|
+
DEBOUNCE_DURATION = timedelta(seconds=0.2)
|
|
26
|
+
LONG_PRESS_DURATION = timedelta(seconds=0.7)
|
|
27
|
+
DELAY_DURATION = 0.1
|
|
28
|
+
SECOND_DELAY_DURATION = 0.3
|
|
29
|
+
|
|
30
|
+
# HA CONSTS
|
|
31
|
+
HOMEASSISTANT = "homeassistant"
|
|
32
|
+
HA_DISCOVERY = "ha_discovery"
|
|
43
33
|
HA_TYPE = "ha_type"
|
|
34
|
+
SHOW_HA = "show_in_ha"
|
|
35
|
+
|
|
36
|
+
# OLED CONST
|
|
37
|
+
OLED = "oled"
|
|
38
|
+
FONTS = "fonts"
|
|
39
|
+
OLED_PIN = "P9_41"
|
|
44
40
|
GIGABYTE = 1073741824
|
|
45
41
|
MEGABYTE = 1048576
|
|
46
|
-
OLED_PIN = "P9_41"
|
|
47
|
-
NUM_OF_SCREENS = 6
|
|
48
42
|
WIDTH = 128
|
|
49
43
|
UPTIME = "uptime"
|
|
50
44
|
NETWORK = "network"
|
|
@@ -53,11 +47,42 @@ DISK = "disk"
|
|
|
53
47
|
MEMORY = "memory"
|
|
54
48
|
SWAP = "swap"
|
|
55
49
|
WHITE = "white"
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
OledDataTypes = Literal[UPTIME, NETWORK, CPU, DISK, MEMORY, SWAP, OUTPUT]
|
|
59
|
-
INIT_SLEEP = "init_sleep"
|
|
50
|
+
|
|
51
|
+
# INPUT CONST
|
|
60
52
|
INPUT = "input"
|
|
53
|
+
SINGLE = "single"
|
|
54
|
+
DOUBLE = "double"
|
|
55
|
+
LONG = "long"
|
|
56
|
+
PRESSED = "pressed"
|
|
57
|
+
RELEASED = "released"
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# MQTT CONST
|
|
61
|
+
PAHO = "paho.mqtt.client"
|
|
62
|
+
MQTT = "mqtt"
|
|
63
|
+
HOST = "host"
|
|
64
|
+
USERNAME = "username"
|
|
65
|
+
PASSWORD = "password"
|
|
66
|
+
ONLINE = "online"
|
|
67
|
+
TOPIC_PREFIX = "topic_prefix"
|
|
68
|
+
|
|
69
|
+
# I2C and MCP CONST
|
|
70
|
+
ADDRESS = "address"
|
|
71
|
+
MCP23017 = "mcp23017"
|
|
72
|
+
MCP = "mcp"
|
|
73
|
+
MCP_ID = "mcp_id"
|
|
74
|
+
INIT_SLEEP = "init_sleep"
|
|
75
|
+
|
|
76
|
+
# SENSOR CONST
|
|
61
77
|
TEMPERATURE = "temperature"
|
|
62
78
|
SENSOR = "sensor"
|
|
63
|
-
|
|
79
|
+
BINARY_SENSOR = "binary_sensor"
|
|
80
|
+
LM75 = "lm75"
|
|
81
|
+
INPUT_SENSOR = "inputsensor"
|
|
82
|
+
|
|
83
|
+
# TYPING
|
|
84
|
+
ClickTypes = Literal[SINGLE, DOUBLE, LONG, PRESSED, RELEASED]
|
|
85
|
+
OledDataTypes = Literal[UPTIME, NETWORK, CPU, DISK, MEMORY, SWAP, OUTPUT]
|
|
86
|
+
Gpio_States = Literal[HIGH, LOW]
|
|
87
|
+
Gpio_Edges = Literal[BOTH, FALLING]
|
|
88
|
+
InputTypes = Literal[INPUT, INPUT_SENSOR]
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
"""Helper dir for BoneIO."""
|
|
2
|
-
from boneio.helper.gpio import
|
|
2
|
+
from boneio.helper.gpio import (
|
|
3
|
+
edge_detect,
|
|
4
|
+
setup_input,
|
|
5
|
+
setup_output,
|
|
6
|
+
read_input,
|
|
7
|
+
write_output,
|
|
8
|
+
GpioBaseClass,
|
|
9
|
+
configure_pin,
|
|
10
|
+
)
|
|
3
11
|
from boneio.helper.oled import make_font
|
|
4
12
|
from boneio.helper.stats import HostData, host_stats
|
|
5
13
|
from boneio.helper.yaml import CustomValidator, load_yaml_file
|
|
@@ -7,6 +15,7 @@ from boneio.helper.ha_discovery import (
|
|
|
7
15
|
ha_relay_availibilty_message,
|
|
8
16
|
ha_sensor_availibilty_message,
|
|
9
17
|
ha_sensor_temp_availibilty_message,
|
|
18
|
+
ha_binary_sensor_availibilty_message,
|
|
10
19
|
)
|
|
11
20
|
from boneio.helper.exceptions import GPIOInputException, I2CError
|
|
12
21
|
|
|
@@ -16,12 +25,17 @@ __all__ = [
|
|
|
16
25
|
"HostData",
|
|
17
26
|
"host_stats",
|
|
18
27
|
"setup_input",
|
|
28
|
+
"setup_output",
|
|
19
29
|
"edge_detect",
|
|
20
30
|
"read_input",
|
|
31
|
+
"write_output",
|
|
21
32
|
"make_font",
|
|
22
33
|
"ha_relay_availibilty_message",
|
|
23
34
|
"ha_sensor_availibilty_message",
|
|
24
35
|
"ha_sensor_temp_availibilty_message",
|
|
36
|
+
"ha_binary_sensor_availibilty_message",
|
|
25
37
|
"GPIOInputException",
|
|
26
38
|
"I2CError",
|
|
39
|
+
"GpioBaseClass",
|
|
40
|
+
"configure_pin",
|
|
27
41
|
]
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import logging
|
|
3
|
+
from Adafruit_BBIO import GPIO
|
|
4
|
+
|
|
5
|
+
from boneio.helper.exceptions import GPIOInputException
|
|
6
|
+
from boneio.const import LOW, FALLING, Gpio_Edges, Gpio_States
|
|
7
|
+
from typing import Callable
|
|
8
|
+
import subprocess
|
|
9
|
+
|
|
10
|
+
from boneio.const import (
|
|
11
|
+
CONFIG_PIN,
|
|
12
|
+
GPIO as GPIO_STR,
|
|
13
|
+
ClickTypes,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
_LOGGER = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def configure_pin(pin: str) -> None:
|
|
20
|
+
pin = f"{pin[0:3]}0{pin[3]}" if len(pin) == 4 else pin
|
|
21
|
+
_LOGGER.debug(f"Configuring pin for GPIO {pin}")
|
|
22
|
+
subprocess.run(
|
|
23
|
+
[CONFIG_PIN, pin, GPIO_STR],
|
|
24
|
+
stdout=subprocess.DEVNULL,
|
|
25
|
+
stderr=subprocess.STDOUT,
|
|
26
|
+
timeout=1,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def setup_output(pin: str):
|
|
31
|
+
"""Set up a GPIO as output."""
|
|
32
|
+
|
|
33
|
+
GPIO.setup(pin, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def setup_input(pin: str, pull_mode: str = "UP"):
|
|
37
|
+
"""Set up a GPIO as input."""
|
|
38
|
+
try:
|
|
39
|
+
GPIO.setup(pin, GPIO.IN, GPIO.PUD_DOWN if pull_mode == "DOWN" else GPIO.PUD_UP)
|
|
40
|
+
except (ValueError, SystemError) as err:
|
|
41
|
+
raise GPIOInputException(err)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def write_output(pin: str, value: str):
|
|
45
|
+
"""Write a value to a GPIO."""
|
|
46
|
+
|
|
47
|
+
GPIO.output(pin, value)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def read_input(pin: str, on_state: Gpio_States = LOW):
|
|
51
|
+
"""Read a value from a GPIO."""
|
|
52
|
+
return GPIO.input(pin) is on_state
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def edge_detect(pin: str, callback: Callable, bounce: int, edge: Gpio_Edges = FALLING):
|
|
56
|
+
"""Add detection for RISING and FALLING events."""
|
|
57
|
+
try:
|
|
58
|
+
GPIO.add_event_detect(gpio=pin, edge=edge, callback=callback, bouncetime=bounce)
|
|
59
|
+
except RuntimeError as err:
|
|
60
|
+
raise GPIOInputException(err)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class GpioBaseClass:
|
|
64
|
+
"""Base class for initialize GPIO"""
|
|
65
|
+
|
|
66
|
+
def __init__(
|
|
67
|
+
self, pin: str, press_callback: Callable[[ClickTypes, str], None], **kwargs
|
|
68
|
+
) -> None:
|
|
69
|
+
"""Setup GPIO Input Button"""
|
|
70
|
+
self._pin = pin
|
|
71
|
+
configure_pin(pin)
|
|
72
|
+
self._loop = asyncio.get_running_loop()
|
|
73
|
+
self._press_callback = press_callback
|
|
74
|
+
setup_input(self._pin)
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def is_pressed(self):
|
|
78
|
+
"""Is button pressed."""
|
|
79
|
+
return read_input(self._pin)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from boneio.version import __version__
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
from boneio.const import INPUT, OFF, ON, RELAY, STATE, SENSOR
|
|
4
|
+
from boneio.const import INPUT, OFF, ON, RELAY, STATE, SENSOR, INPUT_SENSOR
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
def ha_relay_availibilty_message(id: str, name: str, topic: str = "boneio"):
|
|
@@ -43,6 +43,25 @@ def ha_sensor_availibilty_message(id: str, name: str, topic: str = "boneio"):
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
|
|
46
|
+
def ha_binary_sensor_availibilty_message(id: str, name: str, topic: str = "boneio"):
|
|
47
|
+
"""Create availability topic for HA."""
|
|
48
|
+
return {
|
|
49
|
+
"availability": [{"topic": f"{topic}/{STATE}"}],
|
|
50
|
+
"device": {
|
|
51
|
+
"identifiers": [topic],
|
|
52
|
+
"manufacturer": "BoneIO",
|
|
53
|
+
"model": "BoneIO Relay Board",
|
|
54
|
+
"name": f"BoneIO {topic}",
|
|
55
|
+
"sw_version": __version__,
|
|
56
|
+
},
|
|
57
|
+
"payload_on": "pressed",
|
|
58
|
+
"payload_off": "released",
|
|
59
|
+
"name": name,
|
|
60
|
+
"state_topic": f"{topic}/{INPUT_SENSOR}/{id}",
|
|
61
|
+
"unique_id": f"{topic}{INPUT_SENSOR}{id}",
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
46
65
|
def ha_sensor_temp_availibilty_message(id: str, name: str, topic: str = "boneio"):
|
|
47
66
|
"""Create availability topic for HA."""
|
|
48
67
|
return {
|
|
@@ -1,51 +1,28 @@
|
|
|
1
1
|
"""GPIOInputButton to receive signals."""
|
|
2
|
-
import asyncio
|
|
3
2
|
import logging
|
|
4
|
-
import subprocess
|
|
5
3
|
from datetime import datetime
|
|
6
4
|
from functools import partial
|
|
7
|
-
from typing import Callable
|
|
8
5
|
|
|
9
6
|
from boneio.const import (
|
|
10
|
-
CONFIG_PIN,
|
|
11
7
|
DEBOUNCE_DURATION,
|
|
12
8
|
DELAY_DURATION,
|
|
13
9
|
DOUBLE,
|
|
14
|
-
GPIO,
|
|
15
10
|
LONG,
|
|
16
11
|
LONG_PRESS_DURATION,
|
|
17
12
|
SECOND_DELAY_DURATION,
|
|
18
13
|
SINGLE,
|
|
19
|
-
ClickTypes,
|
|
20
14
|
)
|
|
21
|
-
from boneio.helper import edge_detect,
|
|
15
|
+
from boneio.helper import edge_detect, GpioBaseClass
|
|
22
16
|
|
|
23
17
|
_LOGGER = logging.getLogger(__name__)
|
|
24
18
|
|
|
25
19
|
|
|
26
|
-
|
|
27
|
-
pin = f"{pin[0:3]}0{pin[3]}" if len(pin) == 4 else pin
|
|
28
|
-
_LOGGER.debug(f"Configuring pin for GPIO {pin}")
|
|
29
|
-
subprocess.run(
|
|
30
|
-
[CONFIG_PIN, pin, GPIO],
|
|
31
|
-
stdout=subprocess.DEVNULL,
|
|
32
|
-
stderr=subprocess.STDOUT,
|
|
33
|
-
timeout=1,
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
class GpioInputButton:
|
|
20
|
+
class GpioInputButton(GpioBaseClass):
|
|
38
21
|
"""Represent Gpio input switch."""
|
|
39
22
|
|
|
40
|
-
def __init__(
|
|
41
|
-
self, pin: str, press_callback: Callable[[ClickTypes, str], None], **kwargs
|
|
42
|
-
) -> None:
|
|
23
|
+
def __init__(self, **kwargs) -> None:
|
|
43
24
|
"""Setup GPIO Input Button"""
|
|
44
|
-
|
|
45
|
-
configure_pin(pin)
|
|
46
|
-
self._loop = asyncio.get_running_loop()
|
|
47
|
-
self._press_callback = press_callback
|
|
48
|
-
setup_input(self._pin)
|
|
25
|
+
super().__init__(**kwargs)
|
|
49
26
|
edge_detect(self._pin, callback=self._handle_press, bounce=25)
|
|
50
27
|
self._first_press_timestamp = None
|
|
51
28
|
self._is_long_press = False
|
|
@@ -53,7 +30,7 @@ class GpioInputButton:
|
|
|
53
30
|
self._second_check = False
|
|
54
31
|
_LOGGER.debug("Configured listening for input pin %s", self._pin)
|
|
55
32
|
|
|
56
|
-
def _handle_press(self, pin) -> None:
|
|
33
|
+
def _handle_press(self, pin: str) -> None:
|
|
57
34
|
"""Handle the button press callback"""
|
|
58
35
|
# Ignore if we are in a long press
|
|
59
36
|
if self._is_long_press:
|
|
@@ -85,11 +62,6 @@ class GpioInputButton:
|
|
|
85
62
|
self.check_press_length,
|
|
86
63
|
)
|
|
87
64
|
|
|
88
|
-
@property
|
|
89
|
-
def is_pressed(self):
|
|
90
|
-
"""Is button pressed."""
|
|
91
|
-
return read_input(self._pin)
|
|
92
|
-
|
|
93
65
|
def check_press_length(self) -> None:
|
|
94
66
|
"""Check if it's a single, double or long press"""
|
|
95
67
|
# Check if button is still pressed
|
|
@@ -109,7 +81,7 @@ class GpioInputButton:
|
|
|
109
81
|
diff = datetime.now() - self._first_press_timestamp
|
|
110
82
|
if not self._is_long_press and diff > LONG_PRESS_DURATION:
|
|
111
83
|
self._is_long_press = True
|
|
112
|
-
_LOGGER.debug("Long button press, call callback")
|
|
84
|
+
_LOGGER.debug("Long button press on pin %s, call callback", self._pin)
|
|
113
85
|
self._loop.call_soon_threadsafe(
|
|
114
86
|
partial(self._press_callback, LONG, self._pin)
|
|
115
87
|
)
|
|
@@ -129,7 +101,8 @@ class GpioInputButton:
|
|
|
129
101
|
if self._second_check:
|
|
130
102
|
if self._second_press_timestamp:
|
|
131
103
|
_LOGGER.debug(
|
|
132
|
-
"Double click event, roznica %s",
|
|
104
|
+
"Double click event on pin %s, roznica %s",
|
|
105
|
+
self._pin,
|
|
133
106
|
self._second_press_timestamp - self._first_press_timestamp,
|
|
134
107
|
)
|
|
135
108
|
self._loop.call_soon_threadsafe(
|
|
@@ -137,7 +110,7 @@ class GpioInputButton:
|
|
|
137
110
|
)
|
|
138
111
|
|
|
139
112
|
elif self._first_press_timestamp:
|
|
140
|
-
_LOGGER.debug("One click event, call callback")
|
|
113
|
+
_LOGGER.debug("One click event on pin %s, call callback", self._pin)
|
|
141
114
|
self._loop.call_soon_threadsafe(
|
|
142
115
|
partial(self._press_callback, SINGLE, self._pin)
|
|
143
116
|
)
|
|
@@ -28,19 +28,26 @@ from boneio.const import (
|
|
|
28
28
|
RELAY,
|
|
29
29
|
STATE,
|
|
30
30
|
ClickTypes,
|
|
31
|
+
InputTypes,
|
|
32
|
+
SENSOR,
|
|
33
|
+
SHOW_HA,
|
|
34
|
+
BINARY_SENSOR,
|
|
35
|
+
INPUT_SENSOR,
|
|
31
36
|
)
|
|
32
37
|
from boneio.helper import (
|
|
33
38
|
HostData,
|
|
34
39
|
ha_relay_availibilty_message,
|
|
35
40
|
ha_sensor_availibilty_message,
|
|
36
41
|
ha_sensor_temp_availibilty_message,
|
|
42
|
+
ha_binary_sensor_availibilty_message,
|
|
37
43
|
host_stats,
|
|
38
44
|
GPIOInputException,
|
|
39
45
|
I2CError,
|
|
40
46
|
)
|
|
41
47
|
from boneio.input import GpioInputButton
|
|
42
|
-
|
|
48
|
+
|
|
43
49
|
from boneio.relay import GpioRelay, MCPRelay
|
|
50
|
+
from boneio.sensor import GpioInputSensor
|
|
44
51
|
|
|
45
52
|
_LOGGER = logging.getLogger(__name__)
|
|
46
53
|
|
|
@@ -177,31 +184,60 @@ class Manager:
|
|
|
177
184
|
out.send_state,
|
|
178
185
|
)
|
|
179
186
|
|
|
180
|
-
def
|
|
187
|
+
def configure_input(gpio):
|
|
181
188
|
try:
|
|
182
189
|
pin = gpio[PIN]
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
availibilty_msg_func=
|
|
196
|
-
|
|
190
|
+
input_type = gpio.get(KIND)
|
|
191
|
+
if input_type == SENSOR:
|
|
192
|
+
GpioInputSensor(
|
|
193
|
+
pin=pin,
|
|
194
|
+
press_callback=lambda x, i: self.press_callback(
|
|
195
|
+
x=x,
|
|
196
|
+
inpin=i,
|
|
197
|
+
actions=gpio.get(ACTIONS, {}),
|
|
198
|
+
input_type=INPUT_SENSOR,
|
|
199
|
+
),
|
|
200
|
+
rest_pin=gpio,
|
|
201
|
+
)
|
|
202
|
+
availibilty_msg_func = ha_binary_sensor_availibilty_message
|
|
203
|
+
ha_type = BINARY_SENSOR
|
|
204
|
+
else:
|
|
205
|
+
GpioInputButton(
|
|
206
|
+
pin=pin,
|
|
207
|
+
press_callback=lambda x, i: self.press_callback(
|
|
208
|
+
x=x,
|
|
209
|
+
inpin=i,
|
|
210
|
+
actions=gpio.get(ACTIONS, {}),
|
|
211
|
+
input_type=INPUT,
|
|
212
|
+
),
|
|
213
|
+
rest_pin=gpio,
|
|
214
|
+
)
|
|
215
|
+
availibilty_msg_func = ha_sensor_availibilty_message
|
|
216
|
+
ha_type = SENSOR
|
|
217
|
+
if gpio.get(SHOW_HA, True):
|
|
218
|
+
print(
|
|
219
|
+
"INPUT sending disco",
|
|
220
|
+
ha_type,
|
|
221
|
+
ha_discovery_prefix,
|
|
222
|
+
availibilty_msg_func,
|
|
223
|
+
)
|
|
224
|
+
self.send_ha_autodiscovery(
|
|
225
|
+
id=pin,
|
|
226
|
+
name=gpio.get(ID, pin),
|
|
227
|
+
ha_type=ha_type,
|
|
228
|
+
prefix=ha_discovery_prefix,
|
|
229
|
+
availibilty_msg_func=availibilty_msg_func,
|
|
230
|
+
)
|
|
197
231
|
except GPIOInputException as err:
|
|
198
232
|
_LOGGER.error("This PIN %s can't be configured. %s", pin, err)
|
|
199
233
|
pass
|
|
200
234
|
|
|
201
235
|
for gpio in self._input_pins:
|
|
202
|
-
|
|
236
|
+
configure_input(gpio=gpio)
|
|
203
237
|
|
|
204
238
|
if oled:
|
|
239
|
+
from boneio.oled import Oled
|
|
240
|
+
|
|
205
241
|
self._host_data = HostData(
|
|
206
242
|
output=self._grouped_outputs,
|
|
207
243
|
lm75=self._lm75,
|
|
@@ -227,10 +263,12 @@ class Manager:
|
|
|
227
263
|
def get_tasks(self) -> Set[asyncio.Task]:
|
|
228
264
|
return self._tasks
|
|
229
265
|
|
|
230
|
-
def press_callback(
|
|
266
|
+
def press_callback(
|
|
267
|
+
self, x: ClickTypes, inpin: str, actions: dict, input_type: InputTypes = INPUT
|
|
268
|
+
) -> None:
|
|
231
269
|
"""Press callback to use in input gpio.
|
|
232
270
|
If relay input map is provided also toggle action on relay."""
|
|
233
|
-
topic = f"{self._topic_prefix}/{
|
|
271
|
+
topic = f"{self._topic_prefix}/{input_type}/{inpin}"
|
|
234
272
|
self.send_message(topic=topic, payload=x)
|
|
235
273
|
action = actions.get(x)
|
|
236
274
|
if action:
|
|
@@ -8,8 +8,7 @@ from luma.oled.device import sh1106
|
|
|
8
8
|
from PIL import ImageDraw
|
|
9
9
|
|
|
10
10
|
from boneio.const import CPU, DISK, MEMORY, NETWORK, OLED_PIN, SWAP, UPTIME, WHITE
|
|
11
|
-
from boneio.helper import HostData, edge_detect, make_font, setup_input
|
|
12
|
-
from boneio.input import configure_pin
|
|
11
|
+
from boneio.helper import HostData, configure_pin, edge_detect, make_font, setup_input
|
|
13
12
|
|
|
14
13
|
_LOGGER = logging.getLogger(__name__)
|
|
15
14
|
|
|
@@ -38,7 +37,7 @@ class Oled:
|
|
|
38
37
|
self._host_data = host_data
|
|
39
38
|
configure_pin(OLED_PIN)
|
|
40
39
|
setup_input(OLED_PIN)
|
|
41
|
-
edge_detect(OLED_PIN, callback=self._handle_press, bounce=50)
|
|
40
|
+
edge_detect(pin=OLED_PIN, callback=self._handle_press, bounce=50)
|
|
42
41
|
serial = i2c(port=2, address=0x3C)
|
|
43
42
|
self._device = sh1106(serial)
|
|
44
43
|
_LOGGER.debug("Configuring OLED screen.")
|
|
@@ -5,8 +5,9 @@ Created just in case.
|
|
|
5
5
|
|
|
6
6
|
import logging
|
|
7
7
|
|
|
8
|
-
from boneio.helper
|
|
8
|
+
from boneio.helper import read_input, setup_output, write_output
|
|
9
9
|
from boneio.relay.basic import BasicRelay
|
|
10
|
+
from boneio.const import HIGH, LOW
|
|
10
11
|
|
|
11
12
|
_LOGGER = logging.getLogger(__name__)
|
|
12
13
|
|
|
@@ -131,7 +131,7 @@ output:
|
|
|
131
131
|
label: If HA discovery is used device if relay is light or switch.
|
|
132
132
|
|
|
133
133
|
|
|
134
|
-
|
|
134
|
+
input:
|
|
135
135
|
type: list
|
|
136
136
|
meta:
|
|
137
137
|
label: GPIO inputs section
|
|
@@ -148,6 +148,19 @@ gpio_input:
|
|
|
148
148
|
required: True
|
|
149
149
|
meta:
|
|
150
150
|
label: PIN to use.
|
|
151
|
+
show_in_ha:
|
|
152
|
+
type: boolean
|
|
153
|
+
required: True
|
|
154
|
+
default: True
|
|
155
|
+
meta:
|
|
156
|
+
label: If you want you can disable discovering this input in HA.
|
|
157
|
+
kind:
|
|
158
|
+
type: string
|
|
159
|
+
allowed: ['switch', 'sensor']
|
|
160
|
+
default: switch
|
|
161
|
+
required: True
|
|
162
|
+
meta:
|
|
163
|
+
label: Either switch or sensor. If sensor then no clicks are detected, just simple on/off on gpio.
|
|
151
164
|
actions:
|
|
152
165
|
required: False
|
|
153
166
|
type: dict
|
|
@@ -188,3 +201,27 @@ gpio_input:
|
|
|
188
201
|
type: string
|
|
189
202
|
message:
|
|
190
203
|
type: string
|
|
204
|
+
pressed:
|
|
205
|
+
required: False
|
|
206
|
+
type: dict
|
|
207
|
+
schema:
|
|
208
|
+
action:
|
|
209
|
+
type: string
|
|
210
|
+
allowed: ['mqtt', 'output']
|
|
211
|
+
default: output
|
|
212
|
+
pin:
|
|
213
|
+
type: string
|
|
214
|
+
message:
|
|
215
|
+
type: string
|
|
216
|
+
released:
|
|
217
|
+
required: False
|
|
218
|
+
type: dict
|
|
219
|
+
schema:
|
|
220
|
+
action:
|
|
221
|
+
type: string
|
|
222
|
+
allowed: ['mqtt', 'output']
|
|
223
|
+
default: output
|
|
224
|
+
pin:
|
|
225
|
+
type: string
|
|
226
|
+
message:
|
|
227
|
+
type: string
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""GPIOInputButton to receive signals."""
|
|
2
|
+
import logging
|
|
3
|
+
from functools import partial
|
|
4
|
+
|
|
5
|
+
from boneio.const import BOTH, PRESSED, RELEASED
|
|
6
|
+
from boneio.helper import edge_detect, GpioBaseClass
|
|
7
|
+
|
|
8
|
+
_LOGGER = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class GpioInputSensor(GpioBaseClass):
|
|
12
|
+
"""Represent Gpio sensor on input boards."""
|
|
13
|
+
|
|
14
|
+
def __init__(self, **kwargs) -> None:
|
|
15
|
+
"""Setup GPIO Input Button"""
|
|
16
|
+
super().__init__(**kwargs)
|
|
17
|
+
edge_detect(self._pin, callback=self._handle_press, bounce=25, edge=BOTH)
|
|
18
|
+
_LOGGER.debug("Configured sensor pin %s", self._pin)
|
|
19
|
+
|
|
20
|
+
def _handle_press(self, pin: str) -> None:
|
|
21
|
+
"""Handle the button press callback"""
|
|
22
|
+
# Ignore if we are in a long press
|
|
23
|
+
if self.is_pressed:
|
|
24
|
+
_LOGGER.debug("Pressed event on pin %s", self._pin)
|
|
25
|
+
self._loop.call_soon_threadsafe(
|
|
26
|
+
partial(self._press_callback, PRESSED, self._pin)
|
|
27
|
+
)
|
|
28
|
+
else:
|
|
29
|
+
_LOGGER.debug("Released event on pin %s", self._pin)
|
|
30
|
+
self._loop.call_soon_threadsafe(
|
|
31
|
+
partial(self._press_callback, RELEASED, self._pin)
|
|
32
|
+
)
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: boneio
|
|
3
|
-
Version: 0.1.0.
|
|
3
|
+
Version: 0.1.0.dev15
|
|
4
4
|
Summary: Python Helper for BoneIO
|
|
5
5
|
Home-page: https://github.com/maciejk1984/boneIO
|
|
6
6
|
License: GNU General Public License v3.0
|
|
7
|
-
Download-URL: https://github.com/maciejk1984/boneIO/archive/0.1.0.
|
|
7
|
+
Download-URL: https://github.com/maciejk1984/boneIO/archive/0.1.0.dev15.zip
|
|
8
8
|
Platform: UNKNOWN
|
|
9
9
|
Classifier: Programming Language :: Python
|
|
10
10
|
Classifier: Programming Language :: Python :: 3.7
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
from Adafruit_BBIO import GPIO
|
|
3
|
-
from Adafruit_BBIO.GPIO import HIGH, LOW
|
|
4
|
-
|
|
5
|
-
from boneio.helper.exceptions import GPIOInputException
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def setup_output(pin: str):
|
|
9
|
-
"""Set up a GPIO as output."""
|
|
10
|
-
|
|
11
|
-
GPIO.setup(pin, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
def setup_input(pin: str, pull_mode: str = "UP"):
|
|
15
|
-
"""Set up a GPIO as input."""
|
|
16
|
-
try:
|
|
17
|
-
GPIO.setup(pin, GPIO.IN, GPIO.PUD_DOWN if pull_mode == "DOWN" else GPIO.PUD_UP)
|
|
18
|
-
except (ValueError, SystemError) as err:
|
|
19
|
-
raise GPIOInputException(err)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def write_output(pin: str, value: str):
|
|
23
|
-
"""Write a value to a GPIO."""
|
|
24
|
-
|
|
25
|
-
GPIO.output(pin, value)
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
def read_input(pin: str, on_state=LOW):
|
|
29
|
-
"""Read a value from a GPIO."""
|
|
30
|
-
return GPIO.input(pin) is on_state
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
def edge_detect(pin, callback, bounce):
|
|
34
|
-
"""Add detection for RISING and FALLING events."""
|
|
35
|
-
try:
|
|
36
|
-
GPIO.add_event_detect(pin, GPIO.FALLING, callback=callback, bouncetime=bounce)
|
|
37
|
-
except RuntimeError as err:
|
|
38
|
-
raise GPIOInputException(err)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|