forked from kbialek/deye-inverter-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deye_sensor_test.py
127 lines (94 loc) · 3.46 KB
/
deye_sensor_test.py
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
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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import unittest
from unittest.mock import patch
from deye_sensor import Sensor, ComputedSumSensor, DoubleRegisterSensor, SingleRegisterSensor
class TestSensor(Sensor):
def __init__(self, name, value):
super().__init__(name, groups=['string'])
self.value = value
def read_value(self, registers):
return self.value
class DeyeSensorTest(unittest.TestCase):
def test_sum_sensor_returns_sum_when_all_inputs_are_given(self):
# given
test_sensor1 = TestSensor("ts1", 1.1)
test_sensor2 = TestSensor("ts2", 2.2)
# and
sut = ComputedSumSensor('sum', [test_sensor1, test_sensor2], groups=['string'])
# when
result = sut.read_value([])
# then
self.assertAlmostEqual(result, 3.3)
def test_sum_sensor_returns_none_when_any_input_is_none(self):
# given
test_sensor1 = TestSensor("ts1", 1.1)
test_sensor2 = TestSensor("ts2", None)
# and
sut = ComputedSumSensor('sum', [test_sensor1, test_sensor2], groups=['string'])
# when
result = sut.read_value([])
# then
self.assertIsNone(result)
def test_single_reg_sensor_unsigned(self):
# given
sut = SingleRegisterSensor('test', 0x00, 1, signed=False, groups=['string'])
# and
registers = {
0: bytearray.fromhex('0102')
}
# when
result = sut.read_value(registers)
# then
self.assertEqual(result, 0x0102)
def test_dobule_reg_sensor_signed(self):
# given
sut = SingleRegisterSensor('test', 0x00, 1, signed=True, groups=['string'])
# and
registers = {
0: bytearray.fromhex('fffe')
}
# when
result = sut.read_value(registers)
# then
self.assertEqual(result, -2)
def test_dobule_reg_sensor_unsigned(self):
# given
sut = DoubleRegisterSensor('test', 0x00, 1, signed=False, groups=['string'])
# and
registers = {
0: bytearray.fromhex('0102'), # low word
1: bytearray.fromhex('0304') # high word
}
# when
result = sut.read_value(registers)
# then
self.assertEqual(result, 0x03040102)
def test_dobule_reg_sensor_signed(self):
# given
sut = DoubleRegisterSensor('test', 0x00, 1, signed=True, groups=['string'])
# and
registers = {
0: bytearray.fromhex('fffe'), # low word
1: bytearray.fromhex('ffff') # high word
}
# when
result = sut.read_value(registers)
# then
self.assertEqual(result, -2)
if __name__ == '__main__':
unittest.main()