Skip to content

Commit

Permalink
Use Freezer for tests in TotalConnect (#125960)
Browse files Browse the repository at this point in the history
use Freezer for tests in TotalConnect
  • Loading branch information
austinmroczek authored Sep 14, 2024
1 parent adfca85 commit ad46702
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions tests/components/totalconnect/test_alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.util import dt as dt_util

from .common import (
LOCATION_ID,
Expand Down Expand Up @@ -92,7 +91,9 @@ async def test_attributes(
assert mock_request.call_count == 1


async def test_arm_home_success(hass: HomeAssistant) -> None:
async def test_arm_home_success(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test arm home method success."""
responses = [RESPONSE_DISARMED, RESPONSE_ARM_SUCCESS, RESPONSE_ARMED_STAY]
await setup_platform(hass, ALARM_DOMAIN)
Expand All @@ -108,7 +109,8 @@ async def test_arm_home_success(hass: HomeAssistant) -> None:
)
assert mock_request.call_count == 2

async_fire_time_changed(hass, dt_util.utcnow() + DELAY)
freezer.tick(DELAY)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert mock_request.call_count == 3
assert hass.states.get(ENTITY_ID).state == STATE_ALARM_ARMED_HOME
Expand Down Expand Up @@ -148,7 +150,9 @@ async def test_arm_home_failure(hass: HomeAssistant) -> None:
assert mock_request.call_count == 3


async def test_arm_home_instant_success(hass: HomeAssistant) -> None:
async def test_arm_home_instant_success(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test arm home instant method success."""
responses = [RESPONSE_DISARMED, RESPONSE_ARM_SUCCESS, RESPONSE_ARMED_STAY]
await setup_platform(hass, ALARM_DOMAIN)
Expand All @@ -164,7 +168,8 @@ async def test_arm_home_instant_success(hass: HomeAssistant) -> None:
)
assert mock_request.call_count == 2

async_fire_time_changed(hass, dt_util.utcnow() + DELAY)
freezer.tick(DELAY)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert mock_request.call_count == 3
assert hass.states.get(ENTITY_ID).state == STATE_ALARM_ARMED_HOME
Expand Down Expand Up @@ -205,7 +210,9 @@ async def test_arm_home_instant_failure(hass: HomeAssistant) -> None:
assert mock_request.call_count == 3


async def test_arm_away_instant_success(hass: HomeAssistant) -> None:
async def test_arm_away_instant_success(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test arm home instant method success."""
responses = [RESPONSE_DISARMED, RESPONSE_ARM_SUCCESS, RESPONSE_ARMED_AWAY]
await setup_platform(hass, ALARM_DOMAIN)
Expand All @@ -221,7 +228,8 @@ async def test_arm_away_instant_success(hass: HomeAssistant) -> None:
)
assert mock_request.call_count == 2

async_fire_time_changed(hass, dt_util.utcnow() + DELAY)
freezer.tick(DELAY)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert mock_request.call_count == 3
assert hass.states.get(ENTITY_ID).state == STATE_ALARM_ARMED_AWAY
Expand Down Expand Up @@ -262,7 +270,9 @@ async def test_arm_away_instant_failure(hass: HomeAssistant) -> None:
assert mock_request.call_count == 3


async def test_arm_away_success(hass: HomeAssistant) -> None:
async def test_arm_away_success(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test arm away method success."""
responses = [RESPONSE_DISARMED, RESPONSE_ARM_SUCCESS, RESPONSE_ARMED_AWAY]
await setup_platform(hass, ALARM_DOMAIN)
Expand All @@ -277,7 +287,8 @@ async def test_arm_away_success(hass: HomeAssistant) -> None:
)
assert mock_request.call_count == 2

async_fire_time_changed(hass, dt_util.utcnow() + DELAY)
freezer.tick(DELAY)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert mock_request.call_count == 3
assert hass.states.get(ENTITY_ID).state == STATE_ALARM_ARMED_AWAY
Expand Down Expand Up @@ -315,7 +326,9 @@ async def test_arm_away_failure(hass: HomeAssistant) -> None:
assert mock_request.call_count == 3


async def test_disarm_success(hass: HomeAssistant) -> None:
async def test_disarm_success(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test disarm method success."""
responses = [RESPONSE_ARMED_AWAY, RESPONSE_DISARM_SUCCESS, RESPONSE_DISARMED]
await setup_platform(hass, ALARM_DOMAIN)
Expand All @@ -330,7 +343,8 @@ async def test_disarm_success(hass: HomeAssistant) -> None:
)
assert mock_request.call_count == 2

async_fire_time_changed(hass, dt_util.utcnow() + DELAY)
freezer.tick(DELAY)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert mock_request.call_count == 3
assert hass.states.get(ENTITY_ID).state == STATE_ALARM_DISARMED
Expand Down Expand Up @@ -410,7 +424,9 @@ async def test_disarm_code_required(
assert hass.states.get(ENTITY_ID).state == STATE_ALARM_DISARMED


async def test_arm_night_success(hass: HomeAssistant) -> None:
async def test_arm_night_success(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test arm night method success."""
responses = [RESPONSE_DISARMED, RESPONSE_ARM_SUCCESS, RESPONSE_ARMED_NIGHT]
await setup_platform(hass, ALARM_DOMAIN)
Expand All @@ -425,7 +441,8 @@ async def test_arm_night_success(hass: HomeAssistant) -> None:
)
assert mock_request.call_count == 2

async_fire_time_changed(hass, dt_util.utcnow() + DELAY)
freezer.tick(DELAY)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert mock_request.call_count == 3
assert hass.states.get(ENTITY_ID).state == STATE_ALARM_ARMED_NIGHT
Expand Down Expand Up @@ -463,7 +480,7 @@ async def test_arm_night_failure(hass: HomeAssistant) -> None:
assert mock_request.call_count == 3


async def test_arming(hass: HomeAssistant) -> None:
async def test_arming(hass: HomeAssistant, freezer: FrozenDateTimeFactory) -> None:
"""Test arming."""
responses = [RESPONSE_DISARMED, RESPONSE_SUCCESS, RESPONSE_ARMING]
await setup_platform(hass, ALARM_DOMAIN)
Expand All @@ -478,13 +495,14 @@ async def test_arming(hass: HomeAssistant) -> None:
)
assert mock_request.call_count == 2

async_fire_time_changed(hass, dt_util.utcnow() + DELAY)
freezer.tick(DELAY)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert mock_request.call_count == 3
assert hass.states.get(ENTITY_ID).state == STATE_ALARM_ARMING


async def test_disarming(hass: HomeAssistant) -> None:
async def test_disarming(hass: HomeAssistant, freezer: FrozenDateTimeFactory) -> None:
"""Test disarming."""
responses = [RESPONSE_ARMED_AWAY, RESPONSE_SUCCESS, RESPONSE_DISARMING]
await setup_platform(hass, ALARM_DOMAIN)
Expand All @@ -499,7 +517,8 @@ async def test_disarming(hass: HomeAssistant) -> None:
)
assert mock_request.call_count == 2

async_fire_time_changed(hass, dt_util.utcnow() + DELAY)
freezer.tick(DELAY)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert mock_request.call_count == 3
assert hass.states.get(ENTITY_ID).state == STATE_ALARM_DISARMING
Expand Down Expand Up @@ -566,7 +585,9 @@ async def test_unknown(hass: HomeAssistant) -> None:
assert mock_request.call_count == 1


async def test_other_update_failures(hass: HomeAssistant) -> None:
async def test_other_update_failures(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test other failures seen during updates."""
responses = [
RESPONSE_DISARMED,
Expand All @@ -585,31 +606,36 @@ async def test_other_update_failures(hass: HomeAssistant) -> None:
assert mock_request.call_count == 1

# then an error: ServiceUnavailable --> UpdateFailed
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL)
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
assert hass.states.get(ENTITY_ID).state == STATE_UNAVAILABLE
assert mock_request.call_count == 2

# works again
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL * 2)
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
assert hass.states.get(ENTITY_ID).state == STATE_ALARM_DISARMED
assert mock_request.call_count == 3

# then an error: TotalConnectError --> UpdateFailed
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL * 3)
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
assert hass.states.get(ENTITY_ID).state == STATE_UNAVAILABLE
assert mock_request.call_count == 4

# works again
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL * 4)
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
assert hass.states.get(ENTITY_ID).state == STATE_ALARM_DISARMED
assert mock_request.call_count == 5

# unknown TotalConnect status via ValueError
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL * 5)
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
assert hass.states.get(ENTITY_ID).state == STATE_UNAVAILABLE
assert mock_request.call_count == 6
Expand Down

0 comments on commit ad46702

Please sign in to comment.