Move sms coordinators to their own file (#100276)
This commit is contained in:
parent
705ee3032b
commit
1f1411b6a5
2 changed files with 57 additions and 51 deletions
|
@ -1,9 +1,6 @@
|
||||||
"""The sms component."""
|
"""The sms component."""
|
||||||
import asyncio
|
|
||||||
from datetime import timedelta
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import gammu
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
@ -12,12 +9,10 @@ from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers import config_validation as cv, discovery
|
from homeassistant.helpers import config_validation as cv, discovery
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_BAUD_SPEED,
|
CONF_BAUD_SPEED,
|
||||||
DEFAULT_BAUD_SPEED,
|
DEFAULT_BAUD_SPEED,
|
||||||
DEFAULT_SCAN_INTERVAL,
|
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
GATEWAY,
|
GATEWAY,
|
||||||
HASS_CONFIG,
|
HASS_CONFIG,
|
||||||
|
@ -25,6 +20,7 @@ from .const import (
|
||||||
SIGNAL_COORDINATOR,
|
SIGNAL_COORDINATOR,
|
||||||
SMS_GATEWAY,
|
SMS_GATEWAY,
|
||||||
)
|
)
|
||||||
|
from .coordinator import NetworkCoordinator, SignalCoordinator
|
||||||
from .gateway import create_sms_gateway
|
from .gateway import create_sms_gateway
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -45,8 +41,6 @@ CONFIG_SCHEMA = vol.Schema(
|
||||||
extra=vol.ALLOW_EXTRA,
|
extra=vol.ALLOW_EXTRA,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Configure Gammu state machine."""
|
"""Configure Gammu state machine."""
|
||||||
|
@ -107,47 +101,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
await gateway.terminate_async()
|
await gateway.terminate_async()
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
class SignalCoordinator(DataUpdateCoordinator):
|
|
||||||
"""Signal strength coordinator."""
|
|
||||||
|
|
||||||
def __init__(self, hass, gateway):
|
|
||||||
"""Initialize signal strength coordinator."""
|
|
||||||
super().__init__(
|
|
||||||
hass,
|
|
||||||
_LOGGER,
|
|
||||||
name="Device signal state",
|
|
||||||
update_interval=timedelta(seconds=DEFAULT_SCAN_INTERVAL),
|
|
||||||
)
|
|
||||||
self._gateway = gateway
|
|
||||||
|
|
||||||
async def _async_update_data(self):
|
|
||||||
"""Fetch device signal quality."""
|
|
||||||
try:
|
|
||||||
async with asyncio.timeout(10):
|
|
||||||
return await self._gateway.get_signal_quality_async()
|
|
||||||
except gammu.GSMError as exc:
|
|
||||||
raise UpdateFailed(f"Error communicating with device: {exc}") from exc
|
|
||||||
|
|
||||||
|
|
||||||
class NetworkCoordinator(DataUpdateCoordinator):
|
|
||||||
"""Network info coordinator."""
|
|
||||||
|
|
||||||
def __init__(self, hass, gateway):
|
|
||||||
"""Initialize network info coordinator."""
|
|
||||||
super().__init__(
|
|
||||||
hass,
|
|
||||||
_LOGGER,
|
|
||||||
name="Device network state",
|
|
||||||
update_interval=timedelta(seconds=DEFAULT_SCAN_INTERVAL),
|
|
||||||
)
|
|
||||||
self._gateway = gateway
|
|
||||||
|
|
||||||
async def _async_update_data(self):
|
|
||||||
"""Fetch device network info."""
|
|
||||||
try:
|
|
||||||
async with asyncio.timeout(10):
|
|
||||||
return await self._gateway.get_network_info_async()
|
|
||||||
except gammu.GSMError as exc:
|
|
||||||
raise UpdateFailed(f"Error communicating with device: {exc}") from exc
|
|
||||||
|
|
56
homeassistant/components/sms/coordinator.py
Normal file
56
homeassistant/components/sms/coordinator.py
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
"""DataUpdateCoordinators for the sms integration."""
|
||||||
|
import asyncio
|
||||||
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import gammu
|
||||||
|
|
||||||
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
|
from .const import DEFAULT_SCAN_INTERVAL
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class SignalCoordinator(DataUpdateCoordinator):
|
||||||
|
"""Signal strength coordinator."""
|
||||||
|
|
||||||
|
def __init__(self, hass, gateway):
|
||||||
|
"""Initialize signal strength coordinator."""
|
||||||
|
super().__init__(
|
||||||
|
hass,
|
||||||
|
_LOGGER,
|
||||||
|
name="Device signal state",
|
||||||
|
update_interval=timedelta(seconds=DEFAULT_SCAN_INTERVAL),
|
||||||
|
)
|
||||||
|
self._gateway = gateway
|
||||||
|
|
||||||
|
async def _async_update_data(self):
|
||||||
|
"""Fetch device signal quality."""
|
||||||
|
try:
|
||||||
|
async with asyncio.timeout(10):
|
||||||
|
return await self._gateway.get_signal_quality_async()
|
||||||
|
except gammu.GSMError as exc:
|
||||||
|
raise UpdateFailed(f"Error communicating with device: {exc}") from exc
|
||||||
|
|
||||||
|
|
||||||
|
class NetworkCoordinator(DataUpdateCoordinator):
|
||||||
|
"""Network info coordinator."""
|
||||||
|
|
||||||
|
def __init__(self, hass, gateway):
|
||||||
|
"""Initialize network info coordinator."""
|
||||||
|
super().__init__(
|
||||||
|
hass,
|
||||||
|
_LOGGER,
|
||||||
|
name="Device network state",
|
||||||
|
update_interval=timedelta(seconds=DEFAULT_SCAN_INTERVAL),
|
||||||
|
)
|
||||||
|
self._gateway = gateway
|
||||||
|
|
||||||
|
async def _async_update_data(self):
|
||||||
|
"""Fetch device network info."""
|
||||||
|
try:
|
||||||
|
async with asyncio.timeout(10):
|
||||||
|
return await self._gateway.get_network_info_async()
|
||||||
|
except gammu.GSMError as exc:
|
||||||
|
raise UpdateFailed(f"Error communicating with device: {exc}") from exc
|
Loading…
Add table
Add a link
Reference in a new issue