Skip to content

Commit

Permalink
Add events for connection state
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-th committed Jun 22, 2024
1 parent a0d8443 commit 7d70ad3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/homematicip/events/event_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ class ModelUpdateEvent(Enum):
ITEM_REMOVED = auto()
ITEM_UPDATED = auto()
ITEM_CREATED = auto()
HOME_CONNECTED = auto()
HOME_DISCONNECTED = auto()
23 changes: 23 additions & 0 deletions src/homematicip/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from homematicip.connection.websocket_handler import WebSocketHandler
from homematicip.events.event_manager import EventManager
from homematicip.events.event_types import ModelUpdateEvent
from homematicip.events.hmip_event_handler import HmipEventHandler
from homematicip.model.model import Model, build_model_from_json

Expand Down Expand Up @@ -135,11 +136,33 @@ async def _async_start_listening_for_updates(self, context: ConnectionContext):

@property
def websocket_connected(self):
"""Current state of the websocket connection. An event will be published if the state changes."""
return self._websocket_connected

@property
def home_is_connected(self) -> bool:
"""Return if the home is connected."""
return self.model.home.connected if self.model is not None else False

def _set_websocket_connected_state(self, value):
"""Set the websocket connected state and publish the event.
:param value: The new state of the websocket connection."""
state_changed = self._websocket_connected != value
self._websocket_connected = value

if state_changed:
self._publish_websocket_connected(value)

def _publish_websocket_connected(self, connected_value: bool):
"""Publish the websocket connected event.
:param connected_value: The new state of the websocket connection."""
if connected_value:
self.event_manager.publish(ModelUpdateEvent.HOME_CONNECTED, connected_value)
else:
self.event_manager.publish(ModelUpdateEvent.HOME_DISCONNECTED, connected_value)

@property
def rest_connection(self):
return self._rest_connection
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
from homematicip.configuration.config import PersistentConfig, Config
from homematicip.connection.rest_connection import ConnectionContext
from homematicip.model.model import build_model_from_json, Model


@pytest.fixture
Expand Down Expand Up @@ -41,3 +42,8 @@ def connection_context(config: PersistentConfig):
def sample_data_complete(path_to_sample_home_json):
f = open(path_to_sample_home_json, "r")
return json.load(f)


@pytest.fixture
def filled_model(sample_data_complete) -> Model:
return build_model_from_json(sample_data_complete)
8 changes: 0 additions & 8 deletions tests/model/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
import pytest

from homematicip.model.model import build_model_from_json, Model


@pytest.fixture
def filled_model(sample_data_complete) -> Model:
return build_model_from_json(sample_data_complete)
34 changes: 34 additions & 0 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from homematicip.configuration.config import PersistentConfig
from homematicip.connection.rest_connection import RestConnection, RestResult
from homematicip.events.event_types import ModelUpdateEvent
from homematicip.runner import Runner


Expand Down Expand Up @@ -56,3 +57,36 @@ def test_runner_websocket_connected_callable():
runner._set_websocket_connected_state(True)
assert runner.websocket_connected is True
assert runner._websocket_connected is True


def test_runner_websocket_connected_event_is_raised(mocker):
"""Test that the HOME_CONNECTED event is raised when the websocket connection is established."""
mock_publish = mocker.Mock()
runner = Runner()
runner.event_manager.publish = mock_publish
runner._websocket_connected = False

runner._set_websocket_connected_state(True)
mock_publish.assert_called_with(ModelUpdateEvent.HOME_CONNECTED, True)


def test_runner_websocket_disconnected_event_is_raised(mocker):
"""Test that the HOME_DISCONNECTED event is raised when the websocket connection is lost."""
mock_publish = mocker.Mock()
runner = Runner()
runner.event_manager.publish = mock_publish
runner._websocket_connected = True

runner._set_websocket_connected_state(False)
mock_publish.assert_called_with(ModelUpdateEvent.HOME_DISCONNECTED, False)


def test_runner_home_is_connected_property(mocker, filled_model):
"""Test that the home_is_connected property returns the correct value."""
runner = Runner(model=filled_model)
current_connection_state = filled_model.home.connected

assert runner.home_is_connected is current_connection_state

runner.model.home.connected = not current_connection_state
assert runner.home_is_connected is not current_connection_state

0 comments on commit 7d70ad3

Please sign in to comment.