forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Fritz services (home-assistant#50056)
- Loading branch information
1 parent
214fd41
commit d877c0c
Showing
6 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""Services for Fritz integration.""" | ||
import logging | ||
|
||
from homeassistant.core import HomeAssistant, ServiceCall | ||
from homeassistant.exceptions import HomeAssistantError | ||
from homeassistant.helpers.service import async_extract_config_entry_ids | ||
|
||
from .const import DOMAIN, FRITZ_SERVICES, SERVICE_REBOOT, SERVICE_RECONNECT | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup_services(hass: HomeAssistant): | ||
"""Set up services for Fritz integration.""" | ||
if hass.data.get(FRITZ_SERVICES, False): | ||
return | ||
|
||
hass.data[FRITZ_SERVICES] = True | ||
|
||
async def async_call_fritz_service(service_call): | ||
"""Call correct Fritz service.""" | ||
|
||
if not ( | ||
fritzbox_entry_ids := await _async_get_configured_fritz_tools( | ||
hass, service_call | ||
) | ||
): | ||
raise HomeAssistantError( | ||
f"Failed to call service '{service_call.service}'. Config entry for target not found" | ||
) | ||
|
||
for entry in fritzbox_entry_ids: | ||
_LOGGER.debug("Executing service %s", service_call.service) | ||
fritz_tools = hass.data[DOMAIN].get(entry) | ||
await fritz_tools.service_fritzbox(service_call.service) | ||
|
||
for service in [SERVICE_REBOOT, SERVICE_RECONNECT]: | ||
hass.services.async_register(DOMAIN, service, async_call_fritz_service) | ||
|
||
|
||
async def _async_get_configured_fritz_tools( | ||
hass: HomeAssistant, service_call: ServiceCall | ||
): | ||
"""Get FritzBoxTools class from config entry.""" | ||
|
||
return [ | ||
entry_id | ||
for entry_id in await async_extract_config_entry_ids(hass, service_call) | ||
if hass.config_entries.async_get_entry(entry_id).domain == DOMAIN | ||
] | ||
|
||
|
||
async def async_unload_services(hass: HomeAssistant): | ||
"""Unload services for Fritz integration.""" | ||
|
||
if not hass.data.get(FRITZ_SERVICES): | ||
return | ||
|
||
hass.data[FRITZ_SERVICES] = False | ||
|
||
hass.services.async_remove(DOMAIN, SERVICE_REBOOT) | ||
hass.services.async_remove(DOMAIN, SERVICE_RECONNECT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
reconnect: | ||
description: Reconnects your FRITZ!Box internet connection | ||
target: | ||
entity: | ||
integration: fritz | ||
domain: binary_sensor | ||
|
||
reboot: | ||
description: Reboots your FRITZ!Box | ||
target: | ||
entity: | ||
integration: fritz | ||
domain: binary_sensor |