Static typing for PiHole (#51681)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
9cd3ffbd47
commit
39bf304031
7 changed files with 107 additions and 40 deletions
|
@ -1,7 +1,16 @@
|
|||
"""Support for getting statistical data from a Pi-hole system."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from hole import Hole
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from . import PiHoleEntity
|
||||
from .const import (
|
||||
|
@ -14,7 +23,9 @@ from .const import (
|
|||
)
|
||||
|
||||
|
||||
async def async_setup_entry(hass, entry, async_add_entities):
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the Pi-hole sensor."""
|
||||
name = entry.data[CONF_NAME]
|
||||
hole_data = hass.data[PIHOLE_DOMAIN][entry.entry_id]
|
||||
|
@ -34,7 +45,14 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||
class PiHoleSensor(PiHoleEntity, SensorEntity):
|
||||
"""Representation of a Pi-hole sensor."""
|
||||
|
||||
def __init__(self, api, coordinator, name, sensor_name, server_unique_id):
|
||||
def __init__(
|
||||
self,
|
||||
api: Hole,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
name: str,
|
||||
sensor_name: str,
|
||||
server_unique_id: str,
|
||||
) -> None:
|
||||
"""Initialize a Pi-hole sensor."""
|
||||
super().__init__(api, coordinator, name, server_unique_id)
|
||||
|
||||
|
@ -46,27 +64,27 @@ class PiHoleSensor(PiHoleEntity, SensorEntity):
|
|||
self._icon = variable_info[2]
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
def name(self) -> str:
|
||||
"""Return the name of the sensor."""
|
||||
return f"{self._name} {self._condition_name}"
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
def unique_id(self) -> str:
|
||||
"""Return the unique id of the sensor."""
|
||||
return f"{self._server_unique_id}/{self._condition_name}"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
def icon(self) -> str:
|
||||
"""Icon to use in the frontend, if any."""
|
||||
return self._icon
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
def unit_of_measurement(self) -> str:
|
||||
"""Return the unit the value is expressed in."""
|
||||
return self._unit_of_measurement
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
def state(self) -> Any:
|
||||
"""Return the state of the device."""
|
||||
try:
|
||||
return round(self.api.data[self._condition], 2)
|
||||
|
@ -74,6 +92,6 @@ class PiHoleSensor(PiHoleEntity, SensorEntity):
|
|||
return self.api.data[self._condition]
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
def extra_state_attributes(self) -> dict[str, Any]:
|
||||
"""Return the state attributes of the Pi-hole."""
|
||||
return {ATTR_BLOCKED_DOMAINS: self.api.data["domains_being_blocked"]}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue