forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup August activity processing and add tests (home-assistant#31774)
* Update py-august to 0.12.0 * Upstream update also resolves issue home-assistant#28960
- Loading branch information
Showing
10 changed files
with
378 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
"""Mocks for the august component.""" | ||
import datetime | ||
from unittest.mock import MagicMock, PropertyMock | ||
|
||
from august.activity import Activity | ||
|
||
from homeassistant.components.august import AugustData | ||
from homeassistant.util import dt | ||
|
||
|
||
class MockActivity(Activity): | ||
"""A mock for py-august Activity class.""" | ||
|
||
def __init__( | ||
self, action=None, activity_start_timestamp=None, activity_end_timestamp=None | ||
): | ||
"""Init the py-august Activity class mock.""" | ||
self._action = action | ||
self._activity_start_timestamp = activity_start_timestamp | ||
self._activity_end_timestamp = activity_end_timestamp | ||
|
||
@property | ||
def activity_start_time(self): | ||
"""Mock the time activity started.""" | ||
return datetime.datetime.fromtimestamp(self._activity_start_timestamp) | ||
|
||
@property | ||
def activity_end_time(self): | ||
"""Mock the time activity ended.""" | ||
return datetime.datetime.fromtimestamp(self._activity_end_timestamp) | ||
|
||
@property | ||
def action(self): | ||
"""Mock the action.""" | ||
return self._action | ||
|
||
|
||
class MockAugustData(AugustData): | ||
"""A wrapper to mock AugustData.""" | ||
|
||
# AugustData support multiple locks, however for the purposes of | ||
# mocking we currently only mock one lockid | ||
|
||
def __init__( | ||
self, last_lock_status_update_timestamp=1, last_door_state_update_timestamp=1 | ||
): | ||
"""Mock AugustData.""" | ||
self._last_lock_status_update_time_utc = dt.as_utc( | ||
datetime.datetime.fromtimestamp(last_lock_status_update_timestamp) | ||
) | ||
self._last_door_state_update_time_utc = dt.as_utc( | ||
datetime.datetime.fromtimestamp(last_lock_status_update_timestamp) | ||
) | ||
|
||
def get_last_lock_status_update_time_utc(self, device_id): | ||
"""Mock to get last lock status update time.""" | ||
return self._last_lock_status_update_time_utc | ||
|
||
def set_last_lock_status_update_time_utc(self, device_id, update_time): | ||
"""Mock to set last lock status update time.""" | ||
self._last_lock_status_update_time_utc = update_time | ||
|
||
def get_last_door_state_update_time_utc(self, device_id): | ||
"""Mock to get last door state update time.""" | ||
return self._last_door_state_update_time_utc | ||
|
||
def set_last_door_state_update_time_utc(self, device_id, update_time): | ||
"""Mock to set last door state update time.""" | ||
self._last_door_state_update_time_utc = update_time | ||
|
||
|
||
def _mock_august_lock(): | ||
lock = MagicMock(name="august.lock") | ||
type(lock).device_id = PropertyMock(return_value="lock_device_id_1") | ||
return lock | ||
|
||
|
||
def _mock_august_authenticator(): | ||
authenticator = MagicMock(name="august.authenticator") | ||
authenticator.should_refresh = MagicMock( | ||
name="august.authenticator.should_refresh", return_value=0 | ||
) | ||
authenticator.refresh_access_token = MagicMock( | ||
name="august.authenticator.refresh_access_token" | ||
) | ||
return authenticator | ||
|
||
|
||
def _mock_august_authentication(token_text, token_timestamp): | ||
authentication = MagicMock(name="august.authentication") | ||
type(authentication).access_token = PropertyMock(return_value=token_text) | ||
type(authentication).access_token_expires = PropertyMock( | ||
return_value=token_timestamp | ||
) | ||
return authentication |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
"""The lock tests for the august platform.""" | ||
|
||
import datetime | ||
from unittest.mock import MagicMock | ||
|
||
from august.activity import ACTION_DOOR_CLOSED, ACTION_DOOR_OPEN | ||
from august.lock import LockDoorStatus | ||
|
||
from homeassistant.components.august.binary_sensor import AugustDoorBinarySensor | ||
from homeassistant.util import dt | ||
|
||
from tests.components.august.mocks import ( | ||
MockActivity, | ||
MockAugustData, | ||
_mock_august_lock, | ||
) | ||
|
||
|
||
class MockAugustDoorBinarySensor(AugustDoorBinarySensor): | ||
"""A mock for august component AugustLock class.""" | ||
|
||
def __init__(self, august_data=None): | ||
"""Init the mock for august component AugustLock class.""" | ||
self._data = august_data | ||
self._door = _mock_august_lock() | ||
|
||
@property | ||
def name(self): | ||
"""Mock name.""" | ||
return "mockedname1" | ||
|
||
@property | ||
def device_id(self): | ||
"""Mock device_id.""" | ||
return "mockdeviceid1" | ||
|
||
def _update_door_state(self, door_state, activity_start_time_utc): | ||
"""Mock updating the lock status.""" | ||
self._data.set_last_door_state_update_time_utc( | ||
self._door.device_id, activity_start_time_utc | ||
) | ||
self.last_update_door_state = {} | ||
self.last_update_door_state["door_state"] = door_state | ||
self.last_update_door_state["activity_start_time_utc"] = activity_start_time_utc | ||
return MagicMock() | ||
|
||
|
||
def test__sync_door_activity_doored_via_dooropen(): | ||
"""Test _sync_door_activity dooropen.""" | ||
data = MockAugustData(last_door_state_update_timestamp=1) | ||
door = MockAugustDoorBinarySensor(august_data=data) | ||
door_activity_start_timestamp = 1234 | ||
door_activity = MockActivity( | ||
action=ACTION_DOOR_OPEN, | ||
activity_start_timestamp=door_activity_start_timestamp, | ||
activity_end_timestamp=5678, | ||
) | ||
door._sync_door_activity(door_activity) | ||
assert door.last_update_door_state["door_state"] == LockDoorStatus.OPEN | ||
assert door.last_update_door_state["activity_start_time_utc"] == dt.as_utc( | ||
datetime.datetime.fromtimestamp(door_activity_start_timestamp) | ||
) | ||
|
||
|
||
def test__sync_door_activity_doorclosed(): | ||
"""Test _sync_door_activity doorclosed.""" | ||
data = MockAugustData(last_door_state_update_timestamp=1) | ||
door = MockAugustDoorBinarySensor(august_data=data) | ||
door_activity_timestamp = 1234 | ||
door_activity = MockActivity( | ||
action=ACTION_DOOR_CLOSED, | ||
activity_start_timestamp=door_activity_timestamp, | ||
activity_end_timestamp=door_activity_timestamp, | ||
) | ||
door._sync_door_activity(door_activity) | ||
assert door.last_update_door_state["door_state"] == LockDoorStatus.CLOSED | ||
assert door.last_update_door_state["activity_start_time_utc"] == dt.as_utc( | ||
datetime.datetime.fromtimestamp(door_activity_timestamp) | ||
) | ||
|
||
|
||
def test__sync_door_activity_ignores_old_data(): | ||
"""Test _sync_door_activity dooropen then expired doorclosed.""" | ||
data = MockAugustData(last_door_state_update_timestamp=1) | ||
door = MockAugustDoorBinarySensor(august_data=data) | ||
first_door_activity_timestamp = 1234 | ||
door_activity = MockActivity( | ||
action=ACTION_DOOR_OPEN, | ||
activity_start_timestamp=first_door_activity_timestamp, | ||
activity_end_timestamp=first_door_activity_timestamp, | ||
) | ||
door._sync_door_activity(door_activity) | ||
assert door.last_update_door_state["door_state"] == LockDoorStatus.OPEN | ||
assert door.last_update_door_state["activity_start_time_utc"] == dt.as_utc( | ||
datetime.datetime.fromtimestamp(first_door_activity_timestamp) | ||
) | ||
|
||
# Now we do the update with an older start time to | ||
# make sure it ignored | ||
data.set_last_door_state_update_time_utc( | ||
door.device_id, dt.as_utc(datetime.datetime.fromtimestamp(1000)) | ||
) | ||
door_activity_timestamp = 2 | ||
door_activity = MockActivity( | ||
action=ACTION_DOOR_CLOSED, | ||
activity_start_timestamp=door_activity_timestamp, | ||
activity_end_timestamp=door_activity_timestamp, | ||
) | ||
door._sync_door_activity(door_activity) | ||
assert door.last_update_door_state["door_state"] == LockDoorStatus.OPEN | ||
assert door.last_update_door_state["activity_start_time_utc"] == dt.as_utc( | ||
datetime.datetime.fromtimestamp(first_door_activity_timestamp) | ||
) |
Oops, something went wrong.