diff --git a/ROADMAP.md b/ROADMAP.md index 546b518..8dd76aa 100644 --- a/ROADMAP.md +++ b/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 --- diff --git a/config/sensorpajen.env.example b/config/sensorpajen.env.example new file mode 100644 index 0000000..ae581e9 --- /dev/null +++ b/config/sensorpajen.env.example @@ -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 diff --git a/config/sensors.json.example b/config/sensors.json.example new file mode 100644 index 0000000..7a033cf --- /dev/null +++ b/config/sensors.json.example @@ -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" + } + ] +} diff --git a/LYWSD03MMC.py b/legacy/LYWSD03MMC.py similarity index 100% rename from LYWSD03MMC.py rename to legacy/LYWSD03MMC.py diff --git a/bluetooth_utils.py b/legacy/bluetooth_utils.py similarity index 100% rename from bluetooth_utils.py rename to legacy/bluetooth_utils.py diff --git a/sendToMQTT.sh b/legacy/sendToMQTT.sh similarity index 100% rename from sendToMQTT.sh rename to legacy/sendToMQTT.sh diff --git a/sensorer.ini b/legacy/sensorer.ini similarity index 100% rename from sensorer.ini rename to legacy/sensorer.ini diff --git a/sensorer.sh b/legacy/sensorer.sh similarity index 100% rename from sensorer.sh rename to legacy/sensorer.sh diff --git a/startup.sh b/legacy/startup.sh similarity index 100% rename from startup.sh rename to legacy/startup.sh diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..1c1abe2 --- /dev/null +++ b/pyproject.toml @@ -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_*"] diff --git a/src/sensorpajen/__init__.py b/src/sensorpajen/__init__.py new file mode 100644 index 0000000..d931f51 --- /dev/null +++ b/src/sensorpajen/__init__.py @@ -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" diff --git a/temperatur_koksfonstret.py b/temperatur_koksfonstret.py deleted file mode 100755 index abbc7f8..0000000 --- a/temperatur_koksfonstret.py +++ /dev/null @@ -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] ') - 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)