diff --git a/homeassistant/components/zerproc/light.py b/homeassistant/components/zerproc/light.py index 2ab4bc127c4..b45ca4497a4 100644 --- a/homeassistant/components/zerproc/light.py +++ b/homeassistant/components/zerproc/light.py @@ -36,7 +36,7 @@ def connect_lights(lights: List[pyzerproc.Light]) -> List[pyzerproc.Light]: connected = [] for light in lights: try: - light.connect(auto_reconnect=True) + light.connect() connected.append(light) except pyzerproc.ZerprocException: _LOGGER.debug("Unable to connect to '%s'", light.address, exc_info=True) @@ -193,6 +193,8 @@ class ZerprocLight(LightEntity): def update(self): """Fetch new state data for this light.""" try: + if not self._light.connected: + self._light.connect() state = self._light.get_state() except pyzerproc.ZerprocException: if self._available: diff --git a/homeassistant/components/zerproc/manifest.json b/homeassistant/components/zerproc/manifest.json index 4f9b559bc19..344ea569104 100644 --- a/homeassistant/components/zerproc/manifest.json +++ b/homeassistant/components/zerproc/manifest.json @@ -4,7 +4,7 @@ "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/zerproc", "requirements": [ - "pyzerproc==0.2.5" + "pyzerproc==0.3.0" ], "codeowners": [ "@emlove" diff --git a/requirements_all.txt b/requirements_all.txt index 1148222aaff..da06aa3cd2d 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1903,7 +1903,7 @@ pyxeoma==1.4.1 pyzbar==0.1.7 # homeassistant.components.zerproc -pyzerproc==0.2.5 +pyzerproc==0.3.0 # homeassistant.components.qnap qnapstats==0.3.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index e451e137dc4..36c65ae0ffe 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -932,7 +932,7 @@ pywebpush==1.9.2 pywilight==0.0.65 # homeassistant.components.zerproc -pyzerproc==0.2.5 +pyzerproc==0.3.0 # homeassistant.components.rachio rachiopy==1.0.3 diff --git a/tests/components/zerproc/test_light.py b/tests/components/zerproc/test_light.py index 871f91d447c..14756f1183c 100644 --- a/tests/components/zerproc/test_light.py +++ b/tests/components/zerproc/test_light.py @@ -24,19 +24,27 @@ from homeassistant.const import ( ) import homeassistant.util.dt as dt_util -from tests.async_mock import patch +from tests.async_mock import MagicMock, patch from tests.common import MockConfigEntry, async_fire_time_changed @pytest.fixture -async def mock_light(hass): +async def mock_entry(hass): + """Create a mock light entity.""" + return MockConfigEntry(domain=DOMAIN) + + +@pytest.fixture +async def mock_light(hass, mock_entry): """Create a mock light entity.""" await setup.async_setup_component(hass, "persistent_notification", {}) - mock_entry = MockConfigEntry(domain=DOMAIN) mock_entry.add_to_hass(hass) - light = pyzerproc.Light("AA:BB:CC:DD:EE:FF", "LEDBlue-CCDDEEFF") + light = MagicMock(spec=pyzerproc.Light) + light.address = "AA:BB:CC:DD:EE:FF" + light.name = "LEDBlue-CCDDEEFF" + light.connected = False mock_state = pyzerproc.LightState(False, (0, 0, 0)) @@ -49,31 +57,36 @@ async def mock_light(hass): await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() + light.connected = True + return light -async def test_init(hass): +async def test_init(hass, mock_entry): """Test platform setup.""" await setup.async_setup_component(hass, "persistent_notification", {}) - mock_entry = MockConfigEntry(domain=DOMAIN) mock_entry.add_to_hass(hass) - mock_light_1 = pyzerproc.Light("AA:BB:CC:DD:EE:FF", "LEDBlue-CCDDEEFF") - mock_light_2 = pyzerproc.Light("11:22:33:44:55:66", "LEDBlue-33445566") + mock_light_1 = MagicMock(spec=pyzerproc.Light) + mock_light_1.address = "AA:BB:CC:DD:EE:FF" + mock_light_1.name = "LEDBlue-CCDDEEFF" + mock_light_1.connected = True + + mock_light_2 = MagicMock(spec=pyzerproc.Light) + mock_light_2.address = "11:22:33:44:55:66" + mock_light_2.name = "LEDBlue-33445566" + mock_light_2.connected = True mock_state_1 = pyzerproc.LightState(False, (0, 0, 0)) mock_state_2 = pyzerproc.LightState(True, (0, 80, 255)) + mock_light_1.get_state.return_value = mock_state_1 + mock_light_2.get_state.return_value = mock_state_2 + with patch( "homeassistant.components.zerproc.light.pyzerproc.discover", return_value=[mock_light_1, mock_light_2], - ), patch.object(mock_light_1, "connect"), patch.object( - mock_light_2, "connect" - ), patch.object( - mock_light_1, "get_state", return_value=mock_state_1 - ), patch.object( - mock_light_2, "get_state", return_value=mock_state_2 ): await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() @@ -98,22 +111,17 @@ async def test_init(hass): ATTR_XY_COLOR: (0.138, 0.08), } - with patch.object(hass.loop, "stop"), patch.object( - mock_light_1, "disconnect" - ) as mock_disconnect_1, patch.object( - mock_light_2, "disconnect" - ) as mock_disconnect_2: + with patch.object(hass.loop, "stop"): await hass.async_stop() - assert mock_disconnect_1.called - assert mock_disconnect_2.called + assert mock_light_1.disconnect.called + assert mock_light_2.disconnect.called -async def test_discovery_exception(hass): +async def test_discovery_exception(hass, mock_entry): """Test platform setup.""" await setup.async_setup_component(hass, "persistent_notification", {}) - mock_entry = MockConfigEntry(domain=DOMAIN) mock_entry.add_to_hass(hass) with patch( @@ -127,14 +135,16 @@ async def test_discovery_exception(hass): assert len(hass.data[DOMAIN]["addresses"]) == 0 -async def test_connect_exception(hass): +async def test_connect_exception(hass, mock_entry): """Test platform setup.""" await setup.async_setup_component(hass, "persistent_notification", {}) - mock_entry = MockConfigEntry(domain=DOMAIN) mock_entry.add_to_hass(hass) - mock_light = pyzerproc.Light("AA:BB:CC:DD:EE:FF", "LEDBlue-CCDDEEFF") + mock_light = MagicMock(spec=pyzerproc.Light) + mock_light.address = "AA:BB:CC:DD:EE:FF" + mock_light.name = "LEDBlue-CCDDEEFF" + mock_light.connected = False with patch( "homeassistant.components.zerproc.light.pyzerproc.discover", @@ -149,6 +159,14 @@ async def test_connect_exception(hass): assert len(hass.data[DOMAIN]["addresses"]) == 0 +async def test_remove_entry(hass, mock_light, mock_entry): + """Test platform setup.""" + with patch.object(mock_light, "disconnect") as mock_disconnect: + await hass.config_entries.async_remove(mock_entry.entry_id) + + assert mock_disconnect.called + + async def test_light_turn_on(hass, mock_light): """Test ZerprocLight turn_on.""" utcnow = dt_util.utcnow()