2019-04-03 15:40:03 +00:00
|
|
|
"""Support for getting statistical data from a Pi-hole system."""
|
2018-06-04 16:49:26 +00:00
|
|
|
import logging
|
2016-09-09 15:10:46 +00:00
|
|
|
|
2018-06-04 16:49:26 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-09-03 23:18:06 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_BLOCKED_DOMAINS,
|
2019-12-09 08:38:14 +00:00
|
|
|
DOMAIN as PIHOLE_DOMAIN,
|
2019-09-03 23:18:06 +00:00
|
|
|
SENSOR_DICT,
|
2019-12-09 08:38:14 +00:00
|
|
|
SENSOR_LIST,
|
2019-09-03 23:18:06 +00:00
|
|
|
)
|
2018-06-04 16:49:26 +00:00
|
|
|
|
2019-09-03 23:18:06 +00:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
2018-06-04 16:49:26 +00:00
|
|
|
|
2016-09-09 15:10:46 +00:00
|
|
|
|
2019-09-03 23:18:06 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
|
|
|
"""Set up the pi-hole sensor."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
2016-09-09 15:10:46 +00:00
|
|
|
|
2019-09-03 23:18:06 +00:00
|
|
|
sensors = []
|
(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
|
|
|
for pi_hole in hass.data[PIHOLE_DOMAIN].values():
|
|
|
|
for sensor in [
|
|
|
|
PiHoleSensor(pi_hole, sensor_name) for sensor_name in SENSOR_LIST
|
|
|
|
]:
|
|
|
|
sensors.append(sensor)
|
2017-03-08 07:20:30 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(sensors, True)
|
2016-09-09 15:10:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PiHoleSensor(Entity):
|
2018-06-04 16:49:26 +00:00
|
|
|
"""Representation of a Pi-hole sensor."""
|
2016-09-09 15:10:46 +00:00
|
|
|
|
2019-09-03 23:18:06 +00:00
|
|
|
def __init__(self, pi_hole, sensor_name):
|
2018-06-04 16:49:26 +00:00
|
|
|
"""Initialize a Pi-hole sensor."""
|
|
|
|
self.pi_hole = pi_hole
|
2019-09-03 23:18:06 +00:00
|
|
|
self._name = pi_hole.name
|
|
|
|
self._condition = sensor_name
|
2017-03-08 07:20:30 +00:00
|
|
|
|
2019-09-03 23:18:06 +00:00
|
|
|
variable_info = SENSOR_DICT[sensor_name]
|
2018-06-04 16:49:26 +00:00
|
|
|
self._condition_name = variable_info[0]
|
|
|
|
self._unit_of_measurement = variable_info[1]
|
|
|
|
self._icon = variable_info[2]
|
|
|
|
self.data = {}
|
2016-09-09 15:10:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2019-09-03 18:35:00 +00:00
|
|
|
return f"{self._name} {self._condition_name}"
|
2017-03-08 07:20:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
2018-06-04 16:49:26 +00:00
|
|
|
return self._icon
|
2017-03-08 07:20:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit the value is expressed in."""
|
2018-06-04 16:49:26 +00:00
|
|
|
return self._unit_of_measurement
|
2016-09-09 15:10:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
2017-07-17 17:21:41 +00:00
|
|
|
try:
|
2018-06-04 16:49:26 +00:00
|
|
|
return round(self.data[self._condition], 2)
|
2017-07-17 17:21:41 +00:00
|
|
|
except TypeError:
|
2018-06-04 16:49:26 +00:00
|
|
|
return self.data[self._condition]
|
2016-09-09 15:10:46 +00:00
|
|
|
|
|
|
|
@property
|
2016-10-10 17:38:32 +00:00
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the Pi-Hole."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {ATTR_BLOCKED_DOMAINS: self.data["domains_being_blocked"]}
|
2016-09-09 15:10:46 +00:00
|
|
|
|
2017-06-20 12:09:54 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Could the device be accessed during the last update call."""
|
2018-06-04 16:49:26 +00:00
|
|
|
return self.pi_hole.available
|
2017-06-20 12:09:54 +00:00
|
|
|
|
2018-06-04 16:49:26 +00:00
|
|
|
async def async_update(self):
|
|
|
|
"""Get the latest data from the Pi-hole API."""
|
|
|
|
await self.pi_hole.async_update()
|
|
|
|
self.data = self.pi_hole.api.data
|