Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fritz: introduce wrapper.py - part 1 (sensor) (#63682)
* Revert "Improve availability for Shelly Valve" This reverts commit d8cbd81. * Fritz: introduce api.py - part 1 (sensor) * Revert * coverage * Apply review comments * Better naming * Coverage * fix naming * Avoid "ignore[no-any-return]"
- Loading branch information
1 parent
27dd485
commit bdea7fa
Showing
3 changed files
with
104 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
"""AVM FRITZ!Box API wrapper.""" | ||
from __future__ import annotations | ||
|
||
from functools import partial | ||
import logging | ||
from typing import Any | ||
|
||
from fritzconnection.core.exceptions import ( | ||
FritzActionError, | ||
FritzActionFailedError, | ||
FritzConnectionException, | ||
FritzLookUpError, | ||
FritzSecurityError, | ||
FritzServiceError, | ||
) | ||
|
||
from .common import FritzBoxTools | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class AvmWrapper: | ||
"""Setup AVM wrapper for API calls.""" | ||
|
||
def __init__(self, avm_device: FritzBoxTools) -> None: | ||
"""Init wrapper API class.""" | ||
|
||
self._avm_device = avm_device | ||
|
||
def _service_call_action( | ||
self, | ||
service_name: str, | ||
service_suffix: str, | ||
action_name: str, | ||
**kwargs: Any, | ||
) -> dict | None: | ||
"""Return service details.""" | ||
|
||
if ( | ||
f"{service_name}{service_suffix}" | ||
not in self._avm_device.connection.services | ||
): | ||
return None | ||
|
||
try: | ||
result: dict = self._avm_device.connection.call_action( | ||
f"{service_name}:{service_suffix}", | ||
action_name, | ||
**kwargs, | ||
) | ||
return result | ||
except FritzSecurityError: | ||
_LOGGER.error( | ||
"Authorization Error: Please check the provided credentials and verify that you can log into the web interface", | ||
exc_info=True, | ||
) | ||
except ( | ||
FritzActionError, | ||
FritzActionFailedError, | ||
FritzServiceError, | ||
FritzLookUpError, | ||
): | ||
_LOGGER.error( | ||
"Service/Action Error: cannot execute service %s with action %s", | ||
service_name, | ||
action_name, | ||
exc_info=True, | ||
) | ||
except FritzConnectionException: | ||
_LOGGER.error( | ||
"Connection Error: Please check the device is properly configured for remote login", | ||
exc_info=True, | ||
) | ||
return None | ||
|
||
async def _async_service_call_action( | ||
self, service_name: str, service_suffix: str, action_name: str, **kwargs: Any | ||
) -> dict[str, Any] | None: | ||
"""Make call_action async.""" | ||
|
||
return await self._avm_device.hass.async_add_executor_job( | ||
partial( | ||
self._service_call_action, | ||
service_name, | ||
service_suffix, | ||
action_name, | ||
**kwargs, | ||
) | ||
) | ||
|
||
async def get_wan_dsl_interface_config(self) -> dict[str, Any] | None: | ||
"""Call WANDSLInterfaceConfig service.""" | ||
|
||
return await self._async_service_call_action( | ||
"WANDSLInterfaceConfig", | ||
"1", | ||
"GetInfo", | ||
) |