infrared-protocols 1.0.0__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.
- infrared_protocols-1.0.0/LICENSE +21 -0
- infrared_protocols-1.0.0/PKG-INFO +19 -0
- infrared_protocols-1.0.0/README.md +3 -0
- infrared_protocols-1.0.0/infrared_protocols/__init__.py +9 -0
- infrared_protocols-1.0.0/infrared_protocols/commands.py +121 -0
- infrared_protocols-1.0.0/infrared_protocols.egg-info/PKG-INFO +19 -0
- infrared_protocols-1.0.0/infrared_protocols.egg-info/SOURCES.txt +11 -0
- infrared_protocols-1.0.0/infrared_protocols.egg-info/dependency_links.txt +1 -0
- infrared_protocols-1.0.0/infrared_protocols.egg-info/requires.txt +4 -0
- infrared_protocols-1.0.0/infrared_protocols.egg-info/top_level.txt +1 -0
- infrared_protocols-1.0.0/pyproject.toml +47 -0
- infrared_protocols-1.0.0/setup.cfg +4 -0
- infrared_protocols-1.0.0/tests/test_commands.py +124 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Home Assistant Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: infrared-protocols
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Library to decode and encode infrared signals.
|
|
5
|
+
License: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/home-assistant-libs/infrared-protocols
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Requires-Python: >=3.13.0
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: pytest; extra == "dev"
|
|
14
|
+
Requires-Dist: prek; extra == "dev"
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# Python Infrared Protocols for Home Assistant
|
|
18
|
+
|
|
19
|
+
Python package to decode and encode infrared signals for use in Home Assistant.
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"""Common IR command definitions."""
|
|
2
|
+
|
|
3
|
+
import abc
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import override
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True, slots=True)
|
|
9
|
+
class Timing:
|
|
10
|
+
"""High/low signal timing."""
|
|
11
|
+
|
|
12
|
+
high_us: int
|
|
13
|
+
low_us: int
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Command(abc.ABC):
|
|
17
|
+
"""Base class for IR commands."""
|
|
18
|
+
|
|
19
|
+
repeat_count: int
|
|
20
|
+
modulation: int
|
|
21
|
+
|
|
22
|
+
def __init__(self, *, modulation: int, repeat_count: int = 0) -> None:
|
|
23
|
+
"""Initialize the IR command."""
|
|
24
|
+
self.modulation = modulation
|
|
25
|
+
self.repeat_count = repeat_count
|
|
26
|
+
|
|
27
|
+
@abc.abstractmethod
|
|
28
|
+
def get_raw_timings(self) -> list[Timing]:
|
|
29
|
+
"""Get raw timings for the command."""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class NECCommand(Command):
|
|
33
|
+
"""NEC IR command."""
|
|
34
|
+
|
|
35
|
+
address: int
|
|
36
|
+
command: int
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self, *, address: int, command: int, modulation: int, repeat_count: int = 0
|
|
40
|
+
) -> None:
|
|
41
|
+
"""Initialize the NEC IR command."""
|
|
42
|
+
super().__init__(modulation=modulation, repeat_count=repeat_count)
|
|
43
|
+
self.address = address
|
|
44
|
+
self.command = command
|
|
45
|
+
|
|
46
|
+
@override
|
|
47
|
+
def get_raw_timings(self) -> list[Timing]:
|
|
48
|
+
"""Get raw timings for the NEC command.
|
|
49
|
+
|
|
50
|
+
NEC protocol timing (in microseconds):
|
|
51
|
+
- Leader pulse: 9000µs high, 4500µs low
|
|
52
|
+
- Logical '0': 562µs high, 562µs low
|
|
53
|
+
- Logical '1': 562µs high, 1687µs low
|
|
54
|
+
- End pulse: 562µs high
|
|
55
|
+
- Repeat code: 9000µs high, 2250µs low, 562µs end pulse
|
|
56
|
+
- Frame gap: ~96ms between end pulse and next frame (total frame ~108ms)
|
|
57
|
+
|
|
58
|
+
Data format (32 bits, LSB first):
|
|
59
|
+
- Standard NEC: address (8-bit) + ~address (8-bit) + command (8-bit)
|
|
60
|
+
+ ~command (8-bit)
|
|
61
|
+
- Extended NEC: address_low (8-bit) + address_high (8-bit) + command (8-bit)
|
|
62
|
+
+ ~command (8-bit)
|
|
63
|
+
"""
|
|
64
|
+
# NEC timing constants (microseconds)
|
|
65
|
+
leader_high = 9000
|
|
66
|
+
leader_low = 4500
|
|
67
|
+
bit_high = 562
|
|
68
|
+
zero_low = 562
|
|
69
|
+
one_low = 1687
|
|
70
|
+
repeat_low = 2250
|
|
71
|
+
frame_gap = 96000 # Gap to make total frame ~108ms
|
|
72
|
+
|
|
73
|
+
timings: list[Timing] = [Timing(high_us=leader_high, low_us=leader_low)]
|
|
74
|
+
|
|
75
|
+
# Determine if standard (8-bit) or extended (16-bit) address
|
|
76
|
+
if self.address <= 0xFF:
|
|
77
|
+
# Standard NEC: address + inverted address
|
|
78
|
+
address_low = self.address & 0xFF
|
|
79
|
+
address_high = (~self.address) & 0xFF
|
|
80
|
+
else:
|
|
81
|
+
# Extended NEC: 16-bit address (no inversion)
|
|
82
|
+
address_low = self.address & 0xFF
|
|
83
|
+
address_high = (self.address >> 8) & 0xFF
|
|
84
|
+
|
|
85
|
+
command_byte = self.command & 0xFF
|
|
86
|
+
command_inverted = (~self.command) & 0xFF
|
|
87
|
+
|
|
88
|
+
# Build 32-bit command data (LSB first in transmission)
|
|
89
|
+
data = (
|
|
90
|
+
address_low
|
|
91
|
+
| (address_high << 8)
|
|
92
|
+
| (command_byte << 16)
|
|
93
|
+
| (command_inverted << 24)
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
for _ in range(32):
|
|
97
|
+
bit = data & 1
|
|
98
|
+
if bit:
|
|
99
|
+
timings.append(Timing(high_us=bit_high, low_us=one_low))
|
|
100
|
+
else:
|
|
101
|
+
timings.append(Timing(high_us=bit_high, low_us=zero_low))
|
|
102
|
+
data >>= 1
|
|
103
|
+
|
|
104
|
+
# End pulse
|
|
105
|
+
timings.append(Timing(high_us=bit_high, low_us=0))
|
|
106
|
+
|
|
107
|
+
# Add repeat codes if requested
|
|
108
|
+
for _ in range(self.repeat_count):
|
|
109
|
+
# Replace the last timing's low_us with the frame gap
|
|
110
|
+
last_timing = timings[-1]
|
|
111
|
+
timings[-1] = Timing(high_us=last_timing.high_us, low_us=frame_gap)
|
|
112
|
+
|
|
113
|
+
# Repeat code: leader burst + shorter space + end pulse
|
|
114
|
+
timings.extend(
|
|
115
|
+
[
|
|
116
|
+
Timing(high_us=leader_high, low_us=repeat_low),
|
|
117
|
+
Timing(high_us=bit_high, low_us=0),
|
|
118
|
+
]
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
return timings
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: infrared-protocols
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Library to decode and encode infrared signals.
|
|
5
|
+
License: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/home-assistant-libs/infrared-protocols
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Requires-Python: >=3.13.0
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: pytest; extra == "dev"
|
|
14
|
+
Requires-Dist: prek; extra == "dev"
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# Python Infrared Protocols for Home Assistant
|
|
18
|
+
|
|
19
|
+
Python package to decode and encode infrared signals for use in Home Assistant.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
infrared_protocols/__init__.py
|
|
5
|
+
infrared_protocols/commands.py
|
|
6
|
+
infrared_protocols.egg-info/PKG-INFO
|
|
7
|
+
infrared_protocols.egg-info/SOURCES.txt
|
|
8
|
+
infrared_protocols.egg-info/dependency_links.txt
|
|
9
|
+
infrared_protocols.egg-info/requires.txt
|
|
10
|
+
infrared_protocols.egg-info/top_level.txt
|
|
11
|
+
tests/test_commands.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
infrared_protocols
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=78.1.1,<83.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "infrared-protocols"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
license = {text = "MIT"}
|
|
9
|
+
description = "Library to decode and encode infrared signals."
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Programming Language :: Python :: 3",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
]
|
|
15
|
+
requires-python = ">=3.13.0"
|
|
16
|
+
|
|
17
|
+
[project.urls]
|
|
18
|
+
"Homepage" = "https://github.com/home-assistant-libs/infrared-protocols"
|
|
19
|
+
|
|
20
|
+
[tool.setuptools]
|
|
21
|
+
include-package-data = false
|
|
22
|
+
|
|
23
|
+
[tool.setuptools.packages.find]
|
|
24
|
+
include = ["infrared_protocols*"]
|
|
25
|
+
|
|
26
|
+
[tool.ruff.lint]
|
|
27
|
+
select = [
|
|
28
|
+
# pycodestyle
|
|
29
|
+
"E",
|
|
30
|
+
# Pyflakes
|
|
31
|
+
"F",
|
|
32
|
+
# pyupgrade
|
|
33
|
+
"UP",
|
|
34
|
+
# flake8-bugbear
|
|
35
|
+
"B",
|
|
36
|
+
# isort
|
|
37
|
+
"I",]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
[tool.ruff.lint.mccabe]
|
|
41
|
+
max-complexity = 25
|
|
42
|
+
|
|
43
|
+
[tool.pyright]
|
|
44
|
+
typeCheckingMode = "standard"
|
|
45
|
+
|
|
46
|
+
[project.optional-dependencies]
|
|
47
|
+
dev = ["pytest", "prek"]
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""Tests for the Infrared protocol definitions."""
|
|
2
|
+
|
|
3
|
+
from infrared_protocols import NECCommand, Timing
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_nec_command_get_raw_timings_standard() -> None:
|
|
7
|
+
"""Test NEC command raw timings generation for standard 8-bit address."""
|
|
8
|
+
expected_raw_timings = [
|
|
9
|
+
Timing(high_us=9000, low_us=4500),
|
|
10
|
+
Timing(high_us=562, low_us=562),
|
|
11
|
+
Timing(high_us=562, low_us=562),
|
|
12
|
+
Timing(high_us=562, low_us=1687),
|
|
13
|
+
Timing(high_us=562, low_us=562),
|
|
14
|
+
Timing(high_us=562, low_us=562),
|
|
15
|
+
Timing(high_us=562, low_us=562),
|
|
16
|
+
Timing(high_us=562, low_us=562),
|
|
17
|
+
Timing(high_us=562, low_us=562),
|
|
18
|
+
Timing(high_us=562, low_us=1687),
|
|
19
|
+
Timing(high_us=562, low_us=1687),
|
|
20
|
+
Timing(high_us=562, low_us=562),
|
|
21
|
+
Timing(high_us=562, low_us=1687),
|
|
22
|
+
Timing(high_us=562, low_us=1687),
|
|
23
|
+
Timing(high_us=562, low_us=1687),
|
|
24
|
+
Timing(high_us=562, low_us=1687),
|
|
25
|
+
Timing(high_us=562, low_us=1687),
|
|
26
|
+
Timing(high_us=562, low_us=562),
|
|
27
|
+
Timing(high_us=562, low_us=562),
|
|
28
|
+
Timing(high_us=562, low_us=562),
|
|
29
|
+
Timing(high_us=562, low_us=1687),
|
|
30
|
+
Timing(high_us=562, low_us=562),
|
|
31
|
+
Timing(high_us=562, low_us=562),
|
|
32
|
+
Timing(high_us=562, low_us=562),
|
|
33
|
+
Timing(high_us=562, low_us=562),
|
|
34
|
+
Timing(high_us=562, low_us=1687),
|
|
35
|
+
Timing(high_us=562, low_us=1687),
|
|
36
|
+
Timing(high_us=562, low_us=1687),
|
|
37
|
+
Timing(high_us=562, low_us=562),
|
|
38
|
+
Timing(high_us=562, low_us=1687),
|
|
39
|
+
Timing(high_us=562, low_us=1687),
|
|
40
|
+
Timing(high_us=562, low_us=1687),
|
|
41
|
+
Timing(high_us=562, low_us=1687),
|
|
42
|
+
Timing(high_us=562, low_us=0),
|
|
43
|
+
]
|
|
44
|
+
command = NECCommand(address=0x04, command=0x08, modulation=38000, repeat_count=0)
|
|
45
|
+
timings = command.get_raw_timings()
|
|
46
|
+
assert timings == expected_raw_timings
|
|
47
|
+
|
|
48
|
+
# Same command now with 2 repeats
|
|
49
|
+
command_with_repeats = NECCommand(
|
|
50
|
+
address=command.address,
|
|
51
|
+
command=command.command,
|
|
52
|
+
modulation=command.modulation,
|
|
53
|
+
repeat_count=2,
|
|
54
|
+
)
|
|
55
|
+
timings_with_repeats = command_with_repeats.get_raw_timings()
|
|
56
|
+
assert timings_with_repeats == [
|
|
57
|
+
*expected_raw_timings[:-1],
|
|
58
|
+
Timing(high_us=562, low_us=96000),
|
|
59
|
+
Timing(high_us=9000, low_us=2250),
|
|
60
|
+
Timing(high_us=562, low_us=96000),
|
|
61
|
+
Timing(high_us=9000, low_us=2250),
|
|
62
|
+
Timing(high_us=562, low_us=0),
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_nec_command_get_raw_timings_extended() -> None:
|
|
67
|
+
"""Test NEC command raw timings generation for extended 16-bit address."""
|
|
68
|
+
expected_raw_timings = [
|
|
69
|
+
Timing(high_us=9000, low_us=4500),
|
|
70
|
+
Timing(high_us=562, low_us=1687),
|
|
71
|
+
Timing(high_us=562, low_us=1687),
|
|
72
|
+
Timing(high_us=562, low_us=562),
|
|
73
|
+
Timing(high_us=562, low_us=1687),
|
|
74
|
+
Timing(high_us=562, low_us=1687),
|
|
75
|
+
Timing(high_us=562, low_us=1687),
|
|
76
|
+
Timing(high_us=562, low_us=1687),
|
|
77
|
+
Timing(high_us=562, low_us=1687),
|
|
78
|
+
Timing(high_us=562, low_us=562),
|
|
79
|
+
Timing(high_us=562, low_us=562),
|
|
80
|
+
Timing(high_us=562, low_us=1687),
|
|
81
|
+
Timing(high_us=562, low_us=562),
|
|
82
|
+
Timing(high_us=562, low_us=562),
|
|
83
|
+
Timing(high_us=562, low_us=562),
|
|
84
|
+
Timing(high_us=562, low_us=562),
|
|
85
|
+
Timing(high_us=562, low_us=562),
|
|
86
|
+
Timing(high_us=562, low_us=562),
|
|
87
|
+
Timing(high_us=562, low_us=562),
|
|
88
|
+
Timing(high_us=562, low_us=562),
|
|
89
|
+
Timing(high_us=562, low_us=1687),
|
|
90
|
+
Timing(high_us=562, low_us=562),
|
|
91
|
+
Timing(high_us=562, low_us=562),
|
|
92
|
+
Timing(high_us=562, low_us=562),
|
|
93
|
+
Timing(high_us=562, low_us=562),
|
|
94
|
+
Timing(high_us=562, low_us=1687),
|
|
95
|
+
Timing(high_us=562, low_us=1687),
|
|
96
|
+
Timing(high_us=562, low_us=1687),
|
|
97
|
+
Timing(high_us=562, low_us=562),
|
|
98
|
+
Timing(high_us=562, low_us=1687),
|
|
99
|
+
Timing(high_us=562, low_us=1687),
|
|
100
|
+
Timing(high_us=562, low_us=1687),
|
|
101
|
+
Timing(high_us=562, low_us=1687),
|
|
102
|
+
Timing(high_us=562, low_us=0),
|
|
103
|
+
]
|
|
104
|
+
|
|
105
|
+
command = NECCommand(address=0x04FB, command=0x08, modulation=38000, repeat_count=0)
|
|
106
|
+
timings = command.get_raw_timings()
|
|
107
|
+
assert timings == expected_raw_timings
|
|
108
|
+
|
|
109
|
+
# Same command now with 2 repeats
|
|
110
|
+
command_with_repeats = NECCommand(
|
|
111
|
+
address=command.address,
|
|
112
|
+
command=command.command,
|
|
113
|
+
modulation=command.modulation,
|
|
114
|
+
repeat_count=2,
|
|
115
|
+
)
|
|
116
|
+
timings_with_repeats = command_with_repeats.get_raw_timings()
|
|
117
|
+
assert timings_with_repeats == [
|
|
118
|
+
*expected_raw_timings[:-1],
|
|
119
|
+
Timing(high_us=562, low_us=96000),
|
|
120
|
+
Timing(high_us=9000, low_us=2250),
|
|
121
|
+
Timing(high_us=562, low_us=96000),
|
|
122
|
+
Timing(high_us=9000, low_us=2250),
|
|
123
|
+
Timing(high_us=562, low_us=0),
|
|
124
|
+
]
|