mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Fix fritzbox_callmonitor event listener (#70291)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
304426edb1
commit
2676f4df7a
@ -20,7 +20,7 @@ from homeassistant.const import (
|
|||||||
CONF_USERNAME,
|
CONF_USERNAME,
|
||||||
EVENT_HOMEASSISTANT_STOP,
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import Event, HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
@ -116,10 +116,6 @@ async def async_setup_entry(
|
|||||||
port=port,
|
port=port,
|
||||||
)
|
)
|
||||||
|
|
||||||
hass.bus.async_listen_once(
|
|
||||||
EVENT_HOMEASSISTANT_STOP, sensor.async_will_remove_from_hass()
|
|
||||||
)
|
|
||||||
|
|
||||||
async_add_entities([sensor])
|
async_add_entities([sensor])
|
||||||
|
|
||||||
|
|
||||||
@ -138,8 +134,23 @@ class FritzBoxCallSensor(SensorEntity):
|
|||||||
self._port = port
|
self._port = port
|
||||||
self._monitor = None
|
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."""
|
"""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)
|
_LOGGER.debug("Starting monitor for: %s", self.entity_id)
|
||||||
self._monitor = FritzBoxCallMonitor(
|
self._monitor = FritzBoxCallMonitor(
|
||||||
host=self._host,
|
host=self._host,
|
||||||
@ -148,8 +159,8 @@ class FritzBoxCallSensor(SensorEntity):
|
|||||||
)
|
)
|
||||||
self._monitor.connect()
|
self._monitor.connect()
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self):
|
def _stop_call_monitor(self, event: Event | None = None) -> None:
|
||||||
"""Disconnect from FRITZ!Box by stopping monitor."""
|
"""Stop callmonitor thread."""
|
||||||
if (
|
if (
|
||||||
self._monitor
|
self._monitor
|
||||||
and self._monitor.stopped
|
and self._monitor.stopped
|
||||||
|
Loading…
x
Reference in New Issue
Block a user