(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
This commit is contained in:
1 parent
327b5c3c94
commit
7c42f4b45b
5 files changed
+268
-84
No files matched your search
@@ -3,39 +3,34 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from asynctest import CoroutineMock
|
||||
from hole import Hole
|
||||
|
||||
from homeassistant.components import pi_hole
|
||||
|
||||
from tests.common import async_setup_component
|
||||
|
||||
|
||||
def mock_pihole_data_call(Hole):
|
||||
"""Need to override so as to allow mocked data."""
|
||||
Hole.__init__ = (
|
||||
lambda self, host, loop, session, location, tls, verify_tls=True, api_token=None: None
|
||||
)
|
||||
Hole.data = {
|
||||
"ads_blocked_today": 0,
|
||||
"ads_percentage_today": 0,
|
||||
"clients_ever_seen": 0,
|
||||
"dns_queries_today": 0,
|
||||
"domains_being_blocked": 0,
|
||||
"queries_cached": 0,
|
||||
"queries_forwarded": 0,
|
||||
"status": 0,
|
||||
"unique_clients": 0,
|
||||
"unique_domains": 0,
|
||||
}
|
||||
pass
|
||||
ZERO_DATA = {
|
||||
"ads_blocked_today": 0,
|
||||
"ads_percentage_today": 0,
|
||||
"clients_ever_seen": 0,
|
||||
"dns_queries_today": 0,
|
||||
"domains_being_blocked": 0,
|
||||
"queries_cached": 0,
|
||||
"queries_forwarded": 0,
|
||||
"status": 0,
|
||||
"unique_clients": 0,
|
||||
"unique_domains": 0,
|
||||
}
|
||||
|
||||
|
||||
async def test_setup_no_config(hass):
|
||||
"""Tests component setup with no config."""
|
||||
with patch.object(
|
||||
Hole, "get_data", new=CoroutineMock(side_effect=mock_pihole_data_call(Hole))
|
||||
):
|
||||
assert await async_setup_component(hass, pi_hole.DOMAIN, {pi_hole.DOMAIN: {}})
|
||||
async def test_setup_minimal_config(hass):
|
||||
"""Tests component setup with minimal config."""
|
||||
with patch("homeassistant.components.pi_hole.Hole") as _hole:
|
||||
_hole.return_value.get_data = CoroutineMock(return_value=None)
|
||||
_hole.return_value.data = ZERO_DATA
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, pi_hole.DOMAIN, {pi_hole.DOMAIN: [{"host": "pi.hole"}]}
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -84,13 +79,16 @@ async def test_setup_no_config(hass):
|
||||
assert hass.states.get("sensor.pi_hole_seen_clients").state == "0"
|
||||
|
||||
|
||||
async def test_setup_custom_config(hass):
|
||||
"""Tests component setup with custom config."""
|
||||
with patch.object(
|
||||
Hole, "get_data", new=CoroutineMock(side_effect=mock_pihole_data_call(Hole))
|
||||
):
|
||||
async def test_setup_name_config(hass):
|
||||
"""Tests component setup with a custom name."""
|
||||
with patch("homeassistant.components.pi_hole.Hole") as _hole:
|
||||
_hole.return_value.get_data = CoroutineMock(return_value=None)
|
||||
_hole.return_value.data = ZERO_DATA
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, pi_hole.DOMAIN, {pi_hole.DOMAIN: {"name": "Custom"}}
|
||||
hass,
|
||||
pi_hole.DOMAIN,
|
||||
{pi_hole.DOMAIN: [{"host": "pi.hole", "name": "Custom"}]},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
@@ -99,3 +97,66 @@ async def test_setup_custom_config(hass):
|
||||
hass.states.get("sensor.custom_ads_blocked_today").name
|
||||
== "Custom Ads Blocked Today"
|
||||
)
|
||||
|
||||
|
||||
async def test_disable_service_call(hass):
|
||||
"""Test disable service call with no Pi-hole named."""
|
||||
with patch("homeassistant.components.pi_hole.Hole") as _hole:
|
||||
mock_disable = CoroutineMock(return_value=None)
|
||||
_hole.return_value.disable = mock_disable
|
||||
_hole.return_value.get_data = CoroutineMock(return_value=None)
|
||||
_hole.return_value.data = ZERO_DATA
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
pi_hole.DOMAIN,
|
||||
{
|
||||
pi_hole.DOMAIN: [
|
||||
{"host": "pi.hole", "api_key": "1"},
|
||||
{"host": "pi.hole", "name": "Custom", "api_key": "2"},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await hass.services.async_call(
|
||||
pi_hole.DOMAIN,
|
||||
pi_hole.SERVICE_DISABLE,
|
||||
{pi_hole.SERVICE_DISABLE_ATTR_DURATION: "00:00:01"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_disable.call_count == 2
|
||||
|
||||
|
||||
async def test_enable_service_call(hass):
|
||||
"""Test enable service call with no Pi-hole named."""
|
||||
with patch("homeassistant.components.pi_hole.Hole") as _hole:
|
||||
mock_enable = CoroutineMock(return_value=None)
|
||||
_hole.return_value.enable = mock_enable
|
||||
_hole.return_value.get_data = CoroutineMock(return_value=None)
|
||||
_hole.return_value.data = ZERO_DATA
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
pi_hole.DOMAIN,
|
||||
{
|
||||
pi_hole.DOMAIN: [
|
||||
{"host": "pi.hole", "api_key": "1"},
|
||||
{"host": "pi.hole", "name": "Custom", "api_key": "2"},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await hass.services.async_call(
|
||||
pi_hole.DOMAIN, pi_hole.SERVICE_ENABLE, {}, blocking=True
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_enable.call_count == 2
|
||||
Reference in new issue
Block a user