Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimming sensor #99

Merged
merged 8 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/abbfreeathome/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .devices.room_temperature_controller import RoomTemperatureController
from .devices.smoke_detector import SmokeDetector
from .devices.switch_actuator import SwitchActuator
from .devices.switch_sensor import SwitchSensor
from .devices.switch_sensor import DimmingSensor, SwitchSensor
from .devices.temperature_sensor import TemperatureSensor
from .devices.trigger import Trigger
from .devices.wind_sensor import WindSensor
Expand All @@ -44,6 +44,7 @@
Function.FID_DES_DOOR_OPENER_ACTUATOR: DesDoorOpenerActuator,
Function.FID_DES_DOOR_RINGING_SENSOR: DesDoorRingingSensor,
Function.FID_DIMMING_ACTUATOR: DimmingActuator,
Function.FID_DIMMING_SENSOR: DimmingSensor,
Function.FID_FORCE_ON_OFF_SENSOR: ForceOnOffSensor,
Function.FID_HEATING_ACTUATOR: HeatingActuator,
Function.FID_MOVEMENT_DETECTOR: MovementDetector,
Expand Down
14 changes: 14 additions & 0 deletions src/abbfreeathome/devices/switch_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SwitchSensor(Base):
"""Free@Home SwitchSensor Class."""

_state_refresh_output_pairings: list[Pairing] = [
Pairing.AL_RELATIVE_SET_VALUE_CONTROL,
Pairing.AL_SWITCH_ON_OFF,
]

Expand All @@ -29,6 +30,7 @@ def __init__(
) -> None:
"""Initialize the Free@Home SwitchSensor class."""
self._state: bool | None = None
self._longpress: bool | None = None

super().__init__(
device_id,
Expand Down Expand Up @@ -57,4 +59,16 @@ def _refresh_state_from_output(self, output: dict[str, Any]) -> bool:
if output.get("pairingID") == Pairing.AL_SWITCH_ON_OFF.value:
self._state = output.get("value") == "1"
return True
if output.get("pairingID") == Pairing.AL_RELATIVE_SET_VALUE_CONTROL.value:
self._longpress = output.get("value") == "9" or output.get("value") == "8"
return True
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be useful to have a long press up and a long press down?

If this is the possible values.

  • longpress down: 1
  • longpress down release: 0
  • longpress up: 9
  • longpress up release: 8

You could have a state with an enum with the 4 possible options. In Home Assistant they'd appear as one of the 4 options. That way a user can create automations based on one of the 4 options.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try it

return False


class DimmingSensor(SwitchSensor):
"""Free@Home DimmingSensor Class."""

@property
def longpress(self) -> bool | None:
"""Get the longpress value."""
return self._longpress
6 changes: 3 additions & 3 deletions tests/test_freeathome.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ async def test_load_devices(freeathome):
devices = freeathome.get_devices()

# Verify that the devices are loaded correctly
assert len(devices) == 4
assert len(devices) == 5

# Check a single device
device_key = "ABB7F500E17A/ch0003"
Expand All @@ -350,7 +350,7 @@ async def test_load_devices(freeathome):
# Unload a single device and test it's been removed
freeathome.unload_device_by_device_serial(device_serial="ABB7F62F6C0B")
devices = freeathome.get_devices()
assert len(devices) == 2
assert len(devices) == 3


@pytest.mark.asyncio
Expand All @@ -362,7 +362,7 @@ async def test_load_devices_with_orphans(freeathome_orphans):
devices = freeathome_orphans.get_devices()

# Verify that the devices are loaded correctly
assert len(devices) == 6
assert len(devices) == 7

# Check a single orphan device
device_key = "ABB28CBC3651/ch0006"
Expand Down
38 changes: 36 additions & 2 deletions tests/test_switch_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from src.abbfreeathome.api import FreeAtHomeApi
from src.abbfreeathome.devices.switch_sensor import SwitchSensor
from src.abbfreeathome.devices.switch_sensor import DimmingSensor, SwitchSensor


@pytest.fixture
Expand All @@ -20,6 +20,8 @@ def switch_sensor(mock_api):
inputs = {}
outputs = {
"odp0000": {"pairingID": 1, "value": "0"},
"odp0001": {"pairingID": 16, "value": ""},
"odp0006": {"pairingID": 4, "value": ""},
}
parameters = {}

Expand All @@ -35,6 +37,29 @@ def switch_sensor(mock_api):
)


@pytest.fixture
def dimming_sensor(mock_api):
"""Set up the dimming-sensor instance for testing the DimmingSensor device."""
inputs = {}
outputs = {
"odp0000": {"pairingID": 1, "value": "0"},
"odp0001": {"pairingID": 16, "value": ""},
"odp0006": {"pairingID": 4, "value": ""},
}
parameters = {}

return DimmingSensor(
device_id="ABB700D9C0A4",
device_name="Device Name",
channel_id="ch0000",
channel_name="Channel Name",
inputs=inputs,
outputs=outputs,
parameters=parameters,
api=mock_api,
)


@pytest.mark.asyncio
async def test_initial_state(switch_sensor):
"""Test the intial state of the switch-sensor."""
Expand All @@ -54,10 +79,19 @@ async def test_refresh_state(switch_sensor):
)


def test_refresh_state_from_output(switch_sensor):
def test_refresh_state_from_output_switch(switch_sensor):
"""Test the _refresh_state_from_output function."""
# Check output that affects the state.
switch_sensor._refresh_state_from_output(
output={"pairingID": 1, "value": "1"},
)
assert switch_sensor.state is True


def test_refresh_state_from_output_dimming(dimming_sensor):
"""Test the _refresh_state_from_output function."""
# Check output that affects the state.
dimming_sensor._refresh_state_from_output(
output={"pairingID": 16, "value": "9"},
)
assert dimming_sensor.longpress is True
Loading