mbusreader API Documentation
i18n
Created on 2025-01-22
@author: wf
I18n
Simple internationalization class for message handling
Source code in mbusread/i18n.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
get(key, *args)
Get localized message with optional formatting
Source code in mbusread/i18n.py
28 29 30 31 32 33 34 | |
logger
Created on 2025-01-25
@author: wf
Logger
Logger singleton for M-Bus reader
Source code in mbusread/logger.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
mbus_config
Created on 2025-01-22 M-Bus configuration Classes to be read from configuration files e.g. YAML encoded @author: wf
Device
A device class for M-Bus devices storing manufacturer reference
Note on wakeup timing: The M-Bus standard formula suggests 33 bytes per 300 baud with start+8data+stop = 10 bits. However, we use configured pattern repetitions: - Default is 528 times (0x55) - Ultramaxx needs 1056 times (66 lines * 16 bytes) The total time includes sending these patterns at the given baudrate plus a fixed delay time.
Source code in mbusread/mbus_config.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
as_html()
Generate HTML representation of the device including wakeup info
Source code in mbusread/mbus_config.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
wakeup_time(baudrate=2400)
Calculate total wakeup time based on pattern repetitions
Source code in mbusread/mbus_config.py
118 119 120 121 122 123 | |
Link
a link
Source code in mbusread/mbus_config.py
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 | |
create(url, text, tooltip=None, target=None, style=None, url_encode=False)
staticmethod
Create a link for the given URL and text, with optional URL encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url |
str
|
The URL. |
required |
text |
str
|
The link text. |
required |
tooltip |
str
|
An optional tooltip. |
None
|
target |
str
|
Target attribute, e.g., _blank for opening the link in a new tab. |
None
|
style |
str
|
CSS style to be applied. |
None
|
url_encode |
bool
|
Flag to indicate if the URL needs encoding. default: False |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
HTML anchor tag as a string. |
Source code in mbusread/mbus_config.py
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 | |
MBusConfig
Manages M-Bus manufacture/devices/message hierarchy
Source code in mbusread/mbus_config.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
relink()
Link objects in the manufacturer/device/message hierarchy
Source code in mbusread/mbus_config.py
184 185 186 187 188 189 190 191 192 | |
MBusIoConfig
Configuration data class for M-Bus reader
Source code in mbusread/mbus_config.py
57 58 59 60 61 62 63 | |
MBusMessage
An M-Bus message
Source code in mbusread/mbus_config.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
Manufacturer
A manufacturer of M-Bus devices
Source code in mbusread/mbus_config.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
MqttConfig
MQTT configuration
Source code in mbusread/mbus_config.py
66 67 68 69 70 71 72 73 74 | |
mbus_mqtt
Created on 2025-01-24 based on https://github.com/ganehag/pyMeterBus/discussions/40
@author: Thorsten1982,wf
MBusMqtt
MQTT handler for M-Bus data
Source code in mbusread/mbus_mqtt.py
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 | |
publish(record)
Publish M-Bus data via MQTT
Source code in mbusread/mbus_mqtt.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
mbus_parser
Created on 2025-01-22 see also https://github.com/ganehag/pyMeterBus/discussions/40 @author: Thorsten1982, wf
MBusParser
parse MBus data
Source code in mbusread/mbus_parser.py
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 96 97 98 | |
extract_frame(data)
Extract valid M-Bus frame between start (0x68) and end (0x16) bytes
Source code in mbusread/mbus_parser.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
fromhex(x, base=16)
Convert hex string to integer
Source code in mbusread/mbus_parser.py
28 29 30 | |
get_frame_json(frame)
Workarounds for JSON bugs in pyMeterBus
Source code in mbusread/mbus_parser.py
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 | |
parse_mbus_frame(hex_data)
Parse M-Bus hex data and return mbus frame Returns tuple of (error_msg, mbus_frame)
Source code in mbusread/mbus_parser.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
mbus_reader
Created on 2025-01-24 based on https://github.com/ganehag/pyMeterBus/discussions/40
@author: Thorsten1982,wf
MBusReader
Reader for Meter Bus data
Source code in mbusread/mbus_reader.py
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
__init__(device, io_config=None, i18n=None, debug=False)
Initialize MBusReader with configuration
Source code in mbusread/mbus_reader.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
close()
Close the serial connection
Source code in mbusread/mbus_reader.py
150 151 152 153 | |
get_data(read_data_msg_key='read_data')
Get data from the M-Bus device
Source code in mbusread/mbus_reader.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
read_response(buffer_size=256)
Read the response from the device
Source code in mbusread/mbus_reader.py
137 138 139 140 141 142 143 144 145 146 147 148 | |
send_mbus_request(msg_id)
Send an M-Bus request to the device
Source code in mbusread/mbus_reader.py
123 124 125 126 127 128 129 130 131 132 133 134 135 | |
ser_write(msg, info, echo_display_len=16)
Writes a message to the serial port and validates the echo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg |
bytes
|
The message to write as a byte string. |
required |
info |
str
|
The log message key for identifying the operation. |
required |
Logs
A warning if the echo does not match the sent message. A debug message if the echo matches.
Source code in mbusread/mbus_reader.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
wake_up(device)
Perform the wakeup sequence based on device configuration
Source code in mbusread/mbus_reader.py
86 87 88 89 90 91 92 93 94 95 96 97 98 | |
mbus_reader_cmd
MBusCommunicator
communicate with an M-Bus device
Source code in mbusread/mbus_reader_cmd.py
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
mbus_viewer
MBusViewer
Bases: MBusParser
Enhanced M-Bus message viewer with improved error handling and UI organization
Source code in mbusread/mbus_viewer.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
create_textarea(label, placeholder=None, height='h-32')
Create a consistent textarea with error handling
Source code in mbusread/mbus_viewer.py
90 91 92 93 94 95 96 97 98 | |
setup_ui()
Create the main UI layout with two-column design
Source code in mbusread/mbus_viewer.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
RadioSelection
dataclass
Bases: Generic[T]
Generic radio button selection with type hints and improved structure
Source code in mbusread/mbus_viewer.py
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 | |
setup(on_change=None)
Initialize the radio selection UI components
Source code in mbusread/mbus_viewer.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
mbus_viewer_cmd
Created on 2025-01-22
@author: wf
NiceMBusCmd
Bases: WebserverCmd
command line handling for ngwidgets
Source code in mbusread/mbus_viewer_cmd.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
getArgParser(description, version_msg)
override the default argparser call
Source code in mbusread/mbus_viewer_cmd.py
21 22 23 24 25 26 27 28 29 30 31 32 | |
main(argv=None)
main call
Source code in mbusread/mbus_viewer_cmd.py
35 36 37 38 39 40 41 42 43 44 | |
mbus_viewer_server
Created on 22.01.2025
@author: wf
NiceMBus
Bases: InputWebSolution
Source code in mbusread/mbus_viewer_server.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
home()
async
provide the main content page
Source code in mbusread/mbus_viewer_server.py
61 62 63 64 65 66 67 68 69 70 | |
NiceMBusWebserver
Bases: InputWebserver
webserver to demonstrate ngwidgets capabilities
Source code in mbusread/mbus_viewer_server.py
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 | |
__init__()
Constructor
Source code in mbusread/mbus_viewer_server.py
36 37 38 39 40 41 | |
version
Created on 22.01.2025
@author: wf
Version
dataclass
Version handling for nicegui widgets
Source code in mbusread/version.py
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 | |
yamlable
Created on 2023-12-08, Extended on 2023-16-12 and 2024-01-25
@author: wf, ChatGPT
Prompts for the development and extension of the 'YamlAble' class within the 'yamable' module:
- Develop 'YamlAble' class in 'yamable' module. It should convert dataclass instances to/from YAML.
- Implement methods for YAML block scalar style and exclude None values in 'YamlAble' class.
- Add functionality to remove None values from dataclass instances before YAML conversion.
- Ensure 'YamlAble' processes only dataclass instances, with error handling for non-dataclass objects.
- Extend 'YamlAble' for JSON serialization and deserialization.
- Add methods for saving/loading dataclass instances to/from YAML and JSON files in 'YamlAble'.
- Implement loading of dataclass instances from URLs for both YAML and JSON in 'YamlAble'.
- Write tests for 'YamlAble' within the pyLodStorage context. Use 'samples 2' example from pyLoDStorage https://github.com/WolfgangFahl/pyLoDStorage/blob/master/lodstorage/sample2.py as a reference.
- Ensure tests cover YAML/JSON serialization, deserialization, and file I/O operations, using the sample-based approach..
- Use Google-style docstrings, comments, and type hints in 'YamlAble' class and tests.
- Adhere to instructions and seek clarification for any uncertainties.
- Add @lod_storable annotation support that will automatically YamlAble support and add @dataclass and @dataclass_json prerequisite behavior to a class
DateConvert
date converter
Source code in mbusread/yamlable.py
76 77 78 79 80 81 82 83 84 | |
YamlAble
Bases: Generic[T]
An extended YAML handler class for converting dataclass objects to and from YAML format, and handling loading from and saving to files and URLs.
Source code in mbusread/yamlable.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
from_dict2(data)
classmethod
Creates an instance of a dataclass from a dictionary, typically used in deserialization.
Source code in mbusread/yamlable.py
318 319 320 321 322 323 324 325 326 | |
from_yaml(yaml_str)
classmethod
Deserializes a YAML string to a dataclass instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yaml_str |
str
|
A string containing YAML formatted data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in mbusread/yamlable.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
load_from_json_file(filename)
classmethod
Loads a dataclass instance from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename |
str
|
The path to the JSON file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in mbusread/yamlable.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
load_from_json_url(url)
classmethod
Loads a dataclass instance from a JSON string obtained from a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url |
str
|
The URL pointing to the JSON data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in mbusread/yamlable.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
load_from_yaml_file(filename)
classmethod
Loads a dataclass instance from a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename |
str
|
The path to the YAML file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in mbusread/yamlable.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
load_from_yaml_url(url)
classmethod
Loads a dataclass instance from a YAML string obtained from a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url |
str
|
The URL pointing to the YAML data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in mbusread/yamlable.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
read_from_url(url)
classmethod
Helper method to fetch content from a URL.
Source code in mbusread/yamlable.py
253 254 255 256 257 258 259 260 261 262 | |
remove_ignored_values(value, ignore_none=True, ignore_underscore=False, ignore_empty=True)
classmethod
Recursively removes specified types of values from a dictionary or list. By default, it removes keys with None values. Optionally, it can also remove keys starting with an underscore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value |
Any
|
The value to process (dictionary, list, or other). |
required |
ignore_none |
bool
|
Flag to indicate whether None values should be removed. |
True
|
ignore_underscore |
bool
|
Flag to indicate whether keys starting with an underscore should be removed. |
False
|
ignore_empty |
bool
|
Flag to indicate whether empty collections should be removed. |
True
|
Source code in mbusread/yamlable.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
represent_literal(dumper, data)
Custom representer for block scalar style for strings.
Source code in mbusread/yamlable.py
111 112 113 114 115 116 117 | |
represent_none(_, __)
Custom representer for ignoring None values in the YAML output.
Source code in mbusread/yamlable.py
105 106 107 108 109 | |
save_to_json_file(filename, **kwargs)
Saves the current dataclass instance to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename |
str
|
The path where the JSON file will be saved. |
required |
**kwargs |
Additional keyword arguments for the |
{}
|
Source code in mbusread/yamlable.py
241 242 243 244 245 246 247 248 249 250 251 | |
save_to_yaml_file(filename)
Saves the current dataclass instance to a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename |
str
|
The path where the YAML file will be saved. |
required |
Source code in mbusread/yamlable.py
199 200 201 202 203 204 205 206 207 208 | |
to_yaml(ignore_none=True, ignore_underscore=True, allow_unicode=True, sort_keys=False)
Converts this dataclass object to a YAML string, with options to omit None values and/or underscore-prefixed variables, and using block scalar style for strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ignore_none |
bool
|
Flag to indicate whether None values should be removed from the YAML output. |
True
|
ignore_underscore |
bool
|
Flag to indicate whether attributes starting with an underscore should be excluded from the YAML output. |
True
|
allow_unicode |
bool
|
Flag to indicate whether to allow unicode characters in the output. |
True
|
sort_keys |
bool
|
Flag to indicate whether to sort the dictionary keys in the output. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
A string representation of the dataclass object in YAML format. |
Source code in mbusread/yamlable.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
lod_storable(cls)
Decorator to make a class LoDStorable by inheriting from YamlAble. This decorator also ensures the class is a dataclass and has JSON serialization/deserialization capabilities.
Source code in mbusread/yamlable.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |