From b450bb388f22f5aa47a9739d8d3a17c6ddcd5184 Mon Sep 17 00:00:00 2001 From: slespersen Date: Sat, 7 Dec 2024 23:06:21 +0100 Subject: [PATCH] Added argument for setting the unit of measurement, and did a general clean up. --- Dockerfile | 3 ++- README.md | 9 ++++++--- evseMQTT/device.py | 1 + evseMQTT/event_handlers.py | 7 ++++++- evseMQTT/mqttpayloads.py | 6 +++--- main.py | 6 ++++-- 6 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 34423eb..986b007 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,7 @@ RUN apk add bluez ENV BLE_ADDRESS="" \ BLE_PASSWORD="" \ + UNIT="" \ MQTT_BROKER="" \ MQTT_PORT="1883" \ MQTT_USERNAME="" \ @@ -16,4 +17,4 @@ WORKDIR /app RUN pip install -r requirements.txt -CMD ["sh", "-c", "python main.py --address ${BLE_ADDRESS} --password ${BLE_PASSWORD} --mqtt --mqtt_broker ${MQTT_BROKER} --mqtt_port ${MQTT_PORT} --mqtt_user ${MQTT_USERNAME} --mqtt_password ${MQTT_PASSWORD} --logging_level ${LOGGING_LEVEL}"] +CMD ["sh", "-c", "python main.py --address ${BLE_ADDRESS} --password ${BLE_PASSWORD} --unit ${UNIT} --mqtt --mqtt_broker ${MQTT_BROKER} --mqtt_port ${MQTT_PORT} --mqtt_user ${MQTT_USERNAME} --mqtt_password ${MQTT_PASSWORD} --logging_level ${LOGGING_LEVEL}"] diff --git a/README.md b/README.md index e211684..b4021a9 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,8 @@ Since `evseMQTT` is not yet available on pip, it needs to be installed manually. ### Arguments - `--address`: (Required) The BLE device address. -- `--password`: (Optional) The BLE device password. Default is "123456". +- `--password`: (Required) The BLE device password. Default is "123456". +- `--unit`: (Optional) The unit of measurement, for consumed power - kW or W (default)". - `--mqtt`: (Optional) Enable MQTT. - `--mqtt_broker`: (Optional) The MQTT broker address. - `--mqtt_port`: (Optional) The MQTT broker port. @@ -72,6 +73,7 @@ Here's an example of how to run `main.py` with the necessary arguments: ```bash python main.py --address "your_device_mac_address" \ --password "your_6_digit_pin" \ + --unit "kW" \ --mqtt \ --mqtt_broker "your_mqtt_broker_address" \ --mqtt_port 1883 \ @@ -80,7 +82,7 @@ python main.py --address "your_device_mac_address" \ --logging_level "DEBUG" ``` -### run as container +### Run as container ```bash @@ -89,6 +91,7 @@ docker run -d --name evseMQTT \ -v /var/run/dbus:/run/dbus \ -e BLE_ADDRESS="your_device_mac_address" \ -e BLE_PASSWORD="your_6_digit_pin" \ + -e UNIT="kW" \ -e MQTT_BROKER="your_mqtt_broker_address" \ -e MQTT_PORT=1883 \ -e MQTT_USER="your_mqtt_username" \ @@ -97,7 +100,7 @@ docker run -d --name evseMQTT \ ghcr.io/slespersen/evsemqtt:latest ``` -### determine BLE address for your EVSE +### Determine BLE address for your EVSE look for a mac address started with `ACP#` like this: diff --git a/evseMQTT/device.py b/evseMQTT/device.py index 85a0eb3..605c64d 100644 --- a/evseMQTT/device.py +++ b/evseMQTT/device.py @@ -3,6 +3,7 @@ def __init__(self, mac): self.initialization_state = False self.ble_password = "123456" self.ble_user_id = [101, 118, 115, 101, 77, 81, 84, 84, 0, 0, 0, 0, 0, 0, 0, 0] # evseMQTT in ascii 16 bytes + self.unit = "W" self._type = None self._phases = None self._manufacturer = None diff --git a/evseMQTT/event_handlers.py b/evseMQTT/event_handlers.py index da1eaa5..198ed68 100644 --- a/evseMQTT/event_handlers.py +++ b/evseMQTT/event_handlers.py @@ -73,6 +73,11 @@ async def handle_notification(self, sender, message): # Update device info if command 262 is received if cmd in [4, 13]: self.logger.info(f"Device sent a single charge ac status") + + # If the unit is kW we divide by 1000, to achieve it in kW + if self.device.unit == "kW": + data['current_energy'] = data['current_energy'] / 1000 + self.device.charge = data # Update device info if command 262 is received @@ -135,4 +140,4 @@ async def handle_notification(self, sender, message): topic = self.forward_messages[cmd] self.callback(self.device.info['serial'], topic, getattr(self.device, topic)) - return cmd + return cmd \ No newline at end of file diff --git a/evseMQTT/mqttpayloads.py b/evseMQTT/mqttpayloads.py index d95e8c6..7482971 100644 --- a/evseMQTT/mqttpayloads.py +++ b/evseMQTT/mqttpayloads.py @@ -70,7 +70,6 @@ def __init__(self, device): "payload_not_available": "offline", "options": list(set(Constants.CHARGING_STATUS_DESCRIPTIONS.values())), "value_template": "{{ value_json.charging_status_description }}", - #"entity_category": "diagnostic" }, "current_state": { "name": "Current State", @@ -83,7 +82,6 @@ def __init__(self, device): "payload_not_available": "offline", "options": Constants.CURRENT_STATE, "value_template": "{{ value_json.current_state }}", - #"entity_category": "diagnostic" }, "plug_state": { "name": "Plug State", @@ -115,6 +113,7 @@ def __init__(self, device): "name": "Date", "icon": "mdi:calendar-month-outline", "unique_id": f"{self.device.info['serial']}", + "enabled_by_default": False, "state_topic": f"evseMQTT/{self.device.info['serial']}/state/config", "availability_topic": f"evseMQTT/{self.device.info['serial']}/availability", "value_template": "{{ value_json.system_time_raw | int | timestamp_custom('%Y-%m-%d', true) }}", @@ -124,6 +123,7 @@ def __init__(self, device): "name": "Time", "icon": "mdi:clock-outline", "unique_id": f"{self.device.info['serial']}", + "enabled_by_default": False, "state_topic": f"evseMQTT/{self.device.info['serial']}/state/config", "availability_topic": f"evseMQTT/{self.device.info['serial']}/availability", "value_template": "{{ value_json.system_time_raw | int | timestamp_custom('%H:%M', true) }}", @@ -152,7 +152,7 @@ def __init__(self, device): "availability_topic": f"evseMQTT/{self.device.info['serial']}/availability", "payload_available": "online", "payload_not_available": "offline", - "unit_of_measurement": "W", + "unit_of_measurement": "W" if self.device.unit == "W" else "kW", "state_class": "measurement", "value_template": "{{ value_json.current_energy }}", "entity_category": "diagnostic" diff --git a/main.py b/main.py index 6fad2e7..8b64778 100644 --- a/main.py +++ b/main.py @@ -4,12 +4,13 @@ from evseMQTT import BLEManager, Constants, Device, EventHandlers, Commands, Logger, MQTTClient, MQTTCallback, MQTTPayloads, Utils class Manager: - def __init__(self, address, ble_password, mqtt_enabled=False, mqtt_settings=None, logging_level=logging.INFO): + def __init__(self, address, ble_password, unit, mqtt_enabled=False, mqtt_settings=None, logging_level=logging.INFO): self.setup_logging(logging_level) self.logger = logging.getLogger("evseMQTT") debug = logging_level == logging.DEBUG # Determine if debug logging is enabled self.device = Device(address) + self.device.unit = unit # Set the BLE password self.device.ble_password = ble_password @@ -115,6 +116,7 @@ async def restart_run(self, address = None): parser = argparse.ArgumentParser(description="BLE Manager") parser.add_argument("--address", type=str, required=True, help="BLE device address") parser.add_argument("--password", type=str, required=True, help="BLE device password") + parser.add_argument("--unit", type=str, default="W", help="Set the unit of measurement, for consumed power - kW or W") parser.add_argument("--mqtt", action='store_true', help="Enable MQTT") parser.add_argument("--mqtt_broker", type=str, help="MQTT broker address") parser.add_argument("--mqtt_port", type=int, help="MQTT broker port") @@ -133,5 +135,5 @@ async def restart_run(self, address = None): logging_level = getattr(logging, args.logging_level.upper(), logging.INFO) - manager = Manager(args.address, ble_password=args.password, mqtt_enabled=args.mqtt, mqtt_settings=mqtt_settings, logging_level=logging_level) + manager = Manager(args.address, ble_password=args.password, unit=args.unit, mqtt_enabled=args.mqtt, mqtt_settings=mqtt_settings, logging_level=logging_level) asyncio.run(manager.run(args.address))