Cleanup and optimization for Zerproc (#44430)

* Cleanup and optimization for Zerproc

* Remove unnecessary extra method

* Add debug log for exceptions on disconnect
This commit is contained in:
Emily Mills 2020-12-21 12:50:31 -06:00 committed by GitHub
parent bf253819dd
commit f89c682ea6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 11 deletions

View file

@ -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:

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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()