aionecpbx 0.1.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.
- aionecpbx-0.1.0/.github/workflows/ci.yml +47 -0
- aionecpbx-0.1.0/.github/workflows/release.yml +118 -0
- aionecpbx-0.1.0/.gitignore +10 -0
- aionecpbx-0.1.0/CHANGELOG.md +11 -0
- aionecpbx-0.1.0/LICENSE +29 -0
- aionecpbx-0.1.0/NOTICE +6 -0
- aionecpbx-0.1.0/PKG-INFO +120 -0
- aionecpbx-0.1.0/README.md +87 -0
- aionecpbx-0.1.0/pyproject.toml +74 -0
- aionecpbx-0.1.0/src/aionecpbx/__init__.py +73 -0
- aionecpbx-0.1.0/src/aionecpbx/__main__.py +111 -0
- aionecpbx-0.1.0/src/aionecpbx/ami.py +315 -0
- aionecpbx-0.1.0/src/aionecpbx/client.py +171 -0
- aionecpbx-0.1.0/src/aionecpbx/py.typed +0 -0
- aionecpbx-0.1.0/src/aionecpbx/smdr.py +331 -0
- aionecpbx-0.1.0/tests/__init__.py +0 -0
- aionecpbx-0.1.0/tests/fakes.py +147 -0
- aionecpbx-0.1.0/tests/test_ami.py +140 -0
- aionecpbx-0.1.0/tests/test_cli.py +83 -0
- aionecpbx-0.1.0/tests/test_client.py +95 -0
- aionecpbx-0.1.0/tests/test_smdr.py +238 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
pull_request:
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
lint:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: actions/setup-python@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: "3.14"
|
|
19
|
+
- run: pip install -e ".[lint]"
|
|
20
|
+
- run: ruff check .
|
|
21
|
+
- run: ruff format --check .
|
|
22
|
+
- run: mypy
|
|
23
|
+
|
|
24
|
+
test:
|
|
25
|
+
runs-on: ubuntu-latest
|
|
26
|
+
strategy:
|
|
27
|
+
fail-fast: false
|
|
28
|
+
matrix:
|
|
29
|
+
python: ["3.12", "3.13", "3.14"]
|
|
30
|
+
steps:
|
|
31
|
+
- uses: actions/checkout@v4
|
|
32
|
+
- uses: actions/setup-python@v5
|
|
33
|
+
with:
|
|
34
|
+
python-version: ${{ matrix.python }}
|
|
35
|
+
- run: pip install -e ".[test]"
|
|
36
|
+
- run: pytest -q --cov=aionecpbx --cov-report=term-missing
|
|
37
|
+
|
|
38
|
+
build:
|
|
39
|
+
runs-on: ubuntu-latest
|
|
40
|
+
steps:
|
|
41
|
+
- uses: actions/checkout@v4
|
|
42
|
+
- uses: actions/setup-python@v5
|
|
43
|
+
with:
|
|
44
|
+
python-version: "3.14"
|
|
45
|
+
- run: pip install build twine
|
|
46
|
+
- run: python -m build
|
|
47
|
+
- run: twine check dist/*
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
|
|
3
|
+
# Releases happen by themselves: bump __version__ in src/aionecpbx/__init__.py,
|
|
4
|
+
# add its CHANGELOG.md section, and push to main. If no release exists for that
|
|
5
|
+
# version yet, this builds it, publishes it to PyPI and creates the GitHub
|
|
6
|
+
# release and tag. Pushing a "v<version>" tag, or publishing a release by hand,
|
|
7
|
+
# still works too.
|
|
8
|
+
# PyPI publishing uses trusted publishing: no token is stored in GitHub.
|
|
9
|
+
|
|
10
|
+
on:
|
|
11
|
+
push:
|
|
12
|
+
branches: [main]
|
|
13
|
+
tags: ["v*"]
|
|
14
|
+
workflow_dispatch:
|
|
15
|
+
|
|
16
|
+
concurrency:
|
|
17
|
+
group: release
|
|
18
|
+
cancel-in-progress: false
|
|
19
|
+
|
|
20
|
+
jobs:
|
|
21
|
+
plan:
|
|
22
|
+
runs-on: ubuntu-latest
|
|
23
|
+
permissions:
|
|
24
|
+
contents: read
|
|
25
|
+
outputs:
|
|
26
|
+
tag: ${{ steps.plan.outputs.tag }}
|
|
27
|
+
release: ${{ steps.plan.outputs.release }}
|
|
28
|
+
steps:
|
|
29
|
+
- uses: actions/checkout@v4
|
|
30
|
+
- id: plan
|
|
31
|
+
env:
|
|
32
|
+
GH_TOKEN: ${{ github.token }}
|
|
33
|
+
run: |
|
|
34
|
+
version=$(python3 -c "import re,pathlib;print(re.search(r'__version__ = \"(.+?)\"', pathlib.Path('src/aionecpbx/__init__.py').read_text())[1])")
|
|
35
|
+
tag="v$version"
|
|
36
|
+
echo "tag=$tag" >> "$GITHUB_OUTPUT"
|
|
37
|
+
if [ "$GITHUB_REF_TYPE" = "tag" ]; then
|
|
38
|
+
if [ "$GITHUB_REF_NAME" != "$tag" ]; then
|
|
39
|
+
echo "tag $GITHUB_REF_NAME != $tag"; exit 1
|
|
40
|
+
fi
|
|
41
|
+
echo "release=true" >> "$GITHUB_OUTPUT"
|
|
42
|
+
elif gh release view "$tag" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1 \
|
|
43
|
+
|| git ls-remote --exit-code --tags origin "refs/tags/$tag" >/dev/null 2>&1; then
|
|
44
|
+
echo "$tag already released"
|
|
45
|
+
echo "release=false" >> "$GITHUB_OUTPUT"
|
|
46
|
+
elif ! grep -qx "## $version" CHANGELOG.md; then
|
|
47
|
+
echo "CHANGELOG.md has no '## $version' section yet; not releasing"
|
|
48
|
+
echo "release=false" >> "$GITHUB_OUTPUT"
|
|
49
|
+
else
|
|
50
|
+
echo "release=true" >> "$GITHUB_OUTPUT"
|
|
51
|
+
fi
|
|
52
|
+
|
|
53
|
+
build:
|
|
54
|
+
needs: plan
|
|
55
|
+
if: needs.plan.outputs.release == 'true'
|
|
56
|
+
runs-on: ubuntu-latest
|
|
57
|
+
permissions:
|
|
58
|
+
contents: read
|
|
59
|
+
steps:
|
|
60
|
+
- uses: actions/checkout@v4
|
|
61
|
+
- uses: actions/setup-python@v5
|
|
62
|
+
with:
|
|
63
|
+
python-version: "3.14"
|
|
64
|
+
- run: pip install build
|
|
65
|
+
- run: python -m build
|
|
66
|
+
- uses: actions/upload-artifact@v4
|
|
67
|
+
with:
|
|
68
|
+
name: dist
|
|
69
|
+
path: dist/
|
|
70
|
+
|
|
71
|
+
pypi:
|
|
72
|
+
needs: build
|
|
73
|
+
runs-on: ubuntu-latest
|
|
74
|
+
environment:
|
|
75
|
+
name: pypi
|
|
76
|
+
url: https://pypi.org/project/aionecpbx/
|
|
77
|
+
permissions:
|
|
78
|
+
id-token: write
|
|
79
|
+
steps:
|
|
80
|
+
- uses: actions/download-artifact@v4
|
|
81
|
+
with:
|
|
82
|
+
name: dist
|
|
83
|
+
path: dist/
|
|
84
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
85
|
+
|
|
86
|
+
github:
|
|
87
|
+
needs: [plan, pypi]
|
|
88
|
+
runs-on: ubuntu-latest
|
|
89
|
+
permissions:
|
|
90
|
+
contents: write
|
|
91
|
+
steps:
|
|
92
|
+
- uses: actions/checkout@v4
|
|
93
|
+
- uses: actions/download-artifact@v4
|
|
94
|
+
with:
|
|
95
|
+
name: dist
|
|
96
|
+
path: dist/
|
|
97
|
+
- name: Release notes from CHANGELOG.md
|
|
98
|
+
env:
|
|
99
|
+
TAG: ${{ needs.plan.outputs.tag }}
|
|
100
|
+
run: |
|
|
101
|
+
version="${TAG#v}"
|
|
102
|
+
awk -v v="## $version" '$0==v{f=1;next} /^## /{f=0} f' CHANGELOG.md > notes.md
|
|
103
|
+
# A release published by hand creates the tag that starts this workflow,
|
|
104
|
+
# so the release may already exist: then attach the files and fill in
|
|
105
|
+
# empty notes instead of creating it.
|
|
106
|
+
- env:
|
|
107
|
+
GH_TOKEN: ${{ github.token }}
|
|
108
|
+
TAG: ${{ needs.plan.outputs.tag }}
|
|
109
|
+
run: |
|
|
110
|
+
if gh release view "$TAG" >/dev/null 2>&1; then
|
|
111
|
+
gh release upload "$TAG" dist/* --clobber
|
|
112
|
+
body=$(gh release view "$TAG" --json body --jq .body)
|
|
113
|
+
if [ -z "$(echo "$body" | tr -d '[:space:]')" ] && [ -s notes.md ]; then
|
|
114
|
+
gh release edit "$TAG" --notes-file notes.md
|
|
115
|
+
fi
|
|
116
|
+
else
|
|
117
|
+
gh release create "$TAG" dist/* --title "$TAG" --notes-file notes.md --target "$GITHUB_SHA"
|
|
118
|
+
fi
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
First release, moved out of the [NEC PBX integration for Home Assistant](https://github.com/olliemaller/ha-nec-pbx) so it can be used on its own.
|
|
6
|
+
|
|
7
|
+
- `SmdrClient`: SMDR TCP client with TCP keepalive and reconnect back-off (5 s to 5 min).
|
|
8
|
+
- `parse_line` and helpers: token-based SMDR parser for SL2100 and SV9100 layouts, phonebook and extension name lookup, Australian number normalising.
|
|
9
|
+
- `AmiClient`: FreePBX live calls (ringing, answered, ended) and do-not-disturb, with `async_test_login`.
|
|
10
|
+
- `aionecpbx` command-line tool: `parse`, `listen`, `ami`.
|
|
11
|
+
- Fully typed (`mypy --strict`), 98% test coverage.
|
aionecpbx-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Oliver Maller
|
|
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.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
Portions are derived from these MIT-licensed projects:
|
|
26
|
+
|
|
27
|
+
- escea (https://github.com/snikch/escea), Copyright (c) 2026 Oliver Maller
|
|
28
|
+
- ha-escea (https://github.com/derekclapham/ha-escea), Copyright (c) 2026
|
|
29
|
+
Derek Clapham: the web page (timers and diagnostics) request formats
|
aionecpbx-0.1.0/NOTICE
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
aionecpbx
|
|
2
|
+
Copyright (c) 2026 Oliver Maller. MIT licence (see LICENSE).
|
|
3
|
+
|
|
4
|
+
"NEC", "SL2100" and "SV9100" are trademarks of their owner and are used only to
|
|
5
|
+
describe compatibility. This project is not affiliated with or endorsed by NEC.
|
|
6
|
+
"Asterisk" and "FreePBX" are trademarks of Sangoma Technologies.
|
aionecpbx-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: aionecpbx
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Async clients for NEC SL2100 and SV9100 call logs (SMDR) and FreePBX live calls (AMI)
|
|
5
|
+
Project-URL: Homepage, https://github.com/olliemaller/aionecpbx
|
|
6
|
+
Project-URL: Issues, https://github.com/olliemaller/aionecpbx/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/olliemaller/aionecpbx/blob/main/CHANGELOG.md
|
|
8
|
+
Author: Oliver Maller
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
License-File: NOTICE
|
|
12
|
+
Keywords: asterisk,asyncio,freepbx,home-assistant,nec,sl2100,smdr,sv9100
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Framework :: AsyncIO
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Topic :: Communications :: Telephony
|
|
22
|
+
Classifier: Topic :: Home Automation
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.12
|
|
25
|
+
Provides-Extra: lint
|
|
26
|
+
Requires-Dist: mypy>=1.13; extra == 'lint'
|
|
27
|
+
Requires-Dist: ruff>=0.8; extra == 'lint'
|
|
28
|
+
Provides-Extra: test
|
|
29
|
+
Requires-Dist: pytest-asyncio>=0.24; extra == 'test'
|
|
30
|
+
Requires-Dist: pytest-cov>=5; extra == 'test'
|
|
31
|
+
Requires-Dist: pytest>=8; extra == 'test'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# aionecpbx
|
|
35
|
+
|
|
36
|
+
Async Python clients for **NEC SL2100 and SV9100** phone systems, and for live calls through **FreePBX**.
|
|
37
|
+
|
|
38
|
+
- **`SmdrClient`** keeps a connection to the phone system's SMDR (call log) port open, reconnects by itself, and hands over each line.
|
|
39
|
+
- **`parse_line`** turns an SMDR line into an `SmdrRecord`: direction, start, ring and talk time, line, extension, number, and whether it was missed. It works on tokens rather than fixed columns, so the different 35-01 and 35-02 layouts read the same way, and it never raises.
|
|
40
|
+
- **`AmiClient`** follows calls through FreePBX's Asterisk Manager Interface as they ring, are answered and end, and turns do-not-disturb on and off per extension.
|
|
41
|
+
|
|
42
|
+
Standard library only. Python 3.12 or later. It powers the [NEC PBX integration for Home Assistant](https://github.com/olliemaller/ha-nec-pbx).
|
|
43
|
+
|
|
44
|
+
## Install
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install aionecpbx
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Call log (SMDR)
|
|
51
|
+
|
|
52
|
+
Turn SMDR on in the phone system first (PC Pro or WebPro):
|
|
53
|
+
|
|
54
|
+
| Program | Setting |
|
|
55
|
+
|---|---|
|
|
56
|
+
| 10-20, type 5 | SMDR output TCP port: `4001` on Australian systems |
|
|
57
|
+
| 35-01-01 | Output port: **LAN** |
|
|
58
|
+
| 35-02-08 | Print incoming calls: **on** (or you won't see incoming or missed calls) |
|
|
59
|
+
|
|
60
|
+
Only one program can hold the SMDR port at a time.
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
import asyncio
|
|
64
|
+
from aionecpbx import SmdrClient, parse_line
|
|
65
|
+
|
|
66
|
+
def on_line(line: str) -> None:
|
|
67
|
+
record = parse_line(line, extensions={"103": "Kitchen"}, phonebook={"0491570156": "Alex"})
|
|
68
|
+
if record:
|
|
69
|
+
print(record.direction, record.station_name, record.number, record.caller_name, record.missed)
|
|
70
|
+
|
|
71
|
+
async def main() -> None:
|
|
72
|
+
client = SmdrClient("192.168.0.10", 4001, on_line)
|
|
73
|
+
client.start()
|
|
74
|
+
await asyncio.sleep(3600)
|
|
75
|
+
await client.stop()
|
|
76
|
+
|
|
77
|
+
asyncio.run(main())
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
A record is only written when a call **ends**. For calls as they ring, use FreePBX below.
|
|
81
|
+
|
|
82
|
+
## Live calls (FreePBX)
|
|
83
|
+
|
|
84
|
+
In FreePBX, **Settings > Asterisk Manager Users > Add Manager**: permit your address, and give it at least read `call` and `system`, and write `system` for do-not-disturb.
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from aionecpbx import AmiClient, LiveCall
|
|
88
|
+
|
|
89
|
+
def on_change(call: LiveCall, what: str) -> None:
|
|
90
|
+
print(what, call.caller_number, call.caller_name, call.extension) # ringing / answered / ended
|
|
91
|
+
|
|
92
|
+
client = AmiClient("192.168.0.20", 5038, "homeassistant", "secret", on_change)
|
|
93
|
+
client.start()
|
|
94
|
+
...
|
|
95
|
+
await client.set_dnd("103", True)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
A rejected login stops the client rather than retrying, so it can't lock the manager user out. `async_test_login()` checks a login once.
|
|
99
|
+
|
|
100
|
+
## Command line
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
aionecpbx parse "IC 0001 10:00 001 00:01:00 103 0491570156 0:08"
|
|
104
|
+
aionecpbx listen 192.168.0.10 # print each call as JSON as it ends
|
|
105
|
+
aionecpbx ami 192.168.0.20 --username homeassistant --secret ...
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Development
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
pip install -e ".[test,lint]"
|
|
112
|
+
pytest --cov=aionecpbx
|
|
113
|
+
ruff check . && ruff format --check . && mypy
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Releases are automatic: bump `__version__` in `src/aionecpbx/__init__.py`, add a `CHANGELOG.md` section, and push to `main`.
|
|
117
|
+
|
|
118
|
+
## Licence
|
|
119
|
+
|
|
120
|
+
[MIT](LICENSE). See [NOTICE](NOTICE) for trademarks.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# aionecpbx
|
|
2
|
+
|
|
3
|
+
Async Python clients for **NEC SL2100 and SV9100** phone systems, and for live calls through **FreePBX**.
|
|
4
|
+
|
|
5
|
+
- **`SmdrClient`** keeps a connection to the phone system's SMDR (call log) port open, reconnects by itself, and hands over each line.
|
|
6
|
+
- **`parse_line`** turns an SMDR line into an `SmdrRecord`: direction, start, ring and talk time, line, extension, number, and whether it was missed. It works on tokens rather than fixed columns, so the different 35-01 and 35-02 layouts read the same way, and it never raises.
|
|
7
|
+
- **`AmiClient`** follows calls through FreePBX's Asterisk Manager Interface as they ring, are answered and end, and turns do-not-disturb on and off per extension.
|
|
8
|
+
|
|
9
|
+
Standard library only. Python 3.12 or later. It powers the [NEC PBX integration for Home Assistant](https://github.com/olliemaller/ha-nec-pbx).
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install aionecpbx
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Call log (SMDR)
|
|
18
|
+
|
|
19
|
+
Turn SMDR on in the phone system first (PC Pro or WebPro):
|
|
20
|
+
|
|
21
|
+
| Program | Setting |
|
|
22
|
+
|---|---|
|
|
23
|
+
| 10-20, type 5 | SMDR output TCP port: `4001` on Australian systems |
|
|
24
|
+
| 35-01-01 | Output port: **LAN** |
|
|
25
|
+
| 35-02-08 | Print incoming calls: **on** (or you won't see incoming or missed calls) |
|
|
26
|
+
|
|
27
|
+
Only one program can hold the SMDR port at a time.
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
import asyncio
|
|
31
|
+
from aionecpbx import SmdrClient, parse_line
|
|
32
|
+
|
|
33
|
+
def on_line(line: str) -> None:
|
|
34
|
+
record = parse_line(line, extensions={"103": "Kitchen"}, phonebook={"0491570156": "Alex"})
|
|
35
|
+
if record:
|
|
36
|
+
print(record.direction, record.station_name, record.number, record.caller_name, record.missed)
|
|
37
|
+
|
|
38
|
+
async def main() -> None:
|
|
39
|
+
client = SmdrClient("192.168.0.10", 4001, on_line)
|
|
40
|
+
client.start()
|
|
41
|
+
await asyncio.sleep(3600)
|
|
42
|
+
await client.stop()
|
|
43
|
+
|
|
44
|
+
asyncio.run(main())
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
A record is only written when a call **ends**. For calls as they ring, use FreePBX below.
|
|
48
|
+
|
|
49
|
+
## Live calls (FreePBX)
|
|
50
|
+
|
|
51
|
+
In FreePBX, **Settings > Asterisk Manager Users > Add Manager**: permit your address, and give it at least read `call` and `system`, and write `system` for do-not-disturb.
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from aionecpbx import AmiClient, LiveCall
|
|
55
|
+
|
|
56
|
+
def on_change(call: LiveCall, what: str) -> None:
|
|
57
|
+
print(what, call.caller_number, call.caller_name, call.extension) # ringing / answered / ended
|
|
58
|
+
|
|
59
|
+
client = AmiClient("192.168.0.20", 5038, "homeassistant", "secret", on_change)
|
|
60
|
+
client.start()
|
|
61
|
+
...
|
|
62
|
+
await client.set_dnd("103", True)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
A rejected login stops the client rather than retrying, so it can't lock the manager user out. `async_test_login()` checks a login once.
|
|
66
|
+
|
|
67
|
+
## Command line
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
aionecpbx parse "IC 0001 10:00 001 00:01:00 103 0491570156 0:08"
|
|
71
|
+
aionecpbx listen 192.168.0.10 # print each call as JSON as it ends
|
|
72
|
+
aionecpbx ami 192.168.0.20 --username homeassistant --secret ...
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Development
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
pip install -e ".[test,lint]"
|
|
79
|
+
pytest --cov=aionecpbx
|
|
80
|
+
ruff check . && ruff format --check . && mypy
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Releases are automatic: bump `__version__` in `src/aionecpbx/__init__.py`, add a `CHANGELOG.md` section, and push to `main`.
|
|
84
|
+
|
|
85
|
+
## Licence
|
|
86
|
+
|
|
87
|
+
[MIT](LICENSE). See [NOTICE](NOTICE) for trademarks.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "aionecpbx"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Async clients for NEC SL2100 and SV9100 call logs (SMDR) and FreePBX live calls (AMI)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE", "NOTICE"]
|
|
12
|
+
authors = [{ name = "Oliver Maller" }]
|
|
13
|
+
requires-python = ">=3.12"
|
|
14
|
+
dependencies = []
|
|
15
|
+
keywords = ["nec", "sl2100", "sv9100", "smdr", "freepbx", "asterisk", "home-assistant", "asyncio"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Framework :: AsyncIO",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"Operating System :: OS Independent",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Programming Language :: Python :: 3.14",
|
|
25
|
+
"Topic :: Home Automation",
|
|
26
|
+
"Topic :: Communications :: Telephony",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/olliemaller/aionecpbx"
|
|
32
|
+
Issues = "https://github.com/olliemaller/aionecpbx/issues"
|
|
33
|
+
Changelog = "https://github.com/olliemaller/aionecpbx/blob/main/CHANGELOG.md"
|
|
34
|
+
|
|
35
|
+
[project.scripts]
|
|
36
|
+
aionecpbx = "aionecpbx.__main__:main"
|
|
37
|
+
|
|
38
|
+
[project.optional-dependencies]
|
|
39
|
+
test = ["pytest>=8", "pytest-asyncio>=0.24", "pytest-cov>=5"]
|
|
40
|
+
lint = ["ruff>=0.8", "mypy>=1.13"]
|
|
41
|
+
|
|
42
|
+
[tool.hatch.version]
|
|
43
|
+
path = "src/aionecpbx/__init__.py"
|
|
44
|
+
|
|
45
|
+
[tool.hatch.build.targets.wheel]
|
|
46
|
+
packages = ["src/aionecpbx"]
|
|
47
|
+
|
|
48
|
+
[tool.pytest.ini_options]
|
|
49
|
+
asyncio_mode = "auto"
|
|
50
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
51
|
+
testpaths = ["tests"]
|
|
52
|
+
|
|
53
|
+
[tool.coverage.report]
|
|
54
|
+
fail_under = 95
|
|
55
|
+
|
|
56
|
+
[tool.ruff]
|
|
57
|
+
target-version = "py312"
|
|
58
|
+
line-length = 100
|
|
59
|
+
|
|
60
|
+
[tool.ruff.lint]
|
|
61
|
+
select = ["E", "F", "W", "I", "B", "UP", "S", "SIM", "RUF", "ASYNC", "PT"]
|
|
62
|
+
ignore = ["S101", "S104", "ASYNC109"]
|
|
63
|
+
|
|
64
|
+
[tool.ruff.lint.isort]
|
|
65
|
+
force-sort-within-sections = true
|
|
66
|
+
known-first-party = ["aionecpbx"]
|
|
67
|
+
|
|
68
|
+
[tool.mypy]
|
|
69
|
+
strict = true
|
|
70
|
+
python_version = "3.12"
|
|
71
|
+
files = ["src"]
|
|
72
|
+
|
|
73
|
+
[tool.ruff.lint.per-file-ignores]
|
|
74
|
+
"tests/*" = ["S105", "S106", "S107", "S324", "E501", "ASYNC110", "PT011"]
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""Async clients for NEC SL2100 and SV9100 phone systems.
|
|
2
|
+
|
|
3
|
+
* :class:`SmdrClient` keeps a connection to the phone system's SMDR port and
|
|
4
|
+
hands over each call-log line; :func:`parse_line` turns a line into an
|
|
5
|
+
:class:`SmdrRecord`.
|
|
6
|
+
* :class:`AmiClient` follows live calls (ringing, answered, ended) through
|
|
7
|
+
FreePBX's Asterisk Manager Interface, and turns do-not-disturb on and off.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from .ami import (
|
|
13
|
+
DEFAULT_AMI_PORT,
|
|
14
|
+
LIVE_ANSWERED,
|
|
15
|
+
LIVE_ENDED,
|
|
16
|
+
LIVE_RINGING,
|
|
17
|
+
AmiAuthError,
|
|
18
|
+
AmiClient,
|
|
19
|
+
LiveCall,
|
|
20
|
+
async_test_login,
|
|
21
|
+
parse_message,
|
|
22
|
+
)
|
|
23
|
+
from .client import DEFAULT_SMDR_PORT, SmdrClient, async_probe
|
|
24
|
+
from .smdr import (
|
|
25
|
+
DATE_AUTO,
|
|
26
|
+
DATE_DMY,
|
|
27
|
+
DATE_MDY,
|
|
28
|
+
DATE_ORDERS,
|
|
29
|
+
DIRECTION_INCOMING,
|
|
30
|
+
DIRECTION_INTERNAL,
|
|
31
|
+
DIRECTION_OUTGOING,
|
|
32
|
+
DIRECTION_UNKNOWN,
|
|
33
|
+
SmdrRecord,
|
|
34
|
+
format_mapping,
|
|
35
|
+
is_record_line,
|
|
36
|
+
lookup_name,
|
|
37
|
+
normalise_number,
|
|
38
|
+
parse_line,
|
|
39
|
+
parse_mapping,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
__version__ = "0.1.0"
|
|
43
|
+
|
|
44
|
+
__all__ = [
|
|
45
|
+
"DATE_AUTO",
|
|
46
|
+
"DATE_DMY",
|
|
47
|
+
"DATE_MDY",
|
|
48
|
+
"DATE_ORDERS",
|
|
49
|
+
"DEFAULT_AMI_PORT",
|
|
50
|
+
"DEFAULT_SMDR_PORT",
|
|
51
|
+
"DIRECTION_INCOMING",
|
|
52
|
+
"DIRECTION_INTERNAL",
|
|
53
|
+
"DIRECTION_OUTGOING",
|
|
54
|
+
"DIRECTION_UNKNOWN",
|
|
55
|
+
"LIVE_ANSWERED",
|
|
56
|
+
"LIVE_ENDED",
|
|
57
|
+
"LIVE_RINGING",
|
|
58
|
+
"AmiAuthError",
|
|
59
|
+
"AmiClient",
|
|
60
|
+
"LiveCall",
|
|
61
|
+
"SmdrClient",
|
|
62
|
+
"SmdrRecord",
|
|
63
|
+
"__version__",
|
|
64
|
+
"async_probe",
|
|
65
|
+
"async_test_login",
|
|
66
|
+
"format_mapping",
|
|
67
|
+
"is_record_line",
|
|
68
|
+
"lookup_name",
|
|
69
|
+
"normalise_number",
|
|
70
|
+
"parse_line",
|
|
71
|
+
"parse_mapping",
|
|
72
|
+
"parse_message",
|
|
73
|
+
]
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""Command-line tool: ``python -m aionecpbx`` or ``aionecpbx``.
|
|
2
|
+
|
|
3
|
+
Examples::
|
|
4
|
+
|
|
5
|
+
aionecpbx parse "IC 0001 10:00 001 00:01:00 103 0491570156"
|
|
6
|
+
aionecpbx listen 192.168.0.10
|
|
7
|
+
aionecpbx ami 192.168.0.20 --username homeassistant --secret ...
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import argparse
|
|
13
|
+
import asyncio
|
|
14
|
+
from collections.abc import Sequence
|
|
15
|
+
import contextlib
|
|
16
|
+
from datetime import datetime
|
|
17
|
+
import json
|
|
18
|
+
import logging
|
|
19
|
+
import sys
|
|
20
|
+
from typing import Any
|
|
21
|
+
|
|
22
|
+
from . import (
|
|
23
|
+
DEFAULT_AMI_PORT,
|
|
24
|
+
DEFAULT_SMDR_PORT,
|
|
25
|
+
AmiAuthError,
|
|
26
|
+
AmiClient,
|
|
27
|
+
LiveCall,
|
|
28
|
+
SmdrClient,
|
|
29
|
+
is_record_line,
|
|
30
|
+
parse_line,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _print(value: Any) -> None:
|
|
35
|
+
print(json.dumps(value, default=str), flush=True)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _record(line: str) -> dict[str, Any] | None:
|
|
39
|
+
if not is_record_line(line):
|
|
40
|
+
return None
|
|
41
|
+
record = parse_line(line, now=datetime.now())
|
|
42
|
+
return record.as_dict() if record else None
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
async def _listen(host: str, port: int) -> None:
|
|
46
|
+
def on_line(line: str) -> None:
|
|
47
|
+
_print(_record(line) or {"raw": line, "record": False})
|
|
48
|
+
|
|
49
|
+
def on_state(connected: bool) -> None:
|
|
50
|
+
print(f"# {'connected to' if connected else 'lost'} {host}:{port}", file=sys.stderr)
|
|
51
|
+
|
|
52
|
+
client = SmdrClient(host, port, on_line, on_state)
|
|
53
|
+
client.start()
|
|
54
|
+
try:
|
|
55
|
+
await asyncio.Event().wait()
|
|
56
|
+
finally:
|
|
57
|
+
await client.stop()
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
async def _ami(host: str, port: int, username: str, secret: str) -> None:
|
|
61
|
+
def on_change(call: LiveCall, what: str) -> None:
|
|
62
|
+
_print({"change": what, **call.__dict__})
|
|
63
|
+
|
|
64
|
+
client = AmiClient(host, port, username, secret, on_change)
|
|
65
|
+
client.start()
|
|
66
|
+
try:
|
|
67
|
+
# The client only gives up when FreePBX rejects the login.
|
|
68
|
+
assert client._task is not None
|
|
69
|
+
await client._task
|
|
70
|
+
raise AmiAuthError(client.last_error or "login rejected")
|
|
71
|
+
finally:
|
|
72
|
+
await client.stop()
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def main(argv: Sequence[str] | None = None) -> None:
|
|
76
|
+
"""Run the command-line tool."""
|
|
77
|
+
parser = argparse.ArgumentParser(prog="aionecpbx", description=__doc__.splitlines()[0])
|
|
78
|
+
parser.add_argument("-v", "--verbose", action="store_true", help="debug logging")
|
|
79
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
80
|
+
|
|
81
|
+
parse = sub.add_parser("parse", help="parse one SMDR line")
|
|
82
|
+
parse.add_argument("line")
|
|
83
|
+
|
|
84
|
+
listen = sub.add_parser("listen", help="print each call as the phone system logs it")
|
|
85
|
+
listen.add_argument("host")
|
|
86
|
+
listen.add_argument("--port", type=int, default=DEFAULT_SMDR_PORT)
|
|
87
|
+
|
|
88
|
+
ami = sub.add_parser("ami", help="print live calls from FreePBX")
|
|
89
|
+
ami.add_argument("host")
|
|
90
|
+
ami.add_argument("--port", type=int, default=DEFAULT_AMI_PORT)
|
|
91
|
+
ami.add_argument("--username", required=True)
|
|
92
|
+
ami.add_argument("--secret", required=True)
|
|
93
|
+
|
|
94
|
+
args = parser.parse_args(argv)
|
|
95
|
+
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.WARNING)
|
|
96
|
+
if args.command == "parse":
|
|
97
|
+
_print(_record(args.line) or {"raw": args.line, "record": False})
|
|
98
|
+
return
|
|
99
|
+
with contextlib.suppress(KeyboardInterrupt): # pragma: no cover
|
|
100
|
+
try:
|
|
101
|
+
if args.command == "listen":
|
|
102
|
+
asyncio.run(_listen(args.host, args.port))
|
|
103
|
+
else:
|
|
104
|
+
asyncio.run(_ami(args.host, args.port, args.username, args.secret))
|
|
105
|
+
except AmiAuthError as err:
|
|
106
|
+
print(f"error: {err}", file=sys.stderr)
|
|
107
|
+
sys.exit(1)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
if __name__ == "__main__": # pragma: no cover
|
|
111
|
+
main()
|