Skip to content

Commit 45a84fe

Browse files
committed
remove no more existing device/template from cache
1 parent 0a2af26 commit 45a84fe

File tree

8 files changed

+212
-13
lines changed

8 files changed

+212
-13
lines changed

pyfritzhome/cli.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def thermostat_set_window_open(fritz, args):
122122
"""Command that sets the thermostats window state."""
123123
fritz.set_window_open(args.ain, args.timespan)
124124

125+
125126
def thermostat_set_boost_mode(fritz, args):
126127
"""Command that sets the thermostats into boost mode."""
127128
fritz.set_boost_mode(args.ain, args.timespan)
@@ -292,9 +293,7 @@ def main(args=None):
292293
subparser.set_defaults(func=thermostat_set_window_open)
293294

294295
# thermostat boost_mpde
295-
subparser = _sub_switch.add_parser(
296-
"set_boost_mode", help="activate the boost mode"
297-
)
296+
subparser = _sub_switch.add_parser("set_boost_mode", help="activate the boost mode")
298297
subparser.add_argument("ain", type=str, metavar="AIN", help="Actor Identification")
299298
subparser.add_argument(
300299
"timespan",

pyfritzhome/devicetypes/fritzhomedevicethermostat.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def _update_hkr_from_node(self, node):
7171
self.battery_low = self.get_node_value_as_int_as_bool(
7272
hkr_element, "batterylow"
7373
)
74-
self.battery_level = int(self.get_node_value_as_int(hkr_element, "battery"))
74+
self.battery_level = int(
75+
self.get_node_value_as_int(hkr_element, "battery")
76+
)
7577

7678
self.window_open = self.get_node_value_as_int_as_bool(
7779
hkr_element, "windowopenactiv"

pyfritzhome/fritzhome.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,14 @@ def get_prefixed_host(self):
162162
else:
163163
return "http://" + host
164164

165-
def update_devices(self):
165+
def update_devices(self, ignore_removed=True):
166166
"""Update the device."""
167167
_LOGGER.info("Updating Devices ...")
168168
if self._devices is None:
169169
self._devices = {}
170170

171-
for element in self.get_device_elements():
171+
device_elements = self.get_device_elements()
172+
for element in device_elements:
172173
if element.attrib["identifier"] in self._devices.keys():
173174
_LOGGER.info(
174175
"Updating already existing Device " + element.attrib["identifier"]
@@ -178,6 +179,15 @@ def update_devices(self):
178179
_LOGGER.info("Adding new Device " + element.attrib["identifier"])
179180
device = FritzhomeDevice(self, node=element)
180181
self._devices[device.ain] = device
182+
183+
if not ignore_removed:
184+
for identifier in list(self._devices.keys()):
185+
if identifier not in [
186+
element.attrib["identifier"] for element in device_elements
187+
]:
188+
_LOGGER.info("Removing no more existing device " + identifier)
189+
self._devices.pop(identifier)
190+
181191
return True
182192

183193
def _get_listinfo_elements(self, entity_type):
@@ -280,9 +290,7 @@ def set_boost_mode(self, ain, seconds):
280290
"""Set the thermostate to boost mode."""
281291
endtimestamp = int(time.time() + seconds)
282292

283-
self._aha_request(
284-
"sethkrboost", ain=ain, param={"endtimestamp": endtimestamp}
285-
)
293+
self._aha_request("sethkrboost", ain=ain, param={"endtimestamp": endtimestamp})
286294

287295
def get_comfort_temperature(self, ain):
288296
"""Get the thermostate comfort temperature."""
@@ -407,13 +415,14 @@ def has_templates(self):
407415
return False
408416
return True
409417

410-
def update_templates(self):
418+
def update_templates(self, ignore_removed=True):
411419
"""Update the template."""
412420
_LOGGER.info("Updating Templates ...")
413421
if self._templates is None:
414422
self._templates = {}
415423

416-
for element in self.get_template_elements():
424+
template_elements = self.get_template_elements()
425+
for element in template_elements:
417426
if element.attrib["identifier"] in self._templates.keys():
418427
_LOGGER.info(
419428
"Updating already existing Template " + element.attrib["identifier"]
@@ -423,6 +432,15 @@ def update_templates(self):
423432
_LOGGER.info("Adding new Template " + element.attrib["identifier"])
424433
template = FritzhomeTemplate(self, node=element)
425434
self._templates[template.ain] = template
435+
436+
if not ignore_removed:
437+
for identifier in list(self._templates.keys()):
438+
if identifier not in [
439+
element.attrib["identifier"] for element in template_elements
440+
]:
441+
_LOGGER.info("Removing no more existing template " + identifier)
442+
self._templates.pop(identifier)
443+
426444
return True
427445

428446
def get_template_elements(self):
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" ?>
2+
<devicelist version="1">
3+
<device identifier="08761 0000434" id="17" functionbitmask="896" fwversion="03.33" manufacturer="AVM" productname="FRITZ!DECT 200">
4+
<present>1</present>
5+
<name>Steckdose</name>
6+
<switch>
7+
<state>1</state>
8+
<mode>auto</mode>
9+
<lock>0</lock>
10+
<devicelock>0</devicelock>
11+
</switch>
12+
<powermeter>
13+
<power>0</power>
14+
<energy>707</energy>
15+
</powermeter>
16+
<temperature>
17+
<celsius>285</celsius>
18+
<offset>0</offset>
19+
</temperature>
20+
</device>
21+
<device identifier="08761 1048079" id="16" functionbitmask="1280"
22+
fwversion="03.44" manufacturer="AVM" productname="FRITZ!DECT Repeater 100">
23+
<present>1</present>
24+
<name>FRITZ!DECT Rep 100 #1</name>
25+
<temperature>
26+
<celsius>288</celsius>
27+
<offset>0</offset>
28+
</temperature>
29+
</device>
30+
<device identifier="11959 0171328" id="16" functionbitmask="320"
31+
fwversion="03.54" manufacturer="AVM" productname="Comet DECT">
32+
<present>1</present>
33+
<name>Badezimmer</name>
34+
<temperature>
35+
<celsius>205</celsius>
36+
<offset>-15</offset>
37+
</temperature>
38+
<hkr>
39+
<tist>41</tist>
40+
<tsoll>36</tsoll>
41+
<absenk>36</absenk>
42+
<komfort>42</komfort>
43+
<lock>0</lock>
44+
<devicelock>0</devicelock>
45+
<errorcode>0</errorcode>
46+
<batterylow>0</batterylow>
47+
<nextchange>
48+
<endperiod>1508342400</endperiod>
49+
<tchange>42</tchange>
50+
</nextchange>
51+
</hkr>
52+
</device>
53+
<group identifier="65:3A:18-900" id="900" functionbitmask="512" fwversion="1.0" manufacturer="AVM" productname="">
54+
<present>1</present>
55+
<name>Gruppe</name>
56+
<switch>
57+
<state>1</state>
58+
<mode>auto</mode>
59+
<lock/>
60+
<devicelock/>
61+
</switch>
62+
<groupinfo>
63+
<masterdeviceid>0</masterdeviceid>
64+
<members>17</members>
65+
</groupinfo>
66+
</group>
67+
</devicelist>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<templatelist version="1">
2+
<!-- Base Data Tests -->
3+
<template identifier="tmp0B32F7-1B0650682" id="60000" functionbitmask="320" applymask="0">
4+
<name>Base Data</name>
5+
<devices />
6+
<applymask />
7+
</template>
8+
9+
<!-- Device Tests -->
10+
<template identifier="tmp0B32F7-1B0650234" id="60010" functionbitmask="0" applymask="0">
11+
<name>One Device</name>
12+
<devices>
13+
<device identifier="08735 0525249" />
14+
</devices>
15+
<applymask />
16+
</template>
17+
<template identifier="tmp0B32F7-1C40A2B8A" id="60011" functionbitmask="0" applymask="0">
18+
<name>Multiple Devices</name>
19+
<devices>
20+
<device identifier="08735 0316335" />
21+
<device identifier="08735 0525249" />
22+
<device identifier="08735 0526125" />
23+
<device identifier="08735 0340143" />
24+
</devices>
25+
<applymask />
26+
</template>
27+
28+
<!-- Applymask Tests -->
29+
<template identifier="tmp0B32F7-1B064FA20" id="60020" functionbitmask="0" applymask="0">
30+
<name>Apply Heating Summer Mode (Heating off)</name>
31+
<devices />
32+
<applymask>
33+
<hkr_summer />
34+
</applymask>
35+
</template>
36+
<template identifier="tmp0B32F7-1B064FA21" id="60021" functionbitmask="0" applymask="0">
37+
<name>Apply Heating Target Temperature</name>
38+
<devices />
39+
<applymask>
40+
<hkr_temperature />
41+
</applymask>
42+
</template>
43+
<template identifier="tmp0B32F7-1B064FA22" id="60022" functionbitmask="0" applymask="0">
44+
<name>Apply Heating Holiday Mode</name>
45+
<devices />
46+
<applymask>
47+
<hkr_holidays />
48+
</applymask>
49+
</template>
50+
<template identifier="tmp0B32F7-1B064FA23" id="60023" functionbitmask="0" applymask="0">
51+
<name>Apply Heating Time Table</name>
52+
<devices />
53+
<applymask>
54+
<hkr_time_table />
55+
</applymask>
56+
</template>
57+
<template identifier="tmp0B32F7-1B064FA24" id="60024" functionbitmask="0" applymask="0">
58+
<name>Apply Switch/Lamp/Actor manuel on/off Setting</name>
59+
<devices />
60+
<applymask>
61+
<relay_manual />
62+
</applymask>
63+
</template>
64+
<template identifier="tmp0B32F7-1B064FA25" id="60025" functionbitmask="0" applymask="0">
65+
<name>Apply Switch/Lamp/Actor Automatic Time Table</name>
66+
<devices />
67+
<applymask>
68+
<relay_automatic />
69+
</applymask>
70+
</template>
71+
<template identifier="tmp0B32F7-1B064FA26" id="60026" functionbitmask="0" applymask="0">
72+
<name>Apply Lamp/Blind Level</name>
73+
<devices />
74+
<applymask>
75+
<level />
76+
</applymask>
77+
</template>
78+
<template identifier="tmp0B32F7-1B064FA27" id="60027" functionbitmask="0" applymask="0">
79+
<name>Apply Lamp Color</name>
80+
<devices />
81+
<applymask>
82+
<color />
83+
</applymask>
84+
</template>
85+
</templatelist>

tests/test_fritzhome.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ def test_set_boost_mode(self):
234234
self.fritz._request.assert_called_with(
235235
"http://10.0.0.1/webservices/homeautoswitch.lua",
236236
{
237-
"sid": None,
237+
"sid": "0000001",
238238
"ain": "1",
239239
"switchcmd": "sethkrboost",
240240
"endtimestamp": 1000 + 25,
241241
},
242-
)
242+
)

tests/test_fritzhomedevicebase.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ def test_get_device_present(self):
9898
{"ain": "08761 0000434", "switchcmd": "getswitchpresent", "sid": "0000001"},
9999
)
100100

101+
def test_device_removed(self):
102+
self.mock.side_effect = [
103+
Helper.response("base/device_list"),
104+
Helper.response("base/device_list_removed_device"),
105+
Helper.response("base/device_list_removed_device"),
106+
]
107+
108+
self.fritz.update_devices()
109+
assert len(self.fritz.get_devices()) == 5
110+
self.fritz.update_devices()
111+
assert len(self.fritz.get_devices()) == 5
112+
self.fritz.update_devices(ignore_removed=False)
113+
assert len(self.fritz.get_devices()) == 4
114+
101115
def test_device_and_unit_id(self):
102116
device = FritzhomeEntityBase()
103117

tests/test_fritzhometemplate.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ def test_template_init(self):
4040
FritzhomeDeviceFeatures.TEMPERATURE,
4141
]
4242

43+
def test_template_removed(self):
44+
self.mock.side_effect = [
45+
Helper.response("templates/template_list"),
46+
Helper.response("templates/template_list_removed_template"),
47+
Helper.response("templates/template_list_removed_template"),
48+
]
49+
50+
self.fritz.update_templates()
51+
assert len(self.fritz.get_templates()) == 12
52+
self.fritz.update_templates()
53+
assert len(self.fritz.get_templates()) == 12
54+
self.fritz.update_templates(ignore_removed=False)
55+
assert len(self.fritz.get_templates()) == 11
56+
4357
def test_template_with_single_device(self):
4458
template = self.fritz.get_template_by_ain("tmp0B32F7-1B0650234")
4559

0 commit comments

Comments
 (0)