Fix fritzbox_callmonitor event listener (#70291)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Marc Mueller 2022-04-25 10:24:37 +02:00 committed by GitHub
parent 304426edb1
commit 2676f4df7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,7 +20,7 @@ from homeassistant.const import (
CONF_USERNAME,
EVENT_HOMEASSISTANT_STOP,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import Event, HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -116,10 +116,6 @@ async def async_setup_entry(
port=port,
)
hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP, sensor.async_will_remove_from_hass()
)
async_add_entities([sensor])
@ -138,8 +134,23 @@ class FritzBoxCallSensor(SensorEntity):
self._port = port
self._monitor = None
async def async_added_to_hass(self):
async def async_added_to_hass(self) -> None:
"""Connect to FRITZ!Box to monitor its call state."""
await super().async_added_to_hass()
await self.hass.async_add_executor_job(self._start_call_monitor)
self.async_on_remove(
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP, self._stop_call_monitor
)
)
async def async_will_remove_from_hass(self) -> None:
"""Disconnect from FRITZ!Box by stopping monitor."""
await super().async_will_remove_from_hass()
await self.hass.async_add_executor_job(self._stop_call_monitor)
def _start_call_monitor(self) -> None:
"""Check connection and start callmonitor thread."""
_LOGGER.debug("Starting monitor for: %s", self.entity_id)
self._monitor = FritzBoxCallMonitor(
host=self._host,
@ -148,8 +159,8 @@ class FritzBoxCallSensor(SensorEntity):
)
self._monitor.connect()
async def async_will_remove_from_hass(self):
"""Disconnect from FRITZ!Box by stopping monitor."""
def _stop_call_monitor(self, event: Event | None = None) -> None:
"""Stop callmonitor thread."""
if (
self._monitor
and self._monitor.stopped