Skip to content

Commit

Permalink
Merge pull request #71 from grimmpp/feature-branch
Browse files Browse the repository at this point in the history
better tests added for binary sensors
  • Loading branch information
grimmpp authored Feb 19, 2024
2 parents 3be0132 + 206ec1d commit f8b95a1
Show file tree
Hide file tree
Showing 14 changed files with 373 additions and 162 deletions.
4 changes: 4 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes and Feature List

## Version 1.3.8
* fixed window handle F6-10-00 in binary sensor
* Added better tests for binary sensors

## Version 1.3.7 Restore Device States after HA Restart
* Trial to remove import warnings
Reported Issue: https://github.com/grimmpp/home-assistant-eltako/issues/61
Expand Down
20 changes: 12 additions & 8 deletions custom_components/eltako/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,12 @@ def value_changed(self, msg: ESP2Message):
return
elif self.dev_eep in [F6_10_00]:
# LOGGER.debug("[Binary Sensor][%s] Received msg for processing eep %s telegram.", b2s(self.dev_id[0]), self.dev_eep.eep_string)
action = (decoded.movement & 0x70) >> 4

if action == 0x07:
self._attr_is_on = False
elif action in (0x04, 0x06):
self._attr_is_on = False
else:
return
# is_on == True => open
self._attr_is_on = decoded.handle_position > 0

if self.invert_signal:
self._attr_is_on = not self._attr_is_on

elif self.dev_eep in [D5_00_01]:
# LOGGER.debug("[Binary Sensor][%s] Received msg for processing eep %s telegram.", b2s(self.dev_id[0]), self.dev_eep.eep_string)
Expand All @@ -212,16 +210,22 @@ def value_changed(self, msg: ESP2Message):

elif self.dev_eep in [A5_08_01]:
# LOGGER.debug("[Binary Sensor][%s] Received msg for processing eep %s telegram.", b2s(self.dev_id[0]), self.dev_eep.eep_string)
if decoded.learn_button == 1:
if decoded.learn_button == 0:
return

self._attr_is_on = decoded.pir_status == 1

if self.invert_signal:
self._attr_is_on = not self._attr_is_on

elif self.dev_eep in [A5_07_01]:
# LOGGER.debug("[Binary Sensor][%s] Received msg for processing eep %s telegram.", b2s(self.dev_id[0]), self.dev_eep.eep_string)

self._attr_is_on = decoded.pir_status_on == 1

if self.invert_signal:
self._attr_is_on = not self._attr_is_on

else:
LOGGER.warn("[Binary Sensor][] eep %s not found for data processing.", b2s(self.dev_id[0]), self.dev_eep.eep_string)
return
Expand Down
2 changes: 1 addition & 1 deletion custom_components/eltako/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def identifier(self) -> str:



def detect() -> [str]:
def detect() -> list[str]:
"""Return a list of candidate paths for USB Eltako gateways.
This method is currently a bit simplistic, it may need to be
Expand Down
4 changes: 2 additions & 2 deletions custom_components/eltako/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "eltako",
"name": "Eltako",
"codeowners": ["@bdurrer", "@grimmpp"],
"codeowners": ["@grimmpp"],
"config_flow": true,
"dependencies": [],
"documentation": "https://github.com/grimmpp/home-assistant-eltako",
Expand All @@ -10,5 +10,5 @@
"issue_tracker": "https://github.com/grimmpp/home-assistant-eltako/issues",
"loggers": ["eltako"],
"requirements": ["eltako14bus==0.0.45","enocean==0.60.1", "StrEnum"],
"version": "1.3.7"
"version": "1.3.8"
}
15 changes: 15 additions & 0 deletions tests/mocks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from typing import Any

from custom_components.eltako.config_helpers import *
Expand Down Expand Up @@ -25,12 +26,23 @@ class HassMock():

def __init__(self) -> None:
self.bus = BusMock()

# def async_create_task(self, async_call):
# asyncio.run( async_call )

class ConfigEntryMock():

def __init__(self):
self.entry_id = "entity_id"

class EltakoBusMock():

def __init__(self):
self._is_active = True

def is_active(self):
return self._is_active

class GatewayMock(EnOceanGateway):

def __init__(self, general_settings:dict=DEFAULT_GENERAL_SETTINGS, dev_id: int=123, base_id:AddressExpression=AddressExpression.parse('FF-AA-80-00')):
Expand All @@ -39,9 +51,12 @@ def __init__(self, general_settings:dict=DEFAULT_GENERAL_SETTINGS, dev_id: int=1

super().__init__(general_settings, hass, dev_id, gw_type, 'SERIAL_PATH', 56700, base_id, "MyFAM14", ConfigEntryMock())

self._bus = EltakoBusMock()

def set_status_changed_handler(self):
pass


class LatestStateMock():
def __init__(self, state:str=None, attributes:dict[str:str]={}):
self.state = state
Expand Down
151 changes: 0 additions & 151 deletions tests/test_binary_sensor.py

This file was deleted.

33 changes: 33 additions & 0 deletions tests/test_binary_sensor_A5_07_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from mocks import *
from unittest import mock
from homeassistant.helpers.entity import Entity
from homeassistant.const import Platform
from custom_components.eltako.binary_sensor import EltakoBinarySensor
from custom_components.eltako.config_helpers import *
from eltakobus import *
from eltakobus.eep import *

from tests.test_binary_sensor_generic import TestBinarySensor

# mock update of Home Assistant
Entity.schedule_update_ha_state = mock.Mock(return_value=None)
# EltakoBinarySensor.hass.bus.fire is mocked by class HassMock


class TestBinarySensor_A5_07_01(unittest.TestCase):

def test_occupancy_sensor(self):
bs = TestBinarySensor().create_binary_sensor(eep_string="A5-07-01")

self.assertEqual(bs._attr_is_on, None)

msg = Regular4BSMessage(address=b'\x00\x00\x10\x08', data=b'\x00\x96\xC8\x09', status=b'\x00')

bs.value_changed(msg)
self.assertEqual(bs._attr_is_on, True)

msg.data = b'\x00\x96\x0A\x09'
bs.value_changed(msg)
self.assertEqual(bs._attr_is_on, False)

20 changes: 20 additions & 0 deletions tests/test_binary_sensor_A5_08_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest
from mocks import *
from unittest import mock
from homeassistant.helpers.entity import Entity
from homeassistant.const import Platform
from custom_components.eltako.binary_sensor import EltakoBinarySensor
from custom_components.eltako.config_helpers import *
from eltakobus import *
from eltakobus.eep import *

from tests.test_binary_sensor_generic import TestBinarySensor

# mock update of Home Assistant
Entity.schedule_update_ha_state = mock.Mock(return_value=None)
# EltakoBinarySensor.hass.bus.fire is mocked by class HassMock


class TestBinarySensor_A5_08_01(unittest.TestCase):
"""Test missing"""

53 changes: 53 additions & 0 deletions tests/test_binary_sensor_D5_00_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import unittest
from mocks import *
from unittest import mock
from homeassistant.helpers.entity import Entity
from homeassistant.const import Platform
from custom_components.eltako.binary_sensor import EltakoBinarySensor
from custom_components.eltako.config_helpers import *
from eltakobus import *
from eltakobus.eep import *

from tests.test_binary_sensor_generic import TestBinarySensor

# mock update of Home Assistant
Entity.schedule_update_ha_state = mock.Mock(return_value=None)
# EltakoBinarySensor.hass.bus.fire is mocked by class HassMock


class TestBinarySensor_D5_00_01(unittest.TestCase):

def test_binary_sensor_window_contact_triggered_via_FTS14EM(self):
bs = TestBinarySensor().create_binary_sensor(eep_string="D5-00-01", device_class = "window", invert_signal = False)

# test if sensor object is newly created
self.assertEqual(bs._attr_is_on, None)

# test if state is set to no contact
bs._attr_is_on = False
self.assertEqual(bs._attr_is_on, False)

msg = Regular1BSMessage(address=b'\x00\x00\x10\x08',
data=b'\x09', #open
status=b'\x00')

# test if signal is processed correctly (switch on)
bs.value_changed(msg)
self.assertEqual(bs._attr_is_on, False)

# test if signal is processed correctly (switch on)
bs.invert_signal = True
bs.value_changed(msg)
self.assertEqual(bs._attr_is_on, True)

# test if signal is processed correctly (switch off)
msg.data = b'\x08' # closed
bs.invert_signal = False
bs.value_changed(msg)
self.assertEqual(bs._attr_is_on, True)

# test if signal is processed correctly (switch off)
bs.invert_signal = True
bs.value_changed(msg)
self.assertEqual(bs._attr_is_on, False)

Loading

0 comments on commit f8b95a1

Please sign in to comment.