Skip to content

Commit

Permalink
Added argument for setting the unit of measurement, and did a general…
Browse files Browse the repository at this point in the history
… clean up.
  • Loading branch information
slespersen committed Dec 7, 2024
1 parent 9565ee6 commit b450bb3
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ RUN apk add bluez

ENV BLE_ADDRESS="" \
BLE_PASSWORD="" \
UNIT="" \
MQTT_BROKER="" \
MQTT_PORT="1883" \
MQTT_USERNAME="" \
Expand All @@ -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}"]
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 \
Expand All @@ -80,7 +82,7 @@ python main.py --address "your_device_mac_address" \
--logging_level "DEBUG"
```
### run as container
### Run as container
```bash
Expand All @@ -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" \
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions evseMQTT/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion evseMQTT/event_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions evseMQTT/mqttpayloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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) }}",
Expand All @@ -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) }}",
Expand Down Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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))

0 comments on commit b450bb3

Please sign in to comment.