Use location info helper for IP in Cloudflare DNS (#81714)
* Use location info helper for IP in Cloudflare DNS * simplify * Blow up * coverage
This commit is contained in:
parent
1b9c2dfb68
commit
e7a616b8ff
2 changed files with 74 additions and 13 deletions
|
@ -4,6 +4,7 @@ from __future__ import annotations
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from aiohttp import ClientSession
|
||||||
from pycfdns import CloudflareUpdater
|
from pycfdns import CloudflareUpdater
|
||||||
from pycfdns.exceptions import (
|
from pycfdns.exceptions import (
|
||||||
CloudflareAuthenticationException,
|
CloudflareAuthenticationException,
|
||||||
|
@ -14,10 +15,16 @@ from pycfdns.exceptions import (
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_API_TOKEN, CONF_ZONE
|
from homeassistant.const import CONF_API_TOKEN, CONF_ZONE
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall
|
from homeassistant.core import HomeAssistant, ServiceCall
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import (
|
||||||
|
ConfigEntryAuthFailed,
|
||||||
|
ConfigEntryNotReady,
|
||||||
|
HomeAssistantError,
|
||||||
|
)
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.event import async_track_time_interval
|
from homeassistant.helpers.event import async_track_time_interval
|
||||||
|
from homeassistant.util.location import async_detect_location_info
|
||||||
|
from homeassistant.util.network import is_ipv4_address
|
||||||
|
|
||||||
from .const import CONF_RECORDS, DEFAULT_UPDATE_INTERVAL, DOMAIN, SERVICE_UPDATE_RECORDS
|
from .const import CONF_RECORDS, DEFAULT_UPDATE_INTERVAL, DOMAIN, SERVICE_UPDATE_RECORDS
|
||||||
|
|
||||||
|
@ -28,8 +35,9 @@ CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up Cloudflare from a config entry."""
|
"""Set up Cloudflare from a config entry."""
|
||||||
|
session = async_get_clientsession(hass)
|
||||||
cfupdate = CloudflareUpdater(
|
cfupdate = CloudflareUpdater(
|
||||||
async_get_clientsession(hass),
|
session,
|
||||||
entry.data[CONF_API_TOKEN],
|
entry.data[CONF_API_TOKEN],
|
||||||
entry.data[CONF_ZONE],
|
entry.data[CONF_ZONE],
|
||||||
entry.data[CONF_RECORDS],
|
entry.data[CONF_RECORDS],
|
||||||
|
@ -45,14 +53,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
async def update_records(now):
|
async def update_records(now):
|
||||||
"""Set up recurring update."""
|
"""Set up recurring update."""
|
||||||
try:
|
try:
|
||||||
await _async_update_cloudflare(cfupdate, zone_id)
|
await _async_update_cloudflare(session, cfupdate, zone_id)
|
||||||
except CloudflareException as error:
|
except CloudflareException as error:
|
||||||
_LOGGER.error("Error updating zone %s: %s", entry.data[CONF_ZONE], error)
|
_LOGGER.error("Error updating zone %s: %s", entry.data[CONF_ZONE], error)
|
||||||
|
|
||||||
async def update_records_service(call: ServiceCall) -> None:
|
async def update_records_service(call: ServiceCall) -> None:
|
||||||
"""Set up service for manual trigger."""
|
"""Set up service for manual trigger."""
|
||||||
try:
|
try:
|
||||||
await _async_update_cloudflare(cfupdate, zone_id)
|
await _async_update_cloudflare(session, cfupdate, zone_id)
|
||||||
except CloudflareException as error:
|
except CloudflareException as error:
|
||||||
_LOGGER.error("Error updating zone %s: %s", entry.data[CONF_ZONE], error)
|
_LOGGER.error("Error updating zone %s: %s", entry.data[CONF_ZONE], error)
|
||||||
|
|
||||||
|
@ -76,11 +84,20 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def _async_update_cloudflare(cfupdate: CloudflareUpdater, zone_id: str):
|
async def _async_update_cloudflare(
|
||||||
|
session: ClientSession,
|
||||||
|
cfupdate: CloudflareUpdater,
|
||||||
|
zone_id: str,
|
||||||
|
) -> None:
|
||||||
_LOGGER.debug("Starting update for zone %s", cfupdate.zone)
|
_LOGGER.debug("Starting update for zone %s", cfupdate.zone)
|
||||||
|
|
||||||
records = await cfupdate.get_record_info(zone_id)
|
records = await cfupdate.get_record_info(zone_id)
|
||||||
_LOGGER.debug("Records: %s", records)
|
_LOGGER.debug("Records: %s", records)
|
||||||
|
|
||||||
await cfupdate.update_records(zone_id, records)
|
location_info = await async_detect_location_info(session)
|
||||||
|
|
||||||
|
if not location_info or not is_ipv4_address(location_info.ip):
|
||||||
|
raise HomeAssistantError("Could not get external IPv4 address")
|
||||||
|
|
||||||
|
await cfupdate.update_records(zone_id, records, location_info.ip)
|
||||||
_LOGGER.debug("Update for zone %s is complete", cfupdate.zone)
|
_LOGGER.debug("Update for zone %s is complete", cfupdate.zone)
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
"""Test the Cloudflare integration."""
|
"""Test the Cloudflare integration."""
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
from pycfdns.exceptions import (
|
from pycfdns.exceptions import (
|
||||||
CloudflareAuthenticationException,
|
CloudflareAuthenticationException,
|
||||||
CloudflareConnectionException,
|
CloudflareConnectionException,
|
||||||
)
|
)
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.cloudflare.const import DOMAIN, SERVICE_UPDATE_RECORDS
|
from homeassistant.components.cloudflare.const import DOMAIN, SERVICE_UPDATE_RECORDS
|
||||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
|
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
|
from homeassistant.util.location import LocationInfo
|
||||||
|
|
||||||
from . import ENTRY_CONFIG, init_integration
|
from . import ENTRY_CONFIG, init_integration
|
||||||
|
|
||||||
|
@ -70,12 +75,51 @@ async def test_integration_services(hass, cfupdate):
|
||||||
entry = await init_integration(hass)
|
entry = await init_integration(hass)
|
||||||
assert entry.state is ConfigEntryState.LOADED
|
assert entry.state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
await hass.services.async_call(
|
with patch(
|
||||||
DOMAIN,
|
"homeassistant.components.cloudflare.async_detect_location_info",
|
||||||
SERVICE_UPDATE_RECORDS,
|
return_value=LocationInfo(
|
||||||
{},
|
"0.0.0.0",
|
||||||
blocking=True,
|
"US",
|
||||||
)
|
"USD",
|
||||||
await hass.async_block_till_done()
|
"CA",
|
||||||
|
"California",
|
||||||
|
"San Diego",
|
||||||
|
"92122",
|
||||||
|
"America/Los_Angeles",
|
||||||
|
32.8594,
|
||||||
|
-117.2073,
|
||||||
|
True,
|
||||||
|
),
|
||||||
|
):
|
||||||
|
|
||||||
|
await hass.services.async_call(
|
||||||
|
DOMAIN,
|
||||||
|
SERVICE_UPDATE_RECORDS,
|
||||||
|
{},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
instance.update_records.assert_called_once()
|
instance.update_records.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
async def test_integration_services_with_issue(hass, cfupdate):
|
||||||
|
"""Test integration services with issue."""
|
||||||
|
instance = cfupdate.return_value
|
||||||
|
|
||||||
|
entry = await init_integration(hass)
|
||||||
|
assert entry.state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.cloudflare.async_detect_location_info",
|
||||||
|
return_value=None,
|
||||||
|
), pytest.raises(HomeAssistantError, match="Could not get external IPv4 address"):
|
||||||
|
await hass.services.async_call(
|
||||||
|
DOMAIN,
|
||||||
|
SERVICE_UPDATE_RECORDS,
|
||||||
|
{},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
instance.update_records.assert_not_called()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue