Skip to content

Commit

Permalink
Reduce patch surface to only required functionality for humuditysensor
Browse files Browse the repository at this point in the history
Limit the patch surface to only code that is needed to read
HumiditySensor data from Fritz DECT 440.

jayme-github/fritzbox_smarthome_exporter#9
  • Loading branch information
jayme-github committed Feb 6, 2022
1 parent d1cd52c commit d280392
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 19 deletions.
22 changes: 3 additions & 19 deletions fritz/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package fritz
type Capability int

// Known (specified) device capabilities.
// see https://avm.de/fileadmin/user_upload/Global/Service/Schnittstellen/AHA-HTTP-Interface.pdf section 3.2 for full list
const (
HANFUNCompatibility Capability = iota
_
Expand All @@ -21,9 +20,9 @@ const (
_
HANFUNUnit
_
SwitchableDevice
DimmableDevice
ColorSettableDevice
_
_
_
_
_
HumiditySensor
Expand Down Expand Up @@ -102,21 +101,6 @@ func (d *Device) HasHANFUNUnit() bool {
return d.Has(HANFUNUnit)
}

// IsSwitchableDevice returns true if the device is a switchable device/power plug/actor.
func (d *Device) IsSwitchableDevice() bool {
return d.Has(SwitchableDevice)
}

// CanBeDimmed returns true if the device can be dimmed somehow (e.g. light intensity, height level, etc.).
func (d *Device) CanBeDimmed() bool {
return d.Has(DimmableDevice)
}

// CanSetColors returns true if the device can set colors.
func (d *Device) CanSetColors() bool {
return d.Has(ColorSettableDevice)
}

// CanMeasureHumidity returns true if the device has humidity functionality. Returns false otherwise.
func (d *Device) CanMeasureHumidity() bool {
return d.Has(HumiditySensor)
Expand Down
2 changes: 2 additions & 0 deletions fritz/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func TestParsingFunctionBitMask(t *testing.T) {
{name: "320 has no microphone", mask: "320", fct: (*Device).HasMicrophone, expect: false},
{name: "320 has no hanfun unit", mask: "320", fct: (*Device).HasHANFUNUnit, expect: false},
{name: "320 does not speak hanfun protocol", mask: "320", fct: (*Device).IsHANFUNCompatible, expect: false},
{name: "320 can not measure humidity", mask: "320", fct: (*Device).CanMeasureHumidity, expect: false},
{name: "320 is not AVM button", mask: "320", fct: (*Device).IsAVMButton, expect: false},
{name: "1048864 is an AVM button", mask: "1048864", fct: (*Device).IsAVMButton, expect: true},
{name: "1048864 can measure humidity", mask: "1048864", fct: (*Device).CanMeasureHumidity, expect: true},
} {
Expand Down
7 changes: 7 additions & 0 deletions fritz/devicelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ func (l *Devicelist) Buttons() []Device {
})
}

// AVMButtons returns the devices which satisfy IsAVMButton.
func (l *Devicelist) AVMButtons() []Device {
return l.filter(func(d Device) bool {
return d.IsAVMButton()
})
}

func (l *Devicelist) filter(predicate func(Device) bool) []Device {
var filtered []Device
for _, d := range l.Devices {
Expand Down
1 change: 1 addition & 0 deletions fritz/devicelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestDeviceFiltering(t *testing.T) {
{name: "on buttons", xmlfile: "../testdata/devicelist_fritzos06.83.xml", filter: func(d Devicelist) []Device { return d.Buttons() }, expect: 0},
{name: "four thermostats (Issue #56)", xmlfile: "../testdata/devicelist_issue_59.xml", filter: func(d Devicelist) []Device { return d.Thermostats() }, expect: 4},
{name: "eight switches (Issue #56)", xmlfile: "../testdata/devicelist_issue_59.xml", filter: func(d Devicelist) []Device { return d.Switches() }, expect: 8},
{name: "two avm buttons", xmlfile: "../testdata/devicelist_fritzos07.29.xml", filter: func(d Devicelist) []Device { return d.AVMButtons() }, expect: 2},
} {
t.Run(tc.name, func(t *testing.T) {
list := mustUnmarshall(t, tc.xmlfile)
Expand Down
236 changes: 236 additions & 0 deletions testdata/devicelist_fritzos07.29.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
<devicelist version="1" fwversion="7.29">
<device identifier="08761 0000001" id="16" functionbitmask="35712" fwversion="04.16" manufacturer="AVM" productname="FRITZ!DECT 200">
<present>1</present>
<txbusy>0</txbusy>
<name>SWITCH_1</name>
<switch>
<state>0</state>
<mode>auto</mode>
<lock>0</lock>
<devicelock>0</devicelock>
</switch>
<simpleonoff>
<state>0</state>
</simpleonoff>
<powermeter>
<voltage>231090</voltage>
<power>0</power>
<energy>369382</energy>
</powermeter>
<temperature>
<celsius>165</celsius>
<offset>0</offset>
</temperature>
</device>
<device identifier="11960 0000002" id="21" functionbitmask="320" fwversion="03.54" manufacturer="AVM" productname="Comet DECT">
<present>1</present>
<txbusy>0</txbusy>
<name>HKR_1</name>
<battery>60</battery>
<batterylow>0</batterylow>
<temperature>
<celsius>195</celsius>
<offset>0</offset>
</temperature>
<hkr>
<tist>39</tist>
<tsoll>32</tsoll>
<absenk>32</absenk>
<komfort>38</komfort>
<lock>0</lock>
<devicelock>0</devicelock>
<errorcode>0</errorcode>
<windowopenactiv>0</windowopenactiv>
<windowopenactiveendtime>0</windowopenactiveendtime>
<boostactive>0</boostactive>
<boostactiveendtime>0</boostactiveendtime>
<batterylow>0</batterylow>
<battery>60</battery>
<nextchange>
<endperiod>1644177600</endperiod>
<tchange>38</tchange>
</nextchange>
<summeractive>0</summeractive>
<holidayactive>0</holidayactive>
</hkr>
</device>
<device identifier="11795 0000003" id="22" functionbitmask="320" fwversion="03.68" manufacturer="AVM" productname="Comet DECT">
<present>1</present>
<txbusy>0</txbusy>
<name>HKR_2</name>
<battery>70</battery>
<batterylow>0</batterylow>
<temperature>
<celsius>145</celsius>
<offset>-5</offset>
</temperature>
<hkr>
<tist>29</tist>
<tsoll>253</tsoll>
<absenk>32</absenk>
<komfort>40</komfort>
<lock>0</lock>
<devicelock>0</devicelock>
<errorcode>0</errorcode>
<windowopenactiv>0</windowopenactiv>
<windowopenactiveendtime>0</windowopenactiveendtime>
<boostactive>0</boostactive>
<boostactiveendtime>0</boostactiveendtime>
<batterylow>0</batterylow>
<battery>70</battery>
<nextchange>
<endperiod>1644177600</endperiod>
<tchange>40</tchange>
</nextchange>
<summeractive>0</summeractive>
<holidayactive>0</holidayactive>
</hkr>
</device>
<device identifier="11795 0000004" id="23" functionbitmask="320" fwversion="03.68" manufacturer="AVM" productname="Comet DECT">
<present>1</present>
<txbusy>0</txbusy>
<name>HKR_3</name>
<battery>10</battery>
<batterylow>0</batterylow>
<temperature>
<celsius>130</celsius>
<offset>-20</offset>
</temperature>
<hkr>
<tist>26</tist>
<tsoll>253</tsoll>
<absenk>32</absenk>
<komfort>37</komfort>
<lock>0</lock>
<devicelock>0</devicelock>
<errorcode>0</errorcode>
<windowopenactiv>0</windowopenactiv>
<windowopenactiveendtime>0</windowopenactiveendtime>
<boostactive>0</boostactive>
<boostactiveendtime>0</boostactiveendtime>
<batterylow>0</batterylow>
<battery>10</battery>
<nextchange>
<endperiod>1644213600</endperiod>
<tchange>37</tchange>
</nextchange>
<summeractive>0</summeractive>
<holidayactive>0</holidayactive>
</hkr>
</device>
<device identifier="11795 0000005" id="24" functionbitmask="320" fwversion="03.68" manufacturer="AVM" productname="Comet DECT">
<present>1</present>
<txbusy>0</txbusy>
<name>HKR_4</name>
<battery>1</battery>
<batterylow>0</batterylow>
<temperature>
<celsius>180</celsius>
<offset>-10</offset>
</temperature>
<hkr>
<tist>36</tist>
<tsoll>34</tsoll>
<absenk>32</absenk>
<komfort>34</komfort>
<lock>0</lock>
<devicelock>0</devicelock>
<errorcode>0</errorcode>
<windowopenactiv>1</windowopenactiv>
<windowopenactiveendtime>0</windowopenactiveendtime>
<boostactive>0</boostactive>
<boostactiveendtime>0</boostactiveendtime>
<batterylow>0</batterylow>
<battery>1</battery>
<nextchange>
<endperiod>1644174000</endperiod>
<tchange>32</tchange>
</nextchange>
<summeractive>0</summeractive>
<holidayactive>0</holidayactive>
</hkr>
</device>
<device identifier="11657 0000006" id="25" functionbitmask="35712" fwversion="04.17" manufacturer="AVM" productname="FRITZ!DECT 210">
<present>1</present>
<txbusy>0</txbusy>
<name>SWITCH_2</name>
<switch>
<state>1</state>
<mode>manuell</mode>
<lock>0</lock>
<devicelock>1</devicelock>
</switch>
<simpleonoff>
<state>1</state>
</simpleonoff>
<powermeter>
<voltage>230602</voltage>
<power>0</power>
<energy>46752</energy>
</powermeter>
<temperature>
<celsius>125</celsius>
<offset>0</offset>
</temperature>
</device>
<device identifier="FIZZ BUZZ" id="26" functionbitmask="1048864" fwversion="05.21" manufacturer="AVM" productname="FRITZ!DECT 440">
<present>1</present>
<txbusy>0</txbusy>
<name>BUTTON_1</name>
<battery>100</battery>
<batterylow>0</batterylow>
<temperature>
<celsius>175</celsius>
<offset>0</offset>
</temperature>
<humidity>
<rel_humidity>54</rel_humidity>
</humidity>
<button identifier="FIZZ BUZZ-1" id="5008">
<name>Taster Bad: Oben rechts</name>
<lastpressedtimestamp>1643533662</lastpressedtimestamp>
</button>
<button identifier="FIZZ BUZZ-3" id="5009">
<name>Taster Bad: Unten rechts</name>
<lastpressedtimestamp></lastpressedtimestamp>
</button>
<button identifier="FIZZ BUZZ-5" id="5010">
<name>Taster Bad: Unten links</name>
<lastpressedtimestamp></lastpressedtimestamp>
</button>
<button identifier="FIZZ BUZZ-7" id="5011">
<name>Taster Bad: Oben links</name>
<lastpressedtimestamp></lastpressedtimestamp>
</button>
</device>
<device identifier="FOOFAA" id="27" functionbitmask="1048864" fwversion="05.21" manufacturer="AVM" productname="FRITZ!DECT 440">
<present>1</present>
<txbusy>0</txbusy>
<name>BUTTON_2</name>
<battery>100</battery>
<batterylow>0</batterylow>
<temperature>
<celsius>200</celsius>
<offset>0</offset>
</temperature>
<humidity>
<rel_humidity>38</rel_humidity>
</humidity>
<button identifier="FOOFAA-1" id="5000">
<name>Taster Wohnzimmer: Oben rechts</name>
<lastpressedtimestamp>1643351645</lastpressedtimestamp>
</button>
<button identifier="FOOFAA-3" id="5001">
<name>Taster Wohnzimmer: Unten rechts</name>
<lastpressedtimestamp>1642844240</lastpressedtimestamp>
</button>
<button identifier="FOOFAA-5" id="5002">
<name>Taster Wohnzimmer: Unten links</name>
<lastpressedtimestamp>1642791540</lastpressedtimestamp>
</button>
<button identifier="FOOFAA-7" id="5003">
<name>Taster Wohnzimmer: Oben links</name>
<lastpressedtimestamp>1642763963</lastpressedtimestamp>
</button>
</device>
</devicelist>

0 comments on commit d280392

Please sign in to comment.