From f89c682ea6bc3cc68e1fa5737c7871c0243a3075 Mon Sep 17 00:00:00 2001 From: Emily Mills Date: Mon, 21 Dec 2020 12:50:31 -0600 Subject: [PATCH] Cleanup and optimization for Zerproc (#44430) * Cleanup and optimization for Zerproc * Remove unnecessary extra method * Add debug log for exceptions on disconnect --- homeassistant/components/zerproc/light.py | 17 +++++++++-------- homeassistant/components/zerproc/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/zerproc/test_light.py | 10 ++++++++++ 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/zerproc/light.py b/homeassistant/components/zerproc/light.py index fc1f0c9a707..89f60faf84e 100644 --- a/homeassistant/components/zerproc/light.py +++ b/homeassistant/components/zerproc/light.py @@ -110,17 +110,18 @@ class ZerprocLight(LightEntity): """Run when entity about to be added to hass.""" self.async_on_remove( self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, self.on_hass_shutdown + EVENT_HOMEASSISTANT_STOP, self.async_will_remove_from_hass ) ) - async def async_will_remove_from_hass(self) -> None: + async def async_will_remove_from_hass(self, *args) -> None: """Run when entity will be removed from hass.""" - await self._light.disconnect() - - async def on_hass_shutdown(self, event): - """Execute when Home Assistant is shutting down.""" - await self._light.disconnect() + try: + await self._light.disconnect() + except pyzerproc.ZerprocException: + _LOGGER.debug( + "Exception disconnected from %s", self.entity_id, exc_info=True + ) @property def name(self): @@ -192,7 +193,7 @@ class ZerprocLight(LightEntity): async def async_update(self): """Fetch new state data for this light.""" try: - if not await self._light.is_connected(): + if not self._available: await self._light.connect() state = await self._light.get_state() except pyzerproc.ZerprocException: diff --git a/homeassistant/components/zerproc/manifest.json b/homeassistant/components/zerproc/manifest.json index d5a61fd18ae..54b70d78673 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.4.3" + "pyzerproc==0.4.7" ], "codeowners": [ "@emlove" diff --git a/requirements_all.txt b/requirements_all.txt index 210e440a158..1ba5d3d2000 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1904,7 +1904,7 @@ pyxeoma==1.4.1 pyzbar==0.1.7 # homeassistant.components.zerproc -pyzerproc==0.4.3 +pyzerproc==0.4.7 # homeassistant.components.qnap qnapstats==0.3.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index e8e173e1b6b..b6613a840fe 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -939,7 +939,7 @@ pywemo==0.5.3 pywilight==0.0.65 # homeassistant.components.zerproc -pyzerproc==0.4.3 +pyzerproc==0.4.7 # homeassistant.components.rachio rachiopy==1.0.3 diff --git a/tests/components/zerproc/test_light.py b/tests/components/zerproc/test_light.py index 92d8d2638b9..77fdfb7d48a 100644 --- a/tests/components/zerproc/test_light.py +++ b/tests/components/zerproc/test_light.py @@ -173,6 +173,16 @@ async def test_remove_entry(hass, mock_light, mock_entry): assert mock_disconnect.called +async def test_remove_entry_exceptions_caught(hass, mock_light, mock_entry): + """Assert that disconnect exceptions are caught.""" + with patch.object( + mock_light, "disconnect", side_effect=pyzerproc.ZerprocException("Mock error") + ) 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()