From b88e71762de349faea2469823d4741bb6937b825 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 20 Aug 2022 08:37:47 +0200 Subject: [PATCH] Improve type hint in cups sensor entity (#77030) --- homeassistant/components/cups/sensor.py | 97 +++++++++---------------- 1 file changed, 35 insertions(+), 62 deletions(-) diff --git a/homeassistant/components/cups/sensor.py b/homeassistant/components/cups/sensor.py index 893e92f546e..c642eb9112e 100644 --- a/homeassistant/components/cups/sensor.py +++ b/homeassistant/components/cups/sensor.py @@ -4,6 +4,7 @@ from __future__ import annotations from datetime import timedelta import importlib import logging +from typing import Any import voluptuous as vol @@ -62,10 +63,10 @@ def setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the CUPS sensor.""" - host = config[CONF_HOST] - port = config[CONF_PORT] - printers = config[CONF_PRINTERS] - is_cups = config[CONF_IS_CUPS_SERVER] + host: str = config[CONF_HOST] + port: int = config[CONF_PORT] + printers: list[str] = config[CONF_PRINTERS] + is_cups: bool = config[CONF_IS_CUPS_SERVER] if is_cups: data = CupsData(host, port, None) @@ -73,6 +74,7 @@ def setup_platform( if data.available is False: _LOGGER.error("Unable to connect to CUPS server: %s:%s", host, port) raise PlatformNotReady() + assert data.printers is not None dev: list[SensorEntity] = [] for printer in printers: @@ -108,17 +110,14 @@ def setup_platform( class CupsSensor(SensorEntity): """Representation of a CUPS sensor.""" - def __init__(self, data, printer): + _attr_icon = ICON_PRINTER + + def __init__(self, data: CupsData, printer_name: str) -> None: """Initialize the CUPS sensor.""" self.data = data - self._name = printer - self._printer = None - self._available = False - - @property - def name(self): - """Return the name of the sensor.""" - return self._name + self._attr_name = printer_name + self._printer: dict[str, Any] | None = None + self._attr_available = False @property def native_value(self): @@ -129,16 +128,6 @@ class CupsSensor(SensorEntity): key = self._printer["printer-state"] return PRINTER_STATES.get(key, key) - @property - def available(self): - """Return True if entity is available.""" - return self._available - - @property - def icon(self): - """Return the icon to use in the frontend, if any.""" - return ICON_PRINTER - @property def extra_state_attributes(self): """Return the state attributes of the sensor.""" @@ -160,8 +149,10 @@ class CupsSensor(SensorEntity): def update(self) -> None: """Get the latest data and updates the states.""" self.data.update() - self._printer = self.data.printers.get(self._name) - self._available = self.data.available + assert self.name is not None + assert self.data.printers is not None + self._printer = self.data.printers.get(self.name) + self._attr_available = self.data.available class IPPSensor(SensorEntity): @@ -170,28 +161,20 @@ class IPPSensor(SensorEntity): This sensor represents the status of the printer. """ - def __init__(self, data, name): + _attr_icon = ICON_PRINTER + + def __init__(self, data: CupsData, printer_name: str) -> None: """Initialize the sensor.""" self.data = data - self._name = name + self._printer_name = printer_name self._attributes = None - self._available = False + self._attr_available = False @property def name(self): """Return the name of the sensor.""" return self._attributes["printer-make-and-model"] - @property - def icon(self): - """Return the icon to use in the frontend.""" - return ICON_PRINTER - - @property - def available(self): - """Return True if entity is available.""" - return self._available - @property def native_value(self): """Return the state of the sensor.""" @@ -237,8 +220,8 @@ class IPPSensor(SensorEntity): def update(self) -> None: """Fetch new state data for the sensor.""" self.data.update() - self._attributes = self.data.attributes.get(self._name) - self._available = self.data.available + self._attributes = self.data.attributes.get(self._printer_name) + self._attr_available = self.data.available class MarkerSensor(SensorEntity): @@ -247,24 +230,17 @@ class MarkerSensor(SensorEntity): This sensor represents the percentage of ink or toner. """ - def __init__(self, data, printer, name, is_cups): + _attr_icon = ICON_MARKER + _attr_native_unit_of_measurement = PERCENTAGE + + def __init__(self, data: CupsData, printer: str, name: str, is_cups: bool) -> None: """Initialize the sensor.""" self.data = data - self._name = name + self._attr_name = name self._printer = printer self._index = data.attributes[printer]["marker-names"].index(name) self._is_cups = is_cups - self._attributes = None - - @property - def name(self): - """Return the name of the sensor.""" - return self._name - - @property - def icon(self): - """Return the icon to use in the frontend.""" - return ICON_MARKER + self._attributes: dict[str, Any] | None = None @property def native_value(self): @@ -274,11 +250,6 @@ class MarkerSensor(SensorEntity): return self._attributes[self._printer]["marker-levels"][self._index] - @property - def native_unit_of_measurement(self): - """Return the unit of measurement.""" - return PERCENTAGE - @property def extra_state_attributes(self): """Return the state attributes of the sensor.""" @@ -318,17 +289,17 @@ class MarkerSensor(SensorEntity): class CupsData: """Get the latest data from CUPS and update the state.""" - def __init__(self, host, port, ipp_printers): + def __init__(self, host: str, port: int, ipp_printers: list[str] | None) -> None: """Initialize the data object.""" self._host = host self._port = port self._ipp_printers = ipp_printers self.is_cups = ipp_printers is None - self.printers = None - self.attributes = {} + self.printers: dict[str, dict[str, Any]] | None = None + self.attributes: dict[str, Any] = {} self.available = False - def update(self): + def update(self) -> None: """Get the latest data from CUPS.""" cups = importlib.import_module("cups") @@ -336,9 +307,11 @@ class CupsData: conn = cups.Connection(host=self._host, port=self._port) if self.is_cups: self.printers = conn.getPrinters() + assert self.printers is not None for printer in self.printers: self.attributes[printer] = conn.getPrinterAttributes(name=printer) else: + assert self._ipp_printers is not None for ipp_printer in self._ipp_printers: self.attributes[ipp_printer] = conn.getPrinterAttributes( uri=f"ipp://{self._host}:{self._port}/{ipp_printer}"