2019-09-03 23:18:06 +00:00
|
|
|
"""Test pi_hole component."""
|
2020-07-18 06:19:01 +00:00
|
|
|
import logging
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import AsyncMock
|
2020-07-18 06:19:01 +00:00
|
|
|
|
|
|
|
from hole.exceptions import HoleError
|
|
|
|
|
|
|
|
from homeassistant.components import pi_hole, switch
|
|
|
|
from homeassistant.components.pi_hole.const import (
|
|
|
|
CONF_LOCATION,
|
|
|
|
DEFAULT_LOCATION,
|
|
|
|
DEFAULT_NAME,
|
|
|
|
DEFAULT_SSL,
|
|
|
|
DEFAULT_VERIFY_SSL,
|
|
|
|
SERVICE_DISABLE,
|
|
|
|
SERVICE_DISABLE_ATTR_DURATION,
|
|
|
|
)
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_SSL,
|
|
|
|
CONF_VERIFY_SSL,
|
|
|
|
)
|
2020-06-23 17:17:22 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2020-05-13 13:25:06 +00:00
|
|
|
|
2020-07-18 06:19:01 +00:00
|
|
|
from . import (
|
|
|
|
SWITCH_ENTITY_ID,
|
|
|
|
_create_mocked_hole,
|
|
|
|
_patch_config_flow_hole,
|
|
|
|
_patch_init_hole,
|
|
|
|
)
|
2019-09-03 23:18:06 +00:00
|
|
|
|
2020-07-18 06:19:01 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2019-09-03 23:18:06 +00:00
|
|
|
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
|
|
|
|
async def test_setup_minimal_config(hass):
|
|
|
|
"""Tests component setup with minimal config."""
|
2020-05-13 13:25:06 +00:00
|
|
|
mocked_hole = _create_mocked_hole()
|
|
|
|
with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole):
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass, pi_hole.DOMAIN, {pi_hole.DOMAIN: [{"host": "pi.hole"}]}
|
|
|
|
)
|
2019-09-03 23:18:06 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert (
|
|
|
|
hass.states.get("sensor.pi_hole_ads_blocked_today").name
|
|
|
|
== "Pi-Hole Ads Blocked Today"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
hass.states.get("sensor.pi_hole_ads_percentage_blocked_today").name
|
|
|
|
== "Pi-Hole Ads Percentage Blocked Today"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
hass.states.get("sensor.pi_hole_dns_queries_cached").name
|
|
|
|
== "Pi-Hole DNS Queries Cached"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
hass.states.get("sensor.pi_hole_dns_queries_forwarded").name
|
|
|
|
== "Pi-Hole DNS Queries Forwarded"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
hass.states.get("sensor.pi_hole_dns_queries_today").name
|
|
|
|
== "Pi-Hole DNS Queries Today"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
hass.states.get("sensor.pi_hole_dns_unique_clients").name
|
|
|
|
== "Pi-Hole DNS Unique Clients"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
hass.states.get("sensor.pi_hole_dns_unique_domains").name
|
|
|
|
== "Pi-Hole DNS Unique Domains"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
hass.states.get("sensor.pi_hole_domains_blocked").name
|
|
|
|
== "Pi-Hole Domains Blocked"
|
|
|
|
)
|
|
|
|
assert hass.states.get("sensor.pi_hole_seen_clients").name == "Pi-Hole Seen Clients"
|
|
|
|
|
|
|
|
assert hass.states.get("sensor.pi_hole_ads_blocked_today").state == "0"
|
|
|
|
assert hass.states.get("sensor.pi_hole_ads_percentage_blocked_today").state == "0"
|
|
|
|
assert hass.states.get("sensor.pi_hole_dns_queries_cached").state == "0"
|
|
|
|
assert hass.states.get("sensor.pi_hole_dns_queries_forwarded").state == "0"
|
|
|
|
assert hass.states.get("sensor.pi_hole_dns_queries_today").state == "0"
|
|
|
|
assert hass.states.get("sensor.pi_hole_dns_unique_clients").state == "0"
|
|
|
|
assert hass.states.get("sensor.pi_hole_dns_unique_domains").state == "0"
|
|
|
|
assert hass.states.get("sensor.pi_hole_domains_blocked").state == "0"
|
|
|
|
assert hass.states.get("sensor.pi_hole_seen_clients").state == "0"
|
|
|
|
|
2020-07-18 06:19:01 +00:00
|
|
|
assert hass.states.get("binary_sensor.pi_hole").name == "Pi-Hole"
|
|
|
|
assert hass.states.get("binary_sensor.pi_hole").state == "off"
|
|
|
|
|
2019-09-03 23:18:06 +00:00
|
|
|
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
async def test_setup_name_config(hass):
|
|
|
|
"""Tests component setup with a custom name."""
|
2020-05-13 13:25:06 +00:00
|
|
|
mocked_hole = _create_mocked_hole()
|
|
|
|
with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole):
|
2019-09-03 23:18:06 +00:00
|
|
|
assert await async_setup_component(
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
hass,
|
|
|
|
pi_hole.DOMAIN,
|
|
|
|
{pi_hole.DOMAIN: [{"host": "pi.hole", "name": "Custom"}]},
|
2019-09-03 23:18:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert (
|
|
|
|
hass.states.get("sensor.custom_ads_blocked_today").name
|
|
|
|
== "Custom Ads Blocked Today"
|
|
|
|
)
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
|
|
|
|
|
2020-07-18 06:19:01 +00:00
|
|
|
async def test_switch(hass, caplog):
|
|
|
|
"""Test Pi-hole switch."""
|
2020-05-13 13:25:06 +00:00
|
|
|
mocked_hole = _create_mocked_hole()
|
|
|
|
with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole):
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
pi_hole.DOMAIN,
|
2020-07-18 06:19:01 +00:00
|
|
|
{pi_hole.DOMAIN: [{"host": "pi.hole1", "api_key": "1"}]},
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
2020-07-18 06:19:01 +00:00
|
|
|
switch.DOMAIN,
|
|
|
|
switch.SERVICE_TURN_ON,
|
|
|
|
{"entity_id": SWITCH_ENTITY_ID},
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
blocking=True,
|
|
|
|
)
|
2020-07-18 06:19:01 +00:00
|
|
|
mocked_hole.enable.assert_called_once()
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
|
2020-07-18 06:19:01 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
switch.DOMAIN,
|
|
|
|
switch.SERVICE_TURN_OFF,
|
|
|
|
{"entity_id": SWITCH_ENTITY_ID},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
mocked_hole.disable.assert_called_once_with(True)
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
|
2020-07-18 06:19:01 +00:00
|
|
|
# Failed calls
|
|
|
|
type(mocked_hole).enable = AsyncMock(side_effect=HoleError("Error1"))
|
|
|
|
await hass.services.async_call(
|
|
|
|
switch.DOMAIN,
|
|
|
|
switch.SERVICE_TURN_ON,
|
|
|
|
{"entity_id": SWITCH_ENTITY_ID},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
type(mocked_hole).disable = AsyncMock(side_effect=HoleError("Error2"))
|
|
|
|
await hass.services.async_call(
|
|
|
|
switch.DOMAIN,
|
|
|
|
switch.SERVICE_TURN_OFF,
|
|
|
|
{"entity_id": SWITCH_ENTITY_ID},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
errors = [x for x in caplog.records if x.levelno == logging.ERROR]
|
|
|
|
assert errors[-2].message == "Unable to enable Pi-hole: Error1"
|
|
|
|
assert errors[-1].message == "Unable to disable Pi-hole: Error2"
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
|
|
|
|
|
2020-07-18 06:19:01 +00:00
|
|
|
async def test_disable_service_call(hass):
|
|
|
|
"""Test disable service call with no Pi-hole named."""
|
2020-05-13 13:25:06 +00:00
|
|
|
mocked_hole = _create_mocked_hole()
|
|
|
|
with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole):
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
pi_hole.DOMAIN,
|
|
|
|
{
|
|
|
|
pi_hole.DOMAIN: [
|
2020-05-13 13:25:06 +00:00
|
|
|
{"host": "pi.hole1", "api_key": "1"},
|
2020-07-18 06:19:01 +00:00
|
|
|
{"host": "pi.hole2", "name": "Custom"},
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
2020-07-18 06:19:01 +00:00
|
|
|
pi_hole.DOMAIN,
|
|
|
|
SERVICE_DISABLE,
|
|
|
|
{ATTR_ENTITY_ID: "all", SERVICE_DISABLE_ATTR_DURATION: "00:00:01"},
|
|
|
|
blocking=True,
|
(Re)Add support for multiple Pi-Holes (#27569)
* Update configuration schema to support multiple Pi-holes
* Construct sensors for each configured Pi-hole
* Ensure each Pi-hole has a unique name
* Update services to handle multiple Pi-holes
* Update tests for multiple configurations
* Refactor tests to support service testing
* Fix else-raise per pyliunt
* Per code review, add all entities in a single call
* Per code review, add the default name as default.
* Per code review, add cv.ensure_list to prevent breaking change
* Per code review, move name validation to schema
* Remove default name
* Per code review, validate api_key in schema definition
* Per code review, rename variables
* Per code review, use list comprehension
* Ensure unique slug names in config validation
* Per code review, refactor to CoroutineMock
* Fix adding sensor entities
* Per code review, refactor mock function creation
* Per code review, refactor mock function return values
2019-12-12 18:43:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-07-18 06:19:01 +00:00
|
|
|
mocked_hole.disable.assert_called_once_with(1)
|
2020-06-23 01:47:37 +00:00
|
|
|
|
|
|
|
|
2020-07-18 06:19:01 +00:00
|
|
|
async def test_unload(hass):
|
|
|
|
"""Test unload entities."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=pi_hole.DOMAIN,
|
|
|
|
data={
|
|
|
|
CONF_NAME: DEFAULT_NAME,
|
|
|
|
CONF_HOST: "pi.hole",
|
|
|
|
CONF_LOCATION: DEFAULT_LOCATION,
|
|
|
|
CONF_SSL: DEFAULT_SSL,
|
|
|
|
CONF_VERIFY_SSL: DEFAULT_VERIFY_SSL,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
2020-06-23 01:47:37 +00:00
|
|
|
mocked_hole = _create_mocked_hole()
|
|
|
|
with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole):
|
2020-07-18 06:19:01 +00:00
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
2020-06-23 01:47:37 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-07-18 06:19:01 +00:00
|
|
|
assert entry.entry_id in hass.data[pi_hole.DOMAIN]
|
2020-06-23 01:47:37 +00:00
|
|
|
|
2020-07-18 06:19:01 +00:00
|
|
|
assert await hass.config_entries.async_unload(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert entry.entry_id not in hass.data[pi_hole.DOMAIN]
|