Skip to content

Commit 798cd6e

Browse files
author
kc3zvd
committed
feat: initial commit
0 parents  commit 798cd6e

18 files changed

+297
-0
lines changed

.coveragerc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# .coveragerc
2+
[report]
3+
show_missing = True

.github/workflows/test.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches: [main, master]
6+
pull_request:
7+
branches: [main, master]
8+
9+
concurrency:
10+
group: test-${{ github.head_ref }}
11+
cancel-in-progress: true
12+
13+
env:
14+
PYTHONUNBUFFERED: "1"
15+
FORCE_COLOR: "1"
16+
17+
jobs:
18+
run:
19+
name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }}
20+
runs-on: ${{ matrix.os }}
21+
strategy:
22+
fail-fast: false
23+
matrix:
24+
os: [ubuntu-latest, windows-latest, macos-latest]
25+
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
26+
27+
steps:
28+
- uses: actions/checkout@v3
29+
30+
- name: Set up Python ${{ matrix.python-version }}
31+
uses: actions/setup-python@v4
32+
with:
33+
python-version: ${{ matrix.python-version }}
34+
35+
- name: Install Hatch
36+
run: pip install --upgrade hatch
37+
38+
- name: Run static analysis
39+
run: hatch fmt --check
40+
41+
- name: Run tests
42+
run: hatch test --python ${{ matrix.python-version }} --cover --randomize --parallel --retries 2 --retry-delay 1

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.coverage
2+
.coverage.*
3+
.pytest_cache
4+
__pycache__
5+
*.code-workspace

Compose.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
broker:
3+
image: redis:7.4-alpine
4+
ports:
5+
- 6379:6379
6+
mongodb:
7+
image: mongo
8+
ports:
9+
- 27017:27017

LICENSE.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2024-present KC3ZVD <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# iot-state
2+
3+
[![PyPI - Version](https://img.shields.io/pypi/v/iot-state.svg)](https://pypi.org/project/iot-state)
4+
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/iot-state.svg)](https://pypi.org/project/iot-state)
5+
6+
-----
7+
8+
## Table of Contents
9+
10+
- [Installation](#installation)
11+
- [License](#license)
12+
13+
## Installation
14+
15+
```console
16+
pip install iot-state
17+
```
18+
19+
## License
20+
21+
`iot-state` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.

dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM python:3.12-slim
2+
3+
WORKDIR /app
4+
5+
COPY . /app
6+
7+
RUN pip install .
8+
9+
ENTRYPOINT ["celery", "-A", "wled_state_discovery.wled", "worker"]

hatch.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[version]
2+
path = "src/iot_state/__about__.py"
3+
4+
[envs.types]
5+
extra-dependencies = [
6+
"mypy>=1.0.0",
7+
]
8+
[envs.types.scripts]
9+
check = "mypy --install-types --non-interactive {args:src/iot_state tests}"
10+
11+
[envs.default.scripts]
12+
worker = "celery -A iot_state.workers.wled worker"
13+
listener = "python -m iot_state.listeners.wled"
14+
15+
[envs.dev]
16+
17+
[envs.dev.env-vars]
18+
CELERY_BROKER_URL = "redis://localhost:6379/0"
19+
MONGODB_URL = 'mongodb://localhost/iot'

pyproject.toml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[build-system]
2+
requires = ["hatchling"]
3+
build-backend = "hatchling.build"
4+
5+
[project]
6+
name = "iot-state"
7+
dynamic = ["version"]
8+
description = ''
9+
readme = "README.md"
10+
requires-python = ">=3.8"
11+
license = "MIT"
12+
keywords = []
13+
authors = [
14+
{ name = "KC3ZVD", email = "[email protected]" },
15+
]
16+
classifiers = [
17+
"Development Status :: 4 - Beta",
18+
"Programming Language :: Python",
19+
"Programming Language :: Python :: 3.8",
20+
"Programming Language :: Python :: 3.9",
21+
"Programming Language :: Python :: 3.10",
22+
"Programming Language :: Python :: 3.11",
23+
"Programming Language :: Python :: 3.12",
24+
"Programming Language :: Python :: Implementation :: CPython",
25+
"Programming Language :: Python :: Implementation :: PyPy",
26+
]
27+
dependencies = [
28+
"click",
29+
"zeroconf",
30+
"paho-mqtt",
31+
"requests",
32+
"semver",
33+
"Celery[redis]",
34+
"mongoengine"
35+
]
36+
37+
[project.urls]
38+
Documentation = "https://github.com/kc3zvd/iot-state#readme"
39+
Issues = "https://github.com/kc3zvd/iot-state/issues"
40+
Source = "https://github.com/kc3zvd/iot-state"
41+
42+
[project.scripts]
43+
iot-state = "iot_state.cli:iot_state"
44+
45+
[tool.coverage.run]
46+
source_pkgs = ["iot_state", "tests"]
47+
branch = true
48+
parallel = true
49+
omit = [
50+
"src/iot_state/__about__.py",
51+
]
52+
53+
[tool.coverage.paths]
54+
iot_state = ["src/iot_state", "*/iot-state/src/iot_state"]
55+
tests = ["tests", "*/iot-state/tests"]
56+
57+
[tool.coverage.report]
58+
exclude_lines = [
59+
"no cov",
60+
"if __name__ == .__main__.:",
61+
"if TYPE_CHECKING:",
62+
]

src/iot_state/__about__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-FileCopyrightText: 2024-present KC3ZVD <[email protected]>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
__version__ = "0.0.1"

src/iot_state/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: 2024-present KC3ZVD <[email protected]>
2+
#
3+
# SPDX-License-Identifier: MIT

src/iot_state/__main__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-FileCopyrightText: 2024-present KC3ZVD <[email protected]>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
import sys
5+
6+
if __name__ == "__main__":
7+
from iot_state.cli import iot_state
8+
9+
sys.exit(iot_state())

src/iot_state/cli/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-FileCopyrightText: 2024-present KC3ZVD <[email protected]>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
import click
5+
6+
from iot_state.__about__ import __version__
7+
8+
9+
@click.group(context_settings={"help_option_names": ["-h", "--help"]}, invoke_without_command=True)
10+
@click.version_option(version=__version__, prog_name="iot-state")
11+
def iot_state():
12+
click.echo("Hello world!")

src/iot_state/devices.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from mongoengine import Document, StringField, EmbeddedDocument, EmbeddedDocumentListField, ReferenceField
2+
from iot_state import utility
3+
4+
class State(EmbeddedDocument):
5+
"""Represents an IOT Device's state
6+
7+
Attributes:
8+
device_id (str): The Device object this state is associated with
9+
state_class (str): The state class for this state (open_close, on_off, humidity, etc.)
10+
value (str): The state value
11+
unit (str): The unit that the state value represents
12+
timestamp (str): The timestamp representing when the state changed
13+
14+
15+
"""
16+
device_id = ReferenceField('Device')
17+
state_class = StringField(required=True)
18+
value = StringField(required=True)
19+
unit = StringField()
20+
timestamp = StringField(required=True) # TODO: Find a more appropriate type
21+
22+
def friendly_name(self, device_name: str) -> str:
23+
"""Generates a machine-friendly name
24+
25+
Args:
26+
device_name (str): The parent device's name
27+
28+
Returns:
29+
str: Normalized, machne-friendly name
30+
"""
31+
return utility.normalize("%s_%s" % (device_name, self.state_class))
32+
33+
class Device(Document):
34+
"""Represents an IOT Device
35+
36+
Attributes:
37+
platform (str): The platform managing the device
38+
platform_id (str): The ID if the device on it's platform
39+
discovery_source (str): How the device was discovered
40+
name (str): The device's name
41+
area (str): The area/room where the device is located
42+
states [List(State)]: Embedded State documents for state history with this device
43+
44+
"""
45+
platform = StringField(required=True)
46+
platform_id = StringField(required=True)
47+
discovery_source = StringField(required=True)
48+
name = StringField(required=True)
49+
area = StringField(required=True)
50+
51+
52+
states = EmbeddedDocumentListField(State)
53+
54+
@property
55+
def friendly_name(self) -> str:
56+
"""Generates a machine-friendly name
57+
58+
Returns:
59+
str: Normalized, machine-friendly name
60+
"""
61+
return utility.normalize(self.name)
62+
63+
@property
64+
def area_name(self) -> str:
65+
"""Generates a machine-friendly area name
66+
67+
Returns:
68+
str: Normalized, machine-friendly area name
69+
"""
70+
return utility.normalize(self.area)

src/iot_state/utility.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def normalize(input: str) -> str:
2+
return input.lower().replace(' ', '_')

tests/__init__.py

Whitespace-only changes.

tests/test_iot_state.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from iot_state import devices
2+
3+
def test_iot_state():
4+
device = devices.Device()
5+
device.name = "Test IOT Device"
6+
device.area = "Test Area"
7+
8+
state = devices.State()
9+
state.state_class = "Humidity"
10+
11+
assert device.area_name == "test_area"
12+
assert state.friendly_name(device.friendly_name) == "test_iot_device_humidity"
13+
14+

tests/test_utility.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from iot_state import utility
2+
3+
def test_utility():
4+
assert utility.normalize('TesT sTRiNg') == 'test_string'

0 commit comments

Comments
 (0)