This repository was archived by the owner on Jul 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
255 lines (193 loc) · 8.14 KB
/
client.py
File metadata and controls
255 lines (193 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from pathlib import Path
from typing import List, Type, Union
from .connection.abstract_connection import AbstractConnection
from .connection.lircd_connection import LircdConnection
from .exceptions import LircdCommandFailureError
from .reply_packet_parser import ReplyPacketParser
class Client:
"""Communicate with the lircd daemon."""
def __init__(self, connection: Type[AbstractConnection] = None) -> None:
"""Initialize the client by connecting to the lircd socket.
Args:
connection: The connection to lircd. Created with defaults
depending on the operating system if one is not provided.
Raises:
TypeError: If connection is not an instance of AbstractConnection.
LircdConnectionError: If the socket cannot connect to the address.
"""
if not connection:
connection = LircdConnection()
if not isinstance(connection, AbstractConnection):
raise TypeError("`connection` must be an instance of `AbstractConnection`")
# Used for start_repeat and stop_repeat
self._last_send_start_remote = None
self._last_send_start_key = None
self._connection = connection
self._connection.connect()
def _send_command(self, command: str) -> Union[str, List[str]]:
"""Send a command to lircd.
Args:
command: A command from the lircd socket command interface.
Returns:
The data from the lirc response packet.
"""
self._connection.send(command)
parser = ReplyPacketParser()
while not parser.is_finished:
line = self._connection.readline()
parser.feed(line)
parser_data = parser.data[0] if len(parser.data) == 1 else parser.data
if not parser.success:
raise LircdCommandFailureError(
f"The `{command}` command sent to lircd failed: {parser_data}"
)
return parser_data
def close(self) -> None:
"""Close the connection to the socket."""
self._connection.close()
def send_once(self, remote: str, key: str, repeat_count: int = 0) -> None:
"""Send an lircd SEND_ONCE command.
Args:
key: The name of the key to send.
remote: The remote to use keys from.
repeat_count: The number of times to repeat this key.
If this is set to 1, that means this key will be
sent twice (repeated once).
.. versionchanged:: 2.0.0
The repeat_count parameter has been changed to
have a default value of 0 instead of 1. This ensures
send_once only sends 1 IR signal instead of sending 1
and then repeating it (therefore, 2 signals).
Raises:
LircdCommandFailure: If the command fails.
"""
self._send_command(f"SEND_ONCE {remote} {key} {repeat_count}")
def send_start(self, remote: str, key: str) -> None:
"""Send an lircd SEND_START command.
This will repeat the given key until
send_stop is called.
Args:
remote: The remote to use keys from.
key: The name of the key to start sending.
Raises:
LircdCommandFailure: If the command fails.
"""
self._last_send_start_remote = remote
self._last_send_start_key = key
self._send_command(f"SEND_START {remote} {key}")
def send_stop(self, remote: str = "", key: str = "") -> None:
"""Send an lircd SEND_STOP command.
The remote and key default to the remote and key
last used with ``send_start`` if they are not specified,
since the most likely use case is sending a ``send_start``
and then a ``send_stop``.
Args:
remote: The remote to stop.
key: The key to stop sending.
Raises:
LircdCommandFailure: If the command fails.
"""
if not remote and self._last_send_start_remote:
remote = self._last_send_start_remote
if not key and self._last_send_start_key:
key = self._last_send_start_key
self._send_command(f"SEND_STOP {remote} {key}")
def list_remotes(self) -> List[str]:
"""List all the remotes that lirc has in
its ``/etc/lirc/lircd.conf.d`` folder.
Raises:
LircdCommandFailure: If the command fails.
Returns:
The list of all remotes.
"""
return self._send_command("LIST")
def list_remote_keys(self, remote: str) -> List[str]:
"""List all the keys for a specific remote.
Args:
remote: The remote to list the keys of.
Raises:
LircdCommandFailure: If the command fails.
Returns:
The list of keys from the remote.
"""
return self._send_command(f"LIST {remote}")
def start_logging(self, path: Union[str, Path]) -> None:
"""Send a lircd SET_INPUTLOG command which sets
the path to log all lircd received data to.
Args:
path: The path to start logging lircd recieved data to.
Raises:
LircdCommandFailure: If the command fails.
"""
self._send_command(f"SET_INPUTLOG {path}")
def stop_logging(self) -> None:
"""Stop logging to the inputlog path from start_logging.
Raises:
LircdCommandFailure: If the command fails.
"""
# When calling SET_INPUTLOG without the path argument,
# it will stop logging and close the logfile.
self._send_command("SET_INPUTLOG")
def version(self) -> str:
"""Retrieve the version of LIRC
Raises:
LircdCommandFailure: If the command fails.
Returns:
The version of LIRC being used.
"""
return self._send_command("VERSION")
def driver_option(self, key: str, value: str) -> None:
"""Set driver-specific option named key to given value.
Args:
key: The key to set for the driver.
value: The value for the key to set.
Raises:
LircdCommandFailure: If the command fails.
"""
self._send_command(f"DRV_OPTION {key} {value}")
def simulate(
self, remote: str, key: str, repeat_count: int = 0, keycode: int = 0
) -> None:
"""Simulate an IR event.
The ``--allow-simulate`` command line option to lircd must be active for this
command not to fail.
Lircd Format:
<code> <repeat count> <button name> <remote control name>
Example:
0000000000f40bf0 00 KEY_UP ANIMAX
Args:
remote: The remote to simulate key presses from.
key: The key on the remote to simulate.
repeat_count: The number of times to repeat the simulated key press.
keycode: lircd(8) describes this option as a 16 hexadecimal digit
number encoding of the IR signal. However, it says it is depreciated
and should be ignored.
.. versionchanged:: 3.0.0
The repeat_count parameter has been changed to
have a default value of 0 instead of 1. The previous
value was incorrect since it leads to the command
being sent twice (1 and then repeated once).
Raises:
LircdCommandFailure: If the command fails.
"""
self._send_command(
"SIMULATE %016d %02d %s %s\n" % (keycode, repeat_count, key, remote)
)
def set_transmitters(self, transmitters: Union[int, List[int]]) -> None:
"""Set the active transmitters.
Example:
>>> import lirc
>>> client = lirc.Client()
>>> client.set_transmitters(1)
>>> client.set_transmitters([1,3,5])
Args:
transmitters: The transmitters to set active.
Raises:
LircdCommandFailure: If the command fails.
"""
mask = transmitters
if isinstance(transmitters, List):
mask = 0
for transmitter in transmitters:
mask |= 1 << (int(transmitter) - 1)
self._send_command(f"SET_TRANSMITTERS {mask}")