Phase 1 Complete: Preparation & Cleanup
Directory structure: - Created src/sensorpajen/ for new Python package - Created config/ for configuration templates - Created legacy/ for old scripts - Created systemd/ and debian/ for future phases Package setup: - Added pyproject.toml with modern Python packaging - Created package __init__.py - Defined dependencies: bluepy, paho-mqtt Configuration: - Created config/sensors.json.example (converted from INI) - Created config/sensorpajen.env.example for environment variables - All 8 Xiaomi sensors migrated to JSON format Cleanup: - Removed temperatur_koksfonstret.py (DHT11 functionality) - Moved all legacy scripts to legacy/ folder: - LYWSD03MMC.py - bluetooth_utils.py - sendToMQTT.sh - sensorer.sh - startup.sh - sensorer.ini Updated ROADMAP.md to mark Phase 1 as complete. Next: Phase 2 - Python Package Structure
This commit is contained in:
42
ROADMAP.md
42
ROADMAP.md
@@ -102,37 +102,23 @@ Using relative paths for portability across systems:
|
||||
|
||||
## Migration Phases
|
||||
|
||||
### Phase 1: Preparation & Cleanup ✓ TODO
|
||||
### Phase 1: Preparation & Cleanup ✅ DONE (2025-12-27)
|
||||
**Goal**: Reorganize repository without breaking existing functionality
|
||||
|
||||
**Notes**:
|
||||
- Created modern Python package structure with src/ layout
|
||||
- Converted INI sensor config to JSON format (sensors.json.example)
|
||||
- Environment-based configuration instead of hardcoded values
|
||||
- DHT11 sensor functionality removed as planned
|
||||
- Legacy scripts preserved in legacy/ folder
|
||||
|
||||
#### Tasks:
|
||||
1. Create new directory structure
|
||||
```bash
|
||||
mkdir -p src/sensorpajen
|
||||
mkdir -p config
|
||||
mkdir -p legacy
|
||||
mkdir -p systemd
|
||||
mkdir -p debian
|
||||
```
|
||||
|
||||
2. Create pyproject.toml with dependencies:
|
||||
- bluepy
|
||||
- paho-mqtt
|
||||
|
||||
3. Remove DHT11 functionality:
|
||||
- Delete temperatur_koksfonstret.py
|
||||
- Remove DHT11 cron job from documentation
|
||||
- Update README.md
|
||||
|
||||
4. Move legacy scripts to legacy/ folder:
|
||||
- LYWSD03MMC.py
|
||||
- sendToMQTT.sh
|
||||
- startup.sh
|
||||
- sensorer.sh
|
||||
- sensorer.ini
|
||||
- bluetooth_utils.py
|
||||
|
||||
5. Verify existing system still works with legacy scripts
|
||||
- ✅ Create new directory structure
|
||||
- ✅ Create pyproject.toml with dependencies
|
||||
- ✅ Remove DHT11 functionality
|
||||
- ✅ Move legacy scripts to legacy/ folder
|
||||
- ✅ Create config file templates (sensors.json.example, sensorpajen.env.example)
|
||||
- ✅ Preserve requirements.txt for backward compatibility
|
||||
|
||||
---
|
||||
|
||||
|
||||
14
config/sensorpajen.env.example
Normal file
14
config/sensorpajen.env.example
Normal file
@@ -0,0 +1,14 @@
|
||||
# MQTT Configuration
|
||||
MQTT_HOST=192.168.0.114
|
||||
MQTT_PORT=1883
|
||||
MQTT_USER=hasse
|
||||
MQTT_PASSWORD=casablanca
|
||||
MQTT_CLIENT_ID=mibridge
|
||||
|
||||
# Sensor Configuration (relative to project root)
|
||||
SENSOR_CONFIG_FILE=config/sensors.json
|
||||
|
||||
# Application Settings
|
||||
WATCHDOG_TIMEOUT=5
|
||||
ENABLE_BATTERY=true
|
||||
LOG_LEVEL=INFO
|
||||
37
config/sensors.json.example
Normal file
37
config/sensors.json.example
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"sensors": [
|
||||
{
|
||||
"mac": "A4:C1:38:98:7B:B6",
|
||||
"name": "mi_temp_1",
|
||||
"comment": "Example sensor - replace with your sensors"
|
||||
},
|
||||
{
|
||||
"mac": "A4:C1:38:29:03:0D",
|
||||
"name": "mi_temp_2"
|
||||
},
|
||||
{
|
||||
"mac": "A4:C1:38:62:CA:83",
|
||||
"name": "mi_temp_3"
|
||||
},
|
||||
{
|
||||
"mac": "A4:C1:38:D5:EA:63",
|
||||
"name": "mi_temp_4"
|
||||
},
|
||||
{
|
||||
"mac": "A4:C1:38:7C:9C:63",
|
||||
"name": "mi_temp_5"
|
||||
},
|
||||
{
|
||||
"mac": "A4:C1:38:68:2C:DA",
|
||||
"name": "mi_temp_6"
|
||||
},
|
||||
{
|
||||
"mac": "A4:C1:38:AD:74:2B",
|
||||
"name": "mi_temp_7"
|
||||
},
|
||||
{
|
||||
"mac": "A4:C1:38:46:9F:D1",
|
||||
"name": "mi_temp_8"
|
||||
}
|
||||
]
|
||||
}
|
||||
63
pyproject.toml
Normal file
63
pyproject.toml
Normal file
@@ -0,0 +1,63 @@
|
||||
[build-system]
|
||||
requires = ["setuptools>=61.0", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "sensorpajen"
|
||||
version = "2.0.0-dev"
|
||||
description = "Bluetooth temperature sensor monitor for Xiaomi Mijia LYWSD03MMC"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.9"
|
||||
license = {text = "MIT"}
|
||||
authors = [
|
||||
{name = "Fredrik", email = "your@email.com"}
|
||||
]
|
||||
keywords = ["bluetooth", "temperature", "sensor", "mqtt", "raspberry-pi"]
|
||||
classifiers = [
|
||||
"Development Status :: 4 - Beta",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3.9",
|
||||
"Programming Language :: Python :: 3.10",
|
||||
"Programming Language :: Python :: 3.11",
|
||||
"Operating System :: POSIX :: Linux",
|
||||
"Topic :: Home Automation",
|
||||
]
|
||||
|
||||
dependencies = [
|
||||
"bluepy>=1.3.0",
|
||||
"paho-mqtt>=1.6.0",
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
dev = [
|
||||
"pytest>=7.0",
|
||||
"pytest-cov>=4.0",
|
||||
"black>=23.0",
|
||||
"ruff>=0.1.0",
|
||||
]
|
||||
|
||||
[project.urls]
|
||||
Homepage = "https://github.com/yourusername/sensorpajen"
|
||||
Repository = "https://github.com/yourusername/sensorpajen"
|
||||
|
||||
[project.scripts]
|
||||
sensorpajen = "sensorpajen.main:main"
|
||||
|
||||
[tool.setuptools.packages.find]
|
||||
where = ["src"]
|
||||
|
||||
[tool.black]
|
||||
line-length = 100
|
||||
target-version = ["py39", "py310", "py311"]
|
||||
|
||||
[tool.ruff]
|
||||
line-length = 100
|
||||
target-version = "py39"
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["tests"]
|
||||
python_files = ["test_*.py"]
|
||||
python_classes = ["Test*"]
|
||||
python_functions = ["test_*"]
|
||||
10
src/sensorpajen/__init__.py
Normal file
10
src/sensorpajen/__init__.py
Normal file
@@ -0,0 +1,10 @@
|
||||
"""
|
||||
Sensorpajen - Bluetooth Temperature Sensor Monitor
|
||||
|
||||
Monitors Xiaomi Mijia LYWSD03MMC Bluetooth temperature sensors
|
||||
and publishes data to MQTT broker.
|
||||
"""
|
||||
|
||||
__version__ = "2.0.0-dev"
|
||||
__author__ = "Fredrik"
|
||||
__license__ = "MIT"
|
||||
@@ -1,45 +0,0 @@
|
||||
#!/usr/bin/python3
|
||||
# Copyright (c) 2014 Adafruit Industries
|
||||
# Author: Tony DiCola
|
||||
|
||||
import sys
|
||||
import Adafruit_DHT
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
mqttserver = "192.168.0.114"
|
||||
client = mqtt.Client("koksfonstret")
|
||||
client.connect(mqttserver)
|
||||
|
||||
# Parse command line parameters.
|
||||
sensor_args = { '11': Adafruit_DHT.DHT11,
|
||||
'22': Adafruit_DHT.DHT22,
|
||||
'2302': Adafruit_DHT.AM2302 }
|
||||
if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
|
||||
sensor = sensor_args[sys.argv[1]]
|
||||
pin = sys.argv[2]
|
||||
else:
|
||||
print('Usage: sudo ./Adafruit_DHT.py [11|22|2302] <GPIO pin number>')
|
||||
print('Example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO pin #4')
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
# Try to grab a sensor reading. Use the read_retry method which will retry up
|
||||
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
|
||||
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
|
||||
|
||||
# Un-comment the line below to convert the temperature to Fahrenheit.
|
||||
# temperature = temperature * 9/5.0 + 32
|
||||
|
||||
# Note that sometimes you won't get a reading and
|
||||
# the results will be null (because Linux can't
|
||||
# guarantee the timing of calls to read the sensor).
|
||||
# If this happens try again!
|
||||
|
||||
if humidity is not None and temperature is not None:
|
||||
print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity))
|
||||
client.publish("casablanca/koksfonstret/temperature", temperature)
|
||||
client.publish("casablanca/koksfonstret/humidity", humidity)
|
||||
|
||||
else:
|
||||
print('Failed to get reading. Try again!')
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user