boneIO 0.1.0.dev6__py3-none-any.whl → 0.1.0.dev7__py3-none-any.whl

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/bonecli.py CHANGED
@@ -1,6 +1,7 @@
1
1
  from .helper import CustomValidator, load_yaml_file
2
2
  import click
3
3
  import logging
4
+ from contextlib import AsyncExitStack
4
5
  from colorlog import ColoredFormatter
5
6
  from .mqtt_client import MQTTClient
6
7
  from .manager import Manager
@@ -136,7 +137,10 @@ async def run(ctx, debug: int, config: str, mqttpassword: str = ""):
136
137
  mcp23017=_config[MCP23017],
137
138
  oled=_config.get(OLED),
138
139
  )
139
- await client.start_client(manager)
140
+ tasks = set()
141
+ tasks.add(client.start_client(manager))
142
+ tasks.update(manager.get_tasks())
143
+ await asyncio.gather(*tasks)
140
144
 
141
145
 
142
146
  if __name__ == "__main__":
boneio/helper/stats.py CHANGED
@@ -1,7 +1,19 @@
1
1
  import asyncio
2
2
  import psutil
3
3
  from math import floor
4
- from ..const import GIGABYTE, MEGABYTE, NETWORK, DISK, MEMORY, CPU, SWAP, UPTIME
4
+ from ..const import (
5
+ GIGABYTE,
6
+ MEGABYTE,
7
+ NETWORK,
8
+ DISK,
9
+ MEMORY,
10
+ CPU,
11
+ SWAP,
12
+ UPTIME,
13
+ HOST,
14
+ OUTPUT,
15
+ OledDataTypes,
16
+ )
5
17
  import time
6
18
 
7
19
  intervals = (
@@ -113,3 +125,38 @@ host_stats = {
113
125
  SWAP: get_swap_info,
114
126
  UPTIME: get_uptime,
115
127
  }
128
+
129
+ import socket
130
+
131
+
132
+ class HostData:
133
+ """Helper class to store host data."""
134
+
135
+ data = {UPTIME: {}, NETWORK: {}, CPU: {}, DISK: {}, MEMORY: {}, SWAP: {}}
136
+
137
+ def __init__(self, output: dict) -> None:
138
+ """Initialize HostData."""
139
+ self._hostname = socket.gethostname()
140
+ self.data[UPTIME] = {HOST: self._hostname, UPTIME: 0}
141
+ self._output = output
142
+
143
+ def write(self, type: str, data: dict) -> None:
144
+ """Write data of chosen type."""
145
+ self.data[type] = data
146
+
147
+ def write_uptime(self, uptime: str) -> None:
148
+ """Write uptime."""
149
+ self.data[UPTIME][UPTIME] = uptime
150
+
151
+ def get(self, type: OledDataTypes) -> dict:
152
+ """Get saved stats."""
153
+ if type == OUTPUT:
154
+ return self._get_output()
155
+ return self.data[type]
156
+
157
+ def _get_output(self) -> dict:
158
+ """Get stats for output."""
159
+ out = {}
160
+ for output in self._output.values():
161
+ out[output.id] = output.state
162
+ return out
boneio/manager.py CHANGED
@@ -1,8 +1,14 @@
1
1
  from .input import GpioInputButton
2
2
  from .relay import GpioRelay, MCPRelay
3
3
  from .oled import Oled
4
+ from .helper.stats import host_stats, HostData
5
+ from typing import Set
6
+ import asyncio
7
+ import logging
8
+
4
9
  from .const import (
5
10
  ACTION,
11
+ OUTPUT,
6
12
  ACTIONS,
7
13
  ADDRESS,
8
14
  GPIO,
@@ -64,9 +70,11 @@ class Manager:
64
70
  ha_discovery_prefix: str = "homeassistant",
65
71
  mcp23017: Optional[List] = None,
66
72
  oled: bool = False,
73
+ web: bool = False,
67
74
  ) -> None:
68
75
  """Initialize the manager."""
69
76
  loop = asyncio.get_event_loop()
77
+ self._host_data = None
70
78
 
71
79
  self.send_message = send_message
72
80
  self._topic_prefix = topic_prefix
@@ -75,6 +83,7 @@ class Manager:
75
83
  self._i2cbusio = I2C(SCL, SDA)
76
84
  self._mcp = {}
77
85
  self._oled = None
86
+ self._tasks: List[asyncio.Task] = []
78
87
 
79
88
  if mcp23017:
80
89
  for mcp in mcp23017:
@@ -127,15 +136,19 @@ class Manager:
127
136
  for gpio in self._input_pins
128
137
  ]
129
138
 
139
+ if oled or web:
140
+ self._host_data = HostData(output=self.output)
141
+ for f in host_stats.values():
142
+ self._tasks.append(asyncio.create_task(f(self._host_data)))
143
+
130
144
  if oled:
131
- self._oled = Oled(self.output)
145
+ self._oled = Oled(self._host_data)
146
+ self._tasks.append(self._oled.get_task())
132
147
 
133
148
  self.send_message(topic=f"{topic_prefix}/{STATE}", payload=ONLINE)
134
149
 
135
- def get_oled_tasks(self) -> any:
136
- if not self._oled:
137
- return []
138
- return self._oled.get_tasks()
150
+ def get_tasks(self) -> Set[asyncio.Task]:
151
+ return self._tasks
139
152
 
140
153
  def press_callback(self, x: ClickTypes, inpin: str, actions: dict) -> None:
141
154
  """Press callback to use in input gpio.
boneio/mqtt_client.py CHANGED
@@ -144,7 +144,7 @@ class MQTTClient:
144
144
  self.create_client() # reset connect/reconnect futures
145
145
 
146
146
  async def _subscribe_manager(self, manager: any) -> None:
147
- """Connect and subscribe to manager topics."""
147
+ """Connect and subscribe to manager topics + host stats."""
148
148
  async with AsyncExitStack() as stack:
149
149
  tasks: Set[asyncio.Task] = set()
150
150
 
@@ -166,8 +166,6 @@ class MQTTClient:
166
166
  )
167
167
  tasks.add(messages_task)
168
168
 
169
- tasks.update(manager.get_oled_tasks())
170
-
171
169
  topic = f"{manager.relay_topic}"
172
170
  await self.subscribe(topic)
173
171
 
boneio/oled.py CHANGED
@@ -1,5 +1,5 @@
1
1
  from .helper.gpio import setup_input, edge_detect
2
- from .helper.stats import host_stats
2
+ from .helper.stats import HostData
3
3
  from .input.gpio import configure_pin
4
4
  from .const import (
5
5
  OLED_PIN,
@@ -10,17 +10,13 @@ from .const import (
10
10
  SWAP,
11
11
  NUM_OF_SCREENS,
12
12
  UPTIME,
13
- HOST,
14
13
  OUTPUT,
15
14
  WHITE,
16
- OledDataTypes,
17
15
  )
18
16
  import asyncio
19
17
  import os
20
- import socket
21
18
  import logging
22
19
 
23
- from functools import partial
24
20
  from luma.core.interface.serial import i2c
25
21
  from luma.core.render import canvas
26
22
  from luma.oled.device import sh1106
@@ -41,47 +37,14 @@ screen_order = [UPTIME, NETWORK, CPU, DISK, MEMORY, SWAP, OUTPUT]
41
37
  lines = [17, 32, 47]
42
38
 
43
39
 
44
- class HostData:
45
- """Helper class to store host data."""
46
-
47
- data = {UPTIME: {}, NETWORK: {}, CPU: {}, DISK: {}, MEMORY: {}, SWAP: {}}
48
-
49
- def __init__(self, output: dict) -> None:
50
- """Initialize HostData."""
51
- self._hostname = socket.gethostname()
52
- self.data[UPTIME] = {HOST: self._hostname, UPTIME: 0}
53
- self._output = output
54
-
55
- def write(self, type: str, data: dict) -> None:
56
- """Write data of chosen type."""
57
- self.data[type] = data
58
-
59
- def write_uptime(self, uptime: str) -> None:
60
- """Write uptime."""
61
- self.data[UPTIME][UPTIME] = uptime
62
-
63
- def get(self, type: OledDataTypes) -> dict:
64
- """Get saved stats."""
65
- if type == OUTPUT:
66
- return self._get_output()
67
- return self.data[type]
68
-
69
- def _get_output(self) -> dict:
70
- """Get stats for output."""
71
- out = {}
72
- for output in self._output.values():
73
- out[output.id] = output.state
74
- return out
75
-
76
-
77
40
  class Oled:
78
41
  """Oled display class."""
79
42
 
80
- def __init__(self, output: dict) -> None:
43
+ def __init__(self, host_data: HostData) -> None:
81
44
  """Initialize OLED screen."""
82
45
  self._screen_no = 0
83
46
  self._loop = asyncio.get_running_loop()
84
- self._host_data = HostData(output=output)
47
+ self._host_data = host_data
85
48
  configure_pin(OLED_PIN)
86
49
  setup_input(OLED_PIN)
87
50
  edge_detect(OLED_PIN, callback=self._handle_press, bounce=50)
@@ -89,31 +52,30 @@ class Oled:
89
52
  self._device = sh1106(serial)
90
53
  _LOGGER.debug("Configuring OLED screen.")
91
54
 
92
- def get_tasks(self) -> list:
55
+ def get_task(self) -> list:
93
56
  """Periodically check for host statistics."""
94
- return [
95
- asyncio.create_task(func(self._host_data)) for func in host_stats.values()
96
- ]
97
-
98
- def _handle_press(self, pin: any) -> None:
99
- """Handle press of PIN for OLED display."""
57
+ return asyncio.create_task(self.render_display())
100
58
 
101
- def press_callback(screen_no):
102
- screen = screen_order[screen_no]
59
+ async def render_display(self):
60
+ while True:
61
+ screen = screen_order[self._screen_no]
103
62
  data = self._host_data.get(screen)
104
- with canvas(self._device) as draw:
105
- draw.text((1, 1), screen, font=fontBig, fill=WHITE)
106
- i = 0
107
- for k in data:
108
- draw.text(
109
- (3, lines[i]),
110
- f"{k} {data[k]}",
111
- font=fontSmall,
112
- fill=WHITE,
113
- )
114
- i += 1
63
+ if data:
64
+ with canvas(self._device) as draw:
65
+ draw.text((1, 1), screen, font=fontBig, fill=WHITE)
66
+ i = 0
67
+ for k in data:
68
+ draw.text(
69
+ (3, lines[i]),
70
+ f"{k} {data[k]}",
71
+ font=fontSmall,
72
+ fill=WHITE,
73
+ )
74
+ i += 1
75
+ await asyncio.sleep(0.5)
115
76
 
116
- self._loop.call_soon_threadsafe(partial(press_callback, self._screen_no))
77
+ def _handle_press(self, pin: any) -> None:
78
+ """Handle press of PIN for OLED display."""
117
79
  if self._screen_no < NUM_OF_SCREENS:
118
80
  self._screen_no += 1
119
81
  else:
boneio/version.py CHANGED
@@ -1,2 +1,2 @@
1
1
  # flake8: noqa
2
- __version__ = "0.1.0.dev6"
2
+ __version__ = "0.1.0.dev7"
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: boneio
3
- Version: 0.1.0.dev6
3
+ Version: 0.1.0.dev7
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.dev6.zip
7
+ Download-URL: https://github.com/maciejk1984/boneIO/archive/0.1.0.dev7.zip
8
8
  Platform: UNKNOWN
9
9
  Classifier: Programming Language :: Python
10
10
  Classifier: Programming Language :: Python :: 3.7
@@ -1,16 +1,16 @@
1
1
  boneio/__init__.py,sha256=hoLKnu6h1uEsiCHA9Ql2MMDC7VQS0E-s_2cxBS-CTOg,147
2
- boneio/bonecli.py,sha256=kCxGZzsZNUkJuTbhRurtC0JWs5rUla9m6tFmi9shWTI,3563
2
+ boneio/bonecli.py,sha256=SE62MXAle38I_mCpGT8j-0_EfOzYqOshQTSDZ5-NpfU,3695
3
3
  boneio/config.example.yaml,sha256=WAoyi8jg5zLvlDaoDBPEu_XkHq_zB4mDj9Dxbg0-V4A,465
4
4
  boneio/config.yaml,sha256=4YEdjJIh0u_689YnLPAxNlH--a9TqWsaGd8o4-gMABU,254
5
5
  boneio/const.py,sha256=fZXr-SyuSUTXxps0E_DX7ZKpH7ksSn6P-3SoYFJExMU,1165
6
- boneio/manager.py,sha256=hWEmJ4okoKog5alrLaeo9ANLPPH6zdpGqTAJSSUREKI,5494
7
- boneio/mqtt_client.py,sha256=KXkXZiPZjq-s2shOs2wObIpC1TFojS_zC7mJ7dBI_7o,6348
8
- boneio/oled.py,sha256=0ZeQfeXw1EH_ZhMedy7IXgdbgs6cklLAS942vKhVVzQ,3446
6
+ boneio/manager.py,sha256=Erm5-38g1P1lR3mvJS9WF5g_3pdbcsby9Ah_xpAbb8k,5919
7
+ boneio/mqtt_client.py,sha256=o3lTuM9eOfqIs1yaxFmx1NFTU3nvwqWFR7A7VjpZ0fo,6309
8
+ boneio/oled.py,sha256=g9MVsH1uRGJALRz38VtT00q75Qws5kwUdeSmiiaC6es,2372
9
9
  boneio/schema.yaml,sha256=A3oEFx8R9T34OGS6gfFByk_UEffOGduOeFEF1VLni_0,3617
10
- boneio/version.py,sha256=daBxh9WgiCxCnwvyU2UupN9Pyjra4Le4X2fmTLvfmE0,42
10
+ boneio/version.py,sha256=luFPZpokGu-BWg1TYdSkx2eQYB7fDJC2R90J49cJ_cU,42
11
11
  boneio/helper/__init__.py,sha256=AXk0Qciu1nqt4rWdMDinKLJX701SFBYzWOnXrZHh-UQ,128
12
12
  boneio/helper/gpio.py,sha256=7TEknDFdLqjiPhs3g7zDS1R5kjFV2Ns8TNFFJzbx6hY,770
13
- boneio/helper/stats.py,sha256=7yCnN9bklibK2jnN8hD5c-mhRIFqPQZjIqIC-I7PMiE,2894
13
+ boneio/helper/stats.py,sha256=2X17r8-d3pHNwhlRihnfJtpLKvLv-80nISSFnz5sf0g,3964
14
14
  boneio/helper/yaml.py,sha256=_KXXbnDV5btqVw7e9Vd7NVq7FOvPhPDWYssevBc_UVg,1224
15
15
  boneio/input/__init__.py,sha256=h8mkcengWWmT8wYb8ZbVtbrKZFQvHWOKB6Q8eOeDxAo,87
16
16
  boneio/input/gpio.py,sha256=rRlIdfUePk2Q5N6mNO-mA0thCdFuodsZ27qEiGPNROo,4853
@@ -18,8 +18,8 @@ boneio/relay/__init__.py,sha256=iVRXyXh9qvs1ooQ0fzFJC6muiNHwWqkJTu0ajFm9nTo,111
18
18
  boneio/relay/basic.py,sha256=mDeyYnaiVZhny3aimXG5a2vCKu0WaMSI-okjR7pM2d4,1725
19
19
  boneio/relay/gpio.py,sha256=ys_7S0S9lSmGpHCwNeS7ac99jgNUCNbfRgcTFKfXrFw,1128
20
20
  boneio/relay/mcp.py,sha256=RQtMPM0JKpEdMI8oXR5DCavDFrSUNIA_duNXWMJRO2A,1187
21
- boneio-0.1.0.dev6.dist-info/METADATA,sha256=luhOXOs6E5SVPgKcJ1t1y-PMMYwjrf0AJGxEduB2-pY,1280
22
- boneio-0.1.0.dev6.dist-info/WHEEL,sha256=ewwEueio1C2XeHTvT17n8dZUJgOvyCWCt0WVNLClP9o,92
23
- boneio-0.1.0.dev6.dist-info/entry_points.txt,sha256=5SCe5w4eZDOqZwKjMf7UjHzjCI0u2Dcbs0ebrPex5_k,47
24
- boneio-0.1.0.dev6.dist-info/top_level.txt,sha256=ag9S5n1_KXZfyk9tN8R0YUDYbdA--e5a2w2FOD_yMdg,7
25
- boneio-0.1.0.dev6.dist-info/RECORD,,
21
+ boneio-0.1.0.dev7.dist-info/METADATA,sha256=GrUrbffYdSBnwkp3U94bWE7DK5PxO4YULHUJfs-PS9A,1280
22
+ boneio-0.1.0.dev7.dist-info/WHEEL,sha256=ewwEueio1C2XeHTvT17n8dZUJgOvyCWCt0WVNLClP9o,92
23
+ boneio-0.1.0.dev7.dist-info/entry_points.txt,sha256=5SCe5w4eZDOqZwKjMf7UjHzjCI0u2Dcbs0ebrPex5_k,47
24
+ boneio-0.1.0.dev7.dist-info/top_level.txt,sha256=ag9S5n1_KXZfyk9tN8R0YUDYbdA--e5a2w2FOD_yMdg,7
25
+ boneio-0.1.0.dev7.dist-info/RECORD,,