1
1
#!/usr/bin/python3
2
-
2
+
3
3
# This file is part of the sensors2mqtt distribution (https://github.com/DCL777/sensors2mqtt.git).
4
- # Copyright (c) 2020 Dries Claerbout
5
- #
6
- # This program is free software: you can redistribute it and/or modify
7
- # it under the terms of the GNU General Public License as published by
4
+ # Copyright (c) 2020 Dries Claerbout
5
+
6
+ # This program is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
8
# the Free Software Foundation, version 3.
9
- #
10
- # This program is distributed in the hope that it will be useful, but
11
- # WITHOUT ANY WARRANTY; without even the implied warranty of
12
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9
+
10
+ # This program is distributed in the hope that it will be useful, but
11
+ # WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
13
# General Public License for more details.
14
- #
15
- # You should have received a copy of the GNU General Public License
14
+
15
+ # You should have received a copy of the GNU General Public License
16
16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
17
18
18
import logging
19
19
20
20
import os .path
21
- from os import path
21
+ # from os import path
22
+
22
23
23
24
class BColors :
25
+ """CLI colors"""
24
26
HEADER = '\033 [95m'
25
27
OKBLUE = '\033 [94m'
26
28
OKCYAN = '\033 [96m'
@@ -31,46 +33,55 @@ class BColors:
31
33
BOLD = '\033 [1m'
32
34
UNDERLINE = '\033 [4m'
33
35
36
+
34
37
class Sensor :
35
- def __init__ (self , supported_system , manufacturer , sensorName , function ,protocol ,mqtt_client , parameters ):
36
- self .supported_system = supported_system
37
- self .manufacturer = manufacturer
38
- self .sensorName = sensorName
39
- self .function = function
40
- self .protocol = protocol
41
- self .mqtt_client = mqtt_client
42
-
43
- self .update_interval = parameters ['update_interval' ]
44
-
45
- # replace spaces and dashes to underscore !!
46
- sensorNameModified = self .sensorName .replace (' ' , '_' )
47
- sensorNameModified = sensorNameModified .replace ('-' ,'_' )
48
-
49
- self .ownDir = f"{ os .getcwd ()} /sensors/{ self .manufacturer } _{ sensorNameModified } /"
50
-
51
- self .parameters = parameters #.get(f"{self.supported_system}_{self.manufacturer}_{sensorNameModified}")
52
- self .logger = logging .getLogger (__name__ )
53
-
54
-
55
- def printInfo (self ):
56
- print (f"{ BColors .OKCYAN } -> { self .sensorName } \t { self .manufacturer } \t { self .function } \t { self .protocol } \t { self .supported_system } { BColors .ENDC } " )
57
- #self.count = 0
58
- #for a_sensor in self.parameters:
59
- # self.count = self.count +1
60
- #print(f" -> Sensor {self.count}:")
61
- for an_item in self .parameters .items ():
62
- print (f" -> { an_item [0 ]} = { an_item [1 ]} " )
63
- #print(f" -> {an_item}")
64
- #print(f" -> {a_sensor.items()}")
65
-
66
- def is_configured (self ):
67
- return bool (self .parameters )
68
-
69
- def send_value_over_mqtt (self ,mqtt_top_dir_name ):
70
- pass
71
-
72
- def on_exit (self ):
73
- pass
74
-
75
- def get_update_interval (self ):
76
- return self .update_interval
38
+ """Default sensor class. Every sensor must be a instance of this class"""
39
+
40
+ def __init__ (self , supported_system , manufacturer , sensor_name ,
41
+ function , protocol , mqtt_client , parameters ):
42
+ self .supported_system = supported_system
43
+ self .manufacturer = manufacturer
44
+ self .sensor_name = sensor_name
45
+ self .function = function
46
+ self .protocol = protocol
47
+ self .mqtt_client = mqtt_client
48
+
49
+ self .update_interval = parameters ['update_interval' ]
50
+
51
+ # replace spaces and dashes to underscore !!
52
+ sensor_name_modified = self .sensor_name .replace (' ' , '_' )
53
+ sensor_name_modified = sensor_name_modified .replace ('-' , '_' )
54
+
55
+ self .own_dir = f"{ os .getcwd ()} /sensors/{ self .manufacturer } _{ sensor_name_modified } /"
56
+
57
+ # .get(f"{self.supported_system}_{self.manufacturer}_{sensorNameModified}")
58
+ self .parameters = parameters
59
+ self .logger = logging .getLogger (__name__ )
60
+
61
+ def print_info (self ):
62
+ """ Print info about the used parameters """
63
+ print (f"{ BColors .OKCYAN } -> { self .sensor_name } \t { self .manufacturer } \t { self .function } \t { self .protocol } \t { self .supported_system } { BColors .ENDC } " )
64
+ #self.count = 0
65
+ # for a_sensor in self.parameters:
66
+ # self.count = self.count +1
67
+ #print(f" -> Sensor {self.count}:")
68
+ for an_item in self .parameters .items ():
69
+ print (f" -> { an_item [0 ]} = { an_item [1 ]} " )
70
+ #print(f" -> {an_item}")
71
+ #print(f" -> {a_sensor.items()}")
72
+
73
+ def is_configured (self ):
74
+ """ is configured? """
75
+ return bool (self .parameters )
76
+
77
+ def send_value_over_mqtt (self , mqtt_top_dir_name ):
78
+ """ send the actual sensor value over MQTT"""
79
+ # pass
80
+
81
+ def on_exit (self ):
82
+ """ Do this on exit """
83
+ # pass
84
+
85
+ def get_update_interval (self ):
86
+ """ Get the wanted update interval for this sensor """
87
+ return self .update_interval
0 commit comments