Skip to content

Commit 6d162a8

Browse files
committed
refactor(instance): convert heating to true property
1 parent ffc4c8b commit 6d162a8

2 files changed

Lines changed: 28 additions & 71 deletions

File tree

tests/unit/tion.py

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
1-
import sys
2-
import unittest
3-
import unittest.mock as mock
4-
from unittest.mock import MagicMock
5-
sys.modules['bluepy'] = MagicMock()
6-
sys.modules['bluepy.btle'] = MagicMock()
7-
import tion_btle.tion # noqa: E402
1+
import pytest
82

3+
from tion_btle.tion import tion as Tion
94

10-
class TionTests(unittest.TestCase):
11-
def setUp(self):
12-
self.patch = mock.patch('tion_btle.tion.tion.__init__', return_value=None)
13-
self.patch.start()
14-
self._tion = tion_btle.tion.tion("")
155

16-
def tearDown(self):
17-
self.patch.stop()
6+
@pytest.mark.parametrize(
7+
"heater, heater_temp, in_temp, out_temp, result",
8+
[
9+
["on", 20, -2, 21, "on"],
10+
["on", 20, 15, 21, "on"],
11+
["on", 16, 15, 21, "off"],
12+
["off", 20, 15, 21, "off"],
13+
])
14+
def test__detect_heating_state(heater, in_temp, out_temp, heater_temp, result):
15+
"""Test heating detection"""
1816

19-
def test___detect_heating_state(self):
20-
# state, in, out, target, heater
21-
_variants = [
22-
["on", -2, 21, 20, "on"],
23-
["on", 15, 21, 20, "on"],
24-
["off", 15, 21, 16, "on"],
25-
["off", 15, 21, 20, "off"],
26-
]
17+
tion = Tion(mac="")
18+
tion.heater = heater
19+
tion._in_temp = in_temp
20+
tion._out_temp = out_temp
21+
tion._heater_temp = heater_temp
2722

28-
for expect, in_temp, out_temp, heater_temp, heater in _variants:
29-
with self.subTest(expect=expect,
30-
in_temp=in_temp, out_temp=out_temp, heater_temp=heater_temp, heater=heater):
31-
# call private __detect_heating_state from tion class
32-
self._tion._tion__detect_heating_state(in_temp, out_temp, heater_temp, heater)
33-
self.assertEqual(self._tion.heating, expect)
34-
35-
36-
if __name__ == '__main__':
37-
unittest.main()
23+
assert tion.heating == result

tion_btle/tion.py

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def __init__(self, mac: str):
131131
self._state: bool = False
132132
self._heater: bool = False
133133
self._sound: bool = False
134-
self._heating: bool = False
135134
self._filter_remain: float = 0.0
136135
self._error_code: int = 0
137136
self.__failed_connects: int = 0
@@ -209,35 +208,16 @@ def __generate_common_json(self) -> dict:
209208
"model": self.model,
210209
}
211210

212-
def __detect_heating_state(self,
213-
in_temp: int = None,
214-
out_temp: int = None,
215-
heater_temp: int = None,
216-
heater: str = None) -> None:
217-
"""
218-
Tries to guess is heater working right now
219-
:param in_temp: air intake temperature
220-
:param out_temp: ait outtake temperature
221-
:param heater_temp: target temperature for heater
222-
:param heater: heater state
223-
:return: None
224-
"""
225-
if in_temp is None:
226-
in_temp = self.in_temp
227-
if out_temp is None:
228-
out_temp = self.out_temp
229-
if heater_temp is None:
230-
heater_temp = self.heater_temp
231-
if heater is None:
232-
heater = self.heater
233-
234-
if heater == "off":
235-
self.heating = "off"
236-
else:
237-
if heater_temp - in_temp > 3 and out_temp > in_temp:
238-
self.heating = "on"
239-
else:
240-
self.heating = "off"
211+
@property
212+
def heating(self) -> str:
213+
"""Tries to guess is heater working right now."""
214+
if self.heater == "off":
215+
return "off"
216+
217+
if self.heater_temp - self.in_temp > 3 and self.out_temp > self.in_temp:
218+
return "on"
219+
220+
return "off"
241221

242222
def get_state_from_breezer(self, keep_connection: bool = False) -> None:
243223
"""
@@ -257,7 +237,6 @@ def get_state_from_breezer(self, keep_connection: bool = False) -> None:
257237
self.__connections_count -= 1
258238

259239
self._decode_response(response)
260-
self.__detect_heating_state()
261240

262241
def get(self, keep_connection: bool = False, skip_update: bool = False) -> dict:
263242
"""
@@ -509,14 +488,6 @@ def sound(self, new_state: str):
509488
def filter_remain(self) -> float:
510489
return self._filter_remain
511490

512-
@property
513-
def heating(self) -> str:
514-
return self._decode_state(self._heating)
515-
516-
@heating.setter
517-
def heating(self, new_state: str):
518-
self._heating = self._encode_state(new_state)
519-
520491
@property
521492
def mode(self):
522493
return self._process_mode(self._mode)

0 commit comments

Comments
 (0)