Skip to content

Commit 6ec05c0

Browse files
authored
Merge pull request #982 from Morg42/ehz
Smartmeter: neues Plugin für SML- und DLMS-Smartmeter
2 parents 9b1e2a4 + 2f4bafd commit 6ec05c0

File tree

11 files changed

+4032
-0
lines changed

11 files changed

+4032
-0
lines changed

smartmeter/__init__.py

Lines changed: 539 additions & 0 deletions
Large diffs are not rendered by default.

smartmeter/conversion.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/usr/bin/env python3
2+
# vim: set encoding=utf-8 tabstop=4 softtabstop=4 shiftwidth=4 expandtab
3+
#########################################################################
4+
# Copyright 2016 - 2018 Bernd Meiners Bernd.Meiners@mail.de
5+
#########################################################################
6+
#
7+
# This file is part of SmartHomeNG.py.
8+
# Visit: https://github.com/smarthomeNG/
9+
# https://knx-user-forum.de/forum/supportforen/smarthome-py
10+
#
11+
# SmartHomeNG.py is free software: you can redistribute it and/or modify
12+
# it under the terms of the GNU General Public License as published by
13+
# the Free Software Foundation, either version 3 of the License, or
14+
# (at your option) any later version.
15+
#
16+
# SmartHomeNG.py is distributed in the hope that it will be useful,
17+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
# GNU General Public License for more details.
20+
#
21+
# You should have received a copy of the GNU General Public License
22+
# along with SmartHomeNG.py. If not, see <http://www.gnu.org/licenses/>.
23+
#########################################################################
24+
25+
26+
__license__ = "GPL"
27+
__version__ = "2.0"
28+
__revision__ = "0.1"
29+
__docformat__ = 'reStructuredText'
30+
31+
import datetime
32+
33+
CONVERTERS = {
34+
'int': 'int',
35+
'float': 'float',
36+
'ZST10': 'datetime',
37+
'ZST12': 'datetime',
38+
'D6': 'date',
39+
'Z6': 'time',
40+
'Z4': 'time',
41+
'num': 'num'
42+
}
43+
44+
45+
class Conversion:
46+
def _from_ZST10(self, text: str) -> datetime.datetime:
47+
"""
48+
this function converts a string of form "YYMMDDhhmm" into a datetime object
49+
:param text: string to convert
50+
:return: a datetime object upon success or None if error found by malformed string
51+
"""
52+
if len(text) != 10:
53+
raise ValueError("too few characters for date/time code from OBIS")
54+
55+
year = int(text[0:2]) + 2000
56+
month = int(text[2:4])
57+
day = int(text[4:6])
58+
hour = int(text[6:8])
59+
minute = int(text[8:10])
60+
return datetime.datetime(year, month, day, hour, minute, 0)
61+
62+
def _from_ZST12(self, text: str) -> datetime.datetime:
63+
"""
64+
this function converts a string of form "YYMMDDhhmmss" into a datetime object
65+
:param text: string to convert
66+
:return: a datetime object upon success or None if error found by malformed string
67+
"""
68+
if len(text) != 12:
69+
raise ValueError("too few characters for date/time code from OBIS")
70+
71+
year = int(text[0:2]) + 2000
72+
month = int(text[2:4])
73+
day = int(text[4:6])
74+
hour = int(text[6:8])
75+
minute = int(text[8:10])
76+
second = int(text[10:12])
77+
return datetime.datetime(year, month, day, hour, minute, second)
78+
79+
def _from_D6(self, text: str) -> datetime.date:
80+
"""
81+
this function converts a string of form "YYMMDD" into a datetime.date object
82+
:param text: string to convert
83+
:return: a datetime.date object upon success or None if error found by malformed string
84+
"""
85+
if len(text) != 6:
86+
raise ValueError("too few characters for date code from OBIS")
87+
88+
year = int(text[0:2]) + 2000
89+
month = int(text[2:4])
90+
day = int(text[4:6])
91+
return datetime.date(year, month, day)
92+
93+
def _from_Z4(self, text: str) -> datetime.time:
94+
"""
95+
this function converts a string of form "hhmm" into a datetime.time object
96+
:param text: string to convert
97+
:return: a datetime.time object upon success or None if error found by malformed string
98+
"""
99+
if len(text) != 4:
100+
raise ValueError("too few characters for time code from OBIS")
101+
102+
hour = int(text[0:2])
103+
minute = int(text[2:4])
104+
return datetime.time(hour, minute)
105+
106+
def _from_Z6(self, text: str) -> datetime.time:
107+
"""
108+
this function converts a string of form "hhmmss" into a datetime.time object
109+
:param text: string to convert
110+
:return: a datetime.time object upon success or None if error found by malformed string
111+
"""
112+
if len(text) != 6:
113+
raise ValueError("too few characters for time code from OBIS")
114+
115+
hour = int(text[0:2])
116+
minute = int(text[2:4])
117+
second = int(text[4:6])
118+
return datetime.time(hour, minute, second)
119+
120+
def _convert_value(self, val, converter: str = ''):
121+
"""
122+
This function converts the OBIS value to a user chosen valalue
123+
:param val: the value to convert given as string
124+
:param converter: type of value, should contain one of CONVERTERS
125+
:return: after successful conversion the value in converted form
126+
"""
127+
if converter not in CONVERTERS:
128+
return val
129+
130+
try:
131+
if converter in ('num', 'float', 'int'):
132+
133+
if converter in ('num', 'int'):
134+
try:
135+
return int(val)
136+
except (ValueError, AttributeError):
137+
if converter == 'int':
138+
raise ValueError
139+
140+
# try/except to catch floats like '1.0' and '1,0'
141+
try:
142+
return float(val)
143+
except ValueError:
144+
if ',' in val:
145+
val = val.replace(',', '.')
146+
return float(val)
147+
else:
148+
raise ValueError
149+
150+
if not val.isdigit():
151+
raise ValueError("only digits allowed for date/time code from OBIS")
152+
153+
if converter == 'int':
154+
return int(val)
155+
156+
# try to find self._from_<converter> -> run it and return result
157+
if hasattr(self, f'_from_{converter}'):
158+
return getattr(self, f'_from_{converter}')(val)
159+
160+
# no suitable converter found
161+
raise ValueError
162+
163+
except ValueError as e:
164+
raise ValueError(f'could not convert from "{val}" to a {CONVERTERS[converter]} ({e})')

0 commit comments

Comments
 (0)