Skip to content

Commit 78ed109

Browse files
authored
Simplify netgear_lte service actions (home-assistant#146606)
1 parent 2991726 commit 78ed109

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

homeassistant/components/netgear_lte/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def fire_sms_event(sms: SMS) -> None:
9696
await coordinator.async_config_entry_first_refresh()
9797
entry.runtime_data = coordinator
9898

99-
await async_setup_services(hass, modem)
99+
async_setup_services(hass)
100100

101101
await discovery.async_load_platform(
102102
hass,

homeassistant/components/netgear_lte/services.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Services for the Netgear LTE integration."""
22

3-
from eternalegypt.eternalegypt import Modem
43
import voluptuous as vol
54

6-
from homeassistant.core import HomeAssistant, ServiceCall
5+
from homeassistant.const import CONF_HOST
6+
from homeassistant.core import HomeAssistant, ServiceCall, callback
77
from homeassistant.helpers import config_validation as cv
88

99
from .const import (
@@ -16,6 +16,7 @@
1616
FAILOVER_MODES,
1717
LOGGER,
1818
)
19+
from .coordinator import NetgearLTEConfigEntry
1920

2021
SERVICE_DELETE_SMS = "delete_sms"
2122
SERVICE_SET_OPTION = "set_option"
@@ -45,29 +46,36 @@
4546
DISCONNECT_LTE_SCHEMA = vol.Schema({vol.Optional(ATTR_HOST): cv.string})
4647

4748

48-
async def async_setup_services(hass: HomeAssistant, modem: Modem) -> None:
49-
"""Set up services for Netgear LTE integration."""
49+
async def _service_handler(call: ServiceCall) -> None:
50+
"""Apply a service."""
51+
host = call.data.get(ATTR_HOST)
52+
53+
entry: NetgearLTEConfigEntry | None = None
54+
for entry in call.hass.config_entries.async_loaded_entries(DOMAIN):
55+
if entry.data.get(CONF_HOST) == host:
56+
break
57+
58+
if not entry or not (modem := entry.runtime_data.modem).token:
59+
LOGGER.error("%s: host %s unavailable", call.service, host)
60+
return
5061

51-
async def service_handler(call: ServiceCall) -> None:
52-
"""Apply a service."""
53-
host = call.data.get(ATTR_HOST)
54-
55-
if not modem.token:
56-
LOGGER.error("%s: host %s unavailable", call.service, host)
57-
return
58-
59-
if call.service == SERVICE_DELETE_SMS:
60-
for sms_id in call.data[ATTR_SMS_ID]:
61-
await modem.delete_sms(sms_id)
62-
elif call.service == SERVICE_SET_OPTION:
63-
if failover := call.data.get(ATTR_FAILOVER):
64-
await modem.set_failover_mode(failover)
65-
if autoconnect := call.data.get(ATTR_AUTOCONNECT):
66-
await modem.set_autoconnect_mode(autoconnect)
67-
elif call.service == SERVICE_CONNECT_LTE:
68-
await modem.connect_lte()
69-
elif call.service == SERVICE_DISCONNECT_LTE:
70-
await modem.disconnect_lte()
62+
if call.service == SERVICE_DELETE_SMS:
63+
for sms_id in call.data[ATTR_SMS_ID]:
64+
await modem.delete_sms(sms_id)
65+
elif call.service == SERVICE_SET_OPTION:
66+
if failover := call.data.get(ATTR_FAILOVER):
67+
await modem.set_failover_mode(failover)
68+
if autoconnect := call.data.get(ATTR_AUTOCONNECT):
69+
await modem.set_autoconnect_mode(autoconnect)
70+
elif call.service == SERVICE_CONNECT_LTE:
71+
await modem.connect_lte()
72+
elif call.service == SERVICE_DISCONNECT_LTE:
73+
await modem.disconnect_lte()
74+
75+
76+
@callback
77+
def async_setup_services(hass: HomeAssistant) -> None:
78+
"""Set up services for Netgear LTE integration."""
7179

7280
service_schemas = {
7381
SERVICE_DELETE_SMS: DELETE_SMS_SCHEMA,
@@ -77,4 +85,4 @@ async def service_handler(call: ServiceCall) -> None:
7785
}
7886

7987
for service, schema in service_schemas.items():
80-
hass.services.async_register(DOMAIN, service, service_handler, schema=schema)
88+
hass.services.async_register(DOMAIN, service, _service_handler, schema=schema)

0 commit comments

Comments
 (0)