Fix fritzbox_callmonitor event listener (#70291)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
304426edb1
commit
2676f4df7a
1 changed files with 19 additions and 8 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue