"""Coordinator behaviour that is not specific to one platform.""" from __future__ import annotations from datetime import timedelta from unittest.mock import MagicMock from freezegun.api import FrozenDateTimeFactory from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON, STATE_ON from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError import pytest from pytest_homeassistant_custom_component.common import async_fire_time_changed from custom_components.ninlight.api import ( NinlightBusyError, NinlightConnectionError, NinlightRequestError, ) from tests.helpers import entity_id_for @pytest.mark.parametrize( ("error", "key", "placeholders"), [ ( NinlightRequestError(400, "invalid brightness"), "request_failed", {"message": "invalid brightness"}, ), (NinlightBusyError(409, "an update is already running"), "update_busy", None), (NinlightConnectionError("reset"), "cannot_connect", None), ], ) async def test_write_errors_are_translated( hass: HomeAssistant, init_integration, mock_client: MagicMock, error, key, placeholders ) -> None: mock_client.set_state.side_effect = error with pytest.raises(HomeAssistantError) as raised: await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id_for(hass, "light", "light")}, blocking=True, ) assert raised.value.translation_key == key assert raised.value.translation_placeholders == placeholders async def test_polling_pauses_while_an_update_runs( hass: HomeAssistant, init_integration, mock_client: MagicMock, freezer: FrozenDateTimeFactory ) -> None: init_integration.runtime_data.ota_in_progress = True mock_client.get_state.side_effect = NinlightConnectionError("rebooting") reads = mock_client.get_state.await_count freezer.tick(timedelta(seconds=31)) async_fire_time_changed(hass) await hass.async_block_till_done() assert mock_client.get_state.await_count == reads assert hass.states.get(entity_id_for(hass, "light", "light")).state == STATE_ON