Skip to content

Commit

Permalink
Test communication errors with coordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdwetering committed Aug 19, 2023
1 parent 93cd531 commit 14c635b
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions tests/test_coordinator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from unittest.mock import Mock

import aiohuesyncbox
import pytest

from custom_components import huesyncbox
from custom_components.huesyncbox.helpers import (
LinearRangeConverter,
get_group_from_area_name,
)
from homeassistant.config_entries import SOURCE_REAUTH
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry

Expand All @@ -19,11 +17,7 @@ async def test_update_device_registry_and_config_entry_on_name_change(

# Verify current name
dr = device_registry.async_get(hass)
device = dr.async_get_device(
identifiers={
(huesyncbox.DOMAIN, "unique_id")
}
)
device = dr.async_get_device(identifiers={(huesyncbox.DOMAIN, "unique_id")})
assert device is not None
assert device.name == "Name"

Expand All @@ -38,14 +32,48 @@ async def test_update_device_registry_and_config_entry_on_name_change(
await force_coordinator_update(hass)

# Check device registry and config entry got updated
device = dr.async_get_device(
identifiers={
(huesyncbox.DOMAIN, "unique_id")
}
)
device = dr.async_get_device(identifiers={(huesyncbox.DOMAIN, "unique_id")})
assert device is not None
assert device.name == "New name"

config_entry = hass.config_entries.async_get_entry(integration.entry.entry_id)
assert config_entry is not None
assert config_entry.title == "New name"


async def test_authentication_error_starts_reauth_flow(hass: HomeAssistant, mock_api):
integration = await setup_integration(hass, mock_api)

# Make sure we start as expected
config_entry = hass.config_entries.async_get_entry(integration.entry.entry_id)
assert config_entry is not None
assert len(list(config_entry.async_get_active_flows(hass, {SOURCE_REAUTH}))) == 0

# Trigger unauthorized update
mock_api.update.side_effect = aiohuesyncbox.Unauthorized
await force_coordinator_update(hass)

# Check if reauth flow is started
config_entry = hass.config_entries.async_get_entry(integration.entry.entry_id)
assert config_entry is not None
assert len(list(config_entry.async_get_active_flows(hass, {SOURCE_REAUTH}))) == 1


async def test_communication_error_marks_entities_unavailable(
hass: HomeAssistant, mock_api
):
entity_under_test = "switch.name_power"
integration = await setup_integration(hass, mock_api)

# Make sure we start as expected
entity = hass.states.get(entity_under_test)
assert entity is not None
assert entity.state == "on"

# Trigger communicaiton error
mock_api.update.side_effect = aiohuesyncbox.RequestError
await force_coordinator_update(hass)

# Check entities are unavailable
entity = hass.states.get(entity_under_test)
assert entity.state == "unavailable"

0 comments on commit 14c635b

Please sign in to comment.