-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-vedbus.py
More file actions
executable file
·96 lines (74 loc) · 3.14 KB
/
Copy pathtest-vedbus.py
File metadata and controls
executable file
·96 lines (74 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
"""
Test script to see if we can publish objects on com.victronenergy.ble
"""
import sys
import os
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
# Add velib to path
sys.path.insert(1, '/opt/victronenergy/dbus-systemcalc-py/ext/velib_python')
from vedbus import VeDbusService
print("Testing if we can add devices to com.victronenergy.ble service...")
# Initialize D-Bus
DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
# Check if com.victronenergy.ble exists
proxy = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
dbus_iface = dbus.Interface(proxy, 'org.freedesktop.DBus')
if 'com.victronenergy.ble' not in dbus_iface.ListNames():
print("ERROR: com.victronenergy.ble service not found")
sys.exit(1)
print("✓ com.victronenergy.ble service exists")
# We need to register our objects under the com.victronenergy.ble name
# even though we don't own it
try:
# The trick is to use the connection object, not create a BusName
# but still export objects that respond to that name
class VeDbusItemExport(dbus.service.Object):
"""Export a VeDbusItem-compatible object"""
def __init__(self, bus, path, value, conn=None):
# Don't pass conn if not provided - let Object handle it
if conn:
dbus.service.Object.__init__(self, conn, path)
else:
dbus.service.Object.__init__(self, bus, path)
self._value = value
@dbus.service.method('com.victronenergy.BusItem', out_signature='v')
def GetValue(self):
return self._value
@dbus.service.method('com.victronenergy.BusItem', in_signature='v', out_signature='i')
def SetValue(self, value):
self._value = value
return 0 # Success
@dbus.service.method('com.victronenergy.BusItem', out_signature='v')
def GetText(self):
return str(self._value)
# Create device items directly on the bus (not a BusName)
name_item = VeDbusItemExport(bus, '/Devices/test_integration/Name', 'Test Integration Device')
enabled_item = VeDbusItemExport(bus, '/Devices/test_integration/Enabled', 0)
print("✓ Successfully created device objects")
print(" - /Devices/test_integration/Name")
print(" - /Devices/test_integration/Enabled")
# Verify we can read it back
test_name = dbus.Interface(
bus.get_object('com.victronenergy.ble', '/Devices/test_integration/Name'),
'com.victronenergy.BusItem'
).GetValue()
print(f"✓ Verified: Name = '{test_name}'")
print("\nSuccess! We can add devices to com.victronenergy.ble")
print("Keeping service running for 10 seconds so you can inspect it...")
def timeout():
print("\nTest complete - cleaning up")
mainloop.quit()
return False
GLib.timeout_add_seconds(10, timeout)
mainloop = GLib.MainLoop()
mainloop.run()
except Exception as e:
print(f"✗ Failed to create device: {e}")
import traceback
traceback.print_exc()
sys.exit(1)