Skip to content

Commit 69c5892

Browse files
author
Nicolas
committed
Add documentation
Allow to init the db with a dummy unit
1 parent 3846c08 commit 69c5892

File tree

3 files changed

+82
-18
lines changed

3 files changed

+82
-18
lines changed

pybecker/becker.py

+70-17
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,60 @@
99
from .becker_helper import generate_code
1010

1111
COMMAND_UP = 0x20
12-
COMMAND_UP2 = 0x24 # intermediate position "up"
12+
COMMAND_UP2 = 0x21 # move up
13+
COMMAND_UP3 = 0x22 # move up
14+
COMMAND_UP4 = 0x23 # move up
15+
COMMAND_UP5 = 0x24 # intermediate position "up"
1316
COMMAND_DOWN = 0x40
14-
COMMAND_DOWN2 = 0x44 # intermediate position "down"
17+
COMMAND_DOWN2 = 0x41 # move down
18+
COMMAND_DOWN3 = 0x42 # move down
19+
COMMAND_DOWN4 = 0x43 # move down
20+
COMMAND_DOWN5 = 0x44 # intermediate position "down" (sun protection)
1521
COMMAND_HALT = 0x10
1622
COMMAND_PAIR = 0x80 # pair button press
1723
COMMAND_PAIR2 = 0x81 # pair button pressed for 3 seconds (without releasing)
1824
COMMAND_PAIR3 = 0x82 # pair button pressed for 6 seconds (without releasing)
1925
COMMAND_PAIR4 = 0x83 # pair button pressed for 10 seconds (without releasing)
2026

27+
COMMAND_CLEARPOS = 0x90
28+
COMMAND_CLEARPOS2 = 0x91
29+
COMMAND_CLEARPOS3 = 0x92
30+
COMMAND_CLEARPOS4 = 0x93
31+
2132
DEFAULT_DEVICE_NAME = '/dev/serial/by-id/usb-BECKER-ANTRIEBE_GmbH_CDC_RS232_v125_Centronic-if00'
2233

2334
logging.basicConfig()
2435
_LOGGER = logging.getLogger(__name__)
2536

2637

2738
class Becker:
39+
"""
40+
Becker Shutter Controller
41+
=========================
42+
43+
Use this class to perform operations on your Becker Shutter using a centronic USB Stick
44+
This class will as well maintain a call increment in an internal database
45+
"""
46+
def __init__(self, device_name=DEFAULT_DEVICE_NAME, init_dummy=False):
47+
"""
48+
Create a new instance of the Becker controller
2849
29-
def __init__(self, device_name=DEFAULT_DEVICE_NAME):
50+
:param device_name: The path for the centronic stick (default /dev/serial/by-id/usb-BECKER-ANTRIEBE_GmbH_CDC_RS232_v125_Centronic-if00).
51+
:param init_dummy: Boolean that indicate if the database should be initialized with a dummy unit (default False).
52+
:type device_name: str
53+
:type init_dummy: bool
54+
"""
3055
self.is_serial = "/" in device_name
3156
if self.is_serial and not os.path.exists(device_name):
3257
raise FileExistsError(device_name + " don't exists")
3358
self.device = device_name
3459
self.db = Database()
60+
61+
# If no unit is defined create a dummy one
62+
units = self.db.get_all_units()
63+
if not units and init_dummy:
64+
self.db.init_dummy()
65+
3566
if self.is_serial:
3667
self.s = serial.Serial(self.device, 115200, timeout=1)
3768
self.write_function = self.s.write
@@ -59,13 +90,13 @@ async def run_codes(self, channel, unit, cmd, test):
5990
if cmd == "UP":
6091
codes.append(generate_code(channel, unit, COMMAND_UP))
6192
elif cmd == "UP2":
62-
codes.append(generate_code(channel, unit, COMMAND_UP2))
93+
codes.append(generate_code(channel, unit, COMMAND_UP5))
6394
elif cmd == "HALT":
6495
codes.append(generate_code(channel, unit, COMMAND_HALT))
6596
elif cmd == "DOWN":
6697
codes.append(generate_code(channel, unit, COMMAND_DOWN))
6798
elif cmd == "DOWN2":
68-
codes.append(generate_code(channel, unit, COMMAND_DOWN2))
99+
codes.append(generate_code(channel, unit, COMMAND_DOWN5))
69100
elif cmd == "TRAIN":
70101
codes.append(generate_code(channel, unit, COMMAND_PAIR))
71102
unit[1] += 1
@@ -76,6 +107,16 @@ async def run_codes(self, channel, unit, cmd, test):
76107
codes.append(generate_code(channel, unit, COMMAND_PAIR2))
77108
# set unit as configured
78109
unit[2] = 1
110+
elif cmd == "CLEARPOS":
111+
codes.append(generate_code(channel, unit, COMMAND_PAIR))
112+
unit[1] += 1
113+
codes.append(generate_code(channel, unit, COMMAND_CLEARPOS))
114+
unit[1] += 1
115+
codes.append(generate_code(channel, unit, COMMAND_CLEARPOS2))
116+
unit[1] += 1
117+
codes.append(generate_code(channel, unit, COMMAND_CLEARPOS3))
118+
unit[1] += 1
119+
codes.append(generate_code(channel, unit, COMMAND_CLEARPOS4))
79120
elif cmd == "REMOVE":
80121
codes.append(generate_code(channel, unit, COMMAND_PAIR))
81122
unit[1] += 1
@@ -125,43 +166,55 @@ async def send(self, channel, cmd, test=False):
125166
await self.run_codes(ch, unit, cmd, test)
126167

127168
async def move_up(self, channel):
128-
""" Sent the command to move up for a given channel.
169+
"""
170+
Send the command to move up for a given channel.
129171
130-
:param channel: the channel on which the shutter is listening
172+
:param channel: the channel on which the shutter is listening
173+
:type channel: str
131174
"""
132175
await self.send(channel, "UP")
133176

134177
async def move_up_intermediate(self, channel):
135-
""" Sent the command to move up in the intermediate position for a given channel.
178+
"""
179+
Send the command to move up in the intermediate position for a given channel.
136180
137-
:param channel: the channel on which the shutter is listening
181+
:param channel: the channel on which the shutter is listening
182+
:type channel: str
138183
"""
139184
await self.send(channel, "UP2")
140185

141186
async def move_down(self, channel):
142-
""" Sent the command to move down for a given channel.
187+
"""
188+
Sent the command to move down for a given channel.
143189
144-
:param channel: the channel on which the shutter is listening
190+
:param channel: the channel on which the shutter is listening
191+
:type channel: str
145192
"""
146193
await self.send(channel, "DOWN")
147194

148195
async def move_down_intermediate(self, channel):
149-
""" Sent the command to move down in the intermediate position for a given channel.
196+
"""
197+
Send the command to move down in the intermediate position for a given channel.
150198
151-
:param channel: the channel on which the shutter is listening
199+
:param channel: the channel on which the shutter is listening
200+
:type channel: str
152201
"""
153202
await self.send(channel, "DOWN2")
154203

155204
async def stop(self, channel):
156-
""" Sent the command to stop for a given channel.
205+
"""
206+
Send the command to stop for a given channel.
157207
158-
:param channel: the channel on which the shutter is listening
208+
:param channel: the channel on which the shutter is listening
209+
:type channel: str
159210
"""
160211
await self.send(channel, "HALT")
161212

162213
async def pair(self, channel):
163-
""" Initiate the pairing for a given channel.
214+
"""
215+
Initiate the pairing for a given channel.
164216
165-
:param channel: the channel on which the shutter is listening
217+
:param channel: the channel on which the shutter is listening
218+
:type channel: str
166219
"""
167220
await self.send(channel, "TRAIN")

pybecker/database.py

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import time
44
import sqlite3
5+
from random import randrange
56

67
NUMBER_FILE = "centronic-stick.num"
78

@@ -45,6 +46,16 @@ def migrate(self):
4546
_LOGGER.error('Migration failed')
4647
self.conn.rollback()
4748

49+
def init_dummy(self):
50+
try:
51+
c = self.conn.cursor()
52+
inc = randrange(10, 40, 1)
53+
c.execute("UPDATE unit SET increment = ?, configured = ? WHERE code = ?", (inc, 1, '1737b',))
54+
self.conn.commit()
55+
except (sqlite3.Error, OSError):
56+
_LOGGER.error('Dummy Unit initialization failed')
57+
self.conn.rollback()
58+
4859
def create(self):
4960
# create the database table
5061

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="pybecker",
8-
version="1.0.0",
8+
version="1.0.1",
99
author="Nicolas Berthel",
1010
author_email="[email protected]",
1111
install_requires=['pyserial>=3.4'],

0 commit comments

Comments
 (0)