core/homeassistant/components/pi_hole/sensor.py

81 lines
2.2 KiB
Python
Raw Normal View History

"""Support for getting statistical data from a Pi-hole system."""
2018-06-04 16:49:26 +00:00
import logging
2018-06-04 16:49:26 +00:00
from homeassistant.helpers.entity import Entity
2019-07-31 19:25:30 +00:00
from .const import (
DOMAIN as PIHOLE_DOMAIN,
ATTR_BLOCKED_DOMAINS,
SENSOR_LIST,
SENSOR_DICT,
)
2018-06-04 16:49:26 +00:00
LOGGER = logging.getLogger(__name__)
2018-06-04 16:49:26 +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
pi_hole = hass.data[PIHOLE_DOMAIN]
2018-06-04 16:49:26 +00:00
sensors = []
sensors = [PiHoleSensor(pi_hole, sensor_name) for sensor_name in SENSOR_LIST]
async_add_entities(sensors, True)
class PiHoleSensor(Entity):
2018-06-04 16:49:26 +00:00
"""Representation of a Pi-hole sensor."""
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
self._name = pi_hole.name
self._condition = sensor_name
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 = {}
@property
def name(self):
"""Return the name of the sensor."""
return f"{self._name} {self._condition_name}"
@property
def icon(self):
"""Icon to use in the frontend, if any."""
2018-06-04 16:49:26 +00:00
return self._icon
@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
@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]
@property
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"]}
@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
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