Skip to content

Commit

Permalink
Removes use of monitored_conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnluetke committed Aug 11, 2019
1 parent fb0c1a2 commit 4e78b3e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 39 deletions.
16 changes: 2 additions & 14 deletions homeassistant/components/pi_hole/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
import logging
import voluptuous as vol

from homeassistant.const import (
CONF_HOST,
CONF_NAME,
CONF_MONITORED_CONDITIONS,
CONF_SSL,
CONF_VERIFY_SSL,
)
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_SSL, CONF_VERIFY_SSL
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.helpers import config_validation as cv
from homeassistant.exceptions import PlatformNotReady
Expand All @@ -25,7 +19,6 @@
DEFAULT_SSL,
DEFAULT_VERIFY_SSL,
MIN_TIME_BETWEEN_UPDATES,
MONITORED_CONDITIONS,
)

LOGGER = logging.getLogger(__name__)
Expand All @@ -39,9 +32,6 @@
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
vol.Optional(CONF_LOCATION, default=DEFAULT_LOCATION): cv.string,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
vol.Optional(
CONF_MONITORED_CONDITIONS, default=["ads_blocked_today"]
): vol.All(cv.ensure_list, [vol.In(MONITORED_CONDITIONS)]),
}
)
},
Expand Down Expand Up @@ -75,9 +65,7 @@ async def async_setup(hass, config):
hass.data[DOMAIN] = pi_hole

hass.async_create_task(
async_load_platform(
hass, SENSOR_DOMAIN, DOMAIN, conf.get(CONF_MONITORED_CONDITIONS), config
)
async_load_platform(hass, SENSOR_DOMAIN, DOMAIN, None, config)
)

LOGGER.debug("%s integration setup complete", DOMAIN)
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/pi_hole/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)

MONITORED_CONDITIONS = {
SENSOR_DICT = {
"ads_blocked_today": ["Ads Blocked Today", "ads", "mdi:close-octagon-outline"],
"ads_percentage_today": [
"Ads Percentage Blocked Today",
Expand All @@ -39,3 +39,5 @@
"unique_clients": ["DNS Unique Clients", "clients", "mdi:account-outline"],
"unique_domains": ["DNS Unique Domains", "domains", "mdi:domain"],
}

SENSOR_LIST = list(SENSOR_DICT.keys())
20 changes: 10 additions & 10 deletions homeassistant/components/pi_hole/sensor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Support for getting statistical data from a Pi-hole system."""
import logging

from homeassistant.const import CONF_MONITORED_CONDITIONS

from homeassistant.helpers.entity import Entity

from .const import DOMAIN as PIHOLE_DOMAIN, ATTR_BLOCKED_DOMAINS, MONITORED_CONDITIONS
from .const import (
DOMAIN as PIHOLE_DOMAIN,
ATTR_BLOCKED_DOMAINS,
SENSOR_LIST,
SENSOR_DICT,
)

LOGGER = logging.getLogger(__name__)

Expand All @@ -18,24 +21,21 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info)
if discovery_info is None:
return

sensors = [
PiHoleSensor(pi_hole, condition)
for condition in config[CONF_MONITORED_CONDITIONS]
]
sensors = [PiHoleSensor(pi_hole, sensor_name) for sensor_name in SENSOR_LIST]

async_add_entities(sensors, True)


class PiHoleSensor(Entity):
"""Representation of a Pi-hole sensor."""

def __init__(self, pi_hole, condition):
def __init__(self, pi_hole, sensor_name):
"""Initialize a Pi-hole sensor."""
self.pi_hole = pi_hole
self._name = pi_hole.name
self._condition = condition
self._condition = sensor_name

variable_info = MONITORED_CONDITIONS[condition]
variable_info = SENSOR_DICT[sensor_name]
self._condition_name = variable_info[0]
self._unit_of_measurement = variable_info[1]
self._icon = variable_info[2]
Expand Down
2 changes: 1 addition & 1 deletion tests/components/pi_hole/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ def tearDown(self): # pylint: disable=invalid-name

def test_setup_no_config(self):
"""Test a successful setup with no configuration."""
config = {CONF_MONITORED_CONDITIONS: []}
config = {}
with patch.object(Hole, "get_data", new=CoroutineMock()):
assert setup.setup_component(self.hass, pi_hole.DOMAIN, {"pi_hole": config})
14 changes: 1 addition & 13 deletions tests/components/pi_hole/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
DOMAIN as PIHOLE_DOMAIN,
PiHoleData,
)
from homeassistant.const import CONF_MONITORED_CONDITIONS


added_sensors = []
Expand All @@ -22,18 +21,7 @@ async def test_setup_no_monitored_conditions(hass):
"""Test a successful setup with no monitored_conditions."""
hass.data[PIHOLE_DOMAIN] = PiHoleData(None, None)

config = {CONF_MONITORED_CONDITIONS: []}
config = {}
await pi_hole.async_setup_platform(hass, config, mock_async_add_entities, None)

assert len(added_sensors) == 0


async def test_setup_monitored_conditions(hass):
"""Test a successful setup with no monitored_conditions."""
hass.data[PIHOLE_DOMAIN] = PiHoleData(None, "Unit Test")

config = {CONF_MONITORED_CONDITIONS: ["ads_blocked_today"]}
await pi_hole.async_setup_platform(hass, config, mock_async_add_entities, None)

assert len(added_sensors) == 1
assert added_sensors[0].name == "Unit Test Ads Blocked Today"

0 comments on commit 4e78b3e

Please sign in to comment.