State and service related type hint improvements (#38956)
* Improve type hint of service_func to register * Add named type for state values * Narrow some unnecessarily broad state type hints
This commit is contained in:
parent
4ea587804e
commit
802c556e82
6 changed files with 27 additions and 24 deletions
|
@ -1,6 +1,6 @@
|
|||
"""Support for IPP sensors."""
|
||||
from datetime import timedelta
|
||||
from typing import Any, Callable, Dict, List, Optional, Union
|
||||
from typing import Any, Callable, Dict, List, Optional
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import DEVICE_CLASS_TIMESTAMP, UNIT_PERCENTAGE
|
||||
|
@ -133,7 +133,7 @@ class IPPMarkerSensor(IPPSensor):
|
|||
}
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> Optional[int]:
|
||||
"""Return the state of the sensor."""
|
||||
level = self.coordinator.data.markers[self.marker_index].level
|
||||
|
||||
|
@ -174,7 +174,7 @@ class IPPPrinterSensor(IPPSensor):
|
|||
}
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> str:
|
||||
"""Return the state of the sensor."""
|
||||
return self.coordinator.data.state.printer_state
|
||||
|
||||
|
@ -197,7 +197,7 @@ class IPPUptimeSensor(IPPSensor):
|
|||
)
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> str:
|
||||
"""Return the state of the sensor."""
|
||||
uptime = utcnow() - timedelta(seconds=self.coordinator.data.info.uptime)
|
||||
return uptime.replace(microsecond=0).isoformat()
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
"""Support for Sonarr sensors."""
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import Any, Callable, Dict, List, Optional, Union
|
||||
from typing import Any, Callable, Dict, List, Optional
|
||||
|
||||
from sonarr import Sonarr, SonarrConnectionError, SonarrError
|
||||
import voluptuous as vol
|
||||
|
@ -236,7 +236,7 @@ class SonarrCommandsSensor(SonarrSensor):
|
|||
return attrs
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> int:
|
||||
"""Return the state of the sensor."""
|
||||
return len(self._commands)
|
||||
|
||||
|
@ -287,7 +287,7 @@ class SonarrDiskspaceSensor(SonarrSensor):
|
|||
return attrs
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> str:
|
||||
"""Return the state of the sensor."""
|
||||
free = self._to_unit(self._total_free)
|
||||
return f"{free:.2f}"
|
||||
|
@ -329,7 +329,7 @@ class SonarrQueueSensor(SonarrSensor):
|
|||
return attrs
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> int:
|
||||
"""Return the state of the sensor."""
|
||||
return len(self._queue)
|
||||
|
||||
|
@ -367,7 +367,7 @@ class SonarrSeriesSensor(SonarrSensor):
|
|||
return attrs
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> int:
|
||||
"""Return the state of the sensor."""
|
||||
return len(self._items)
|
||||
|
||||
|
@ -426,7 +426,7 @@ class SonarrUpcomingSensor(SonarrSensor):
|
|||
return attrs
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> int:
|
||||
"""Return the state of the sensor."""
|
||||
return len(self._upcoming)
|
||||
|
||||
|
@ -438,7 +438,7 @@ class SonarrWantedSensor(SonarrSensor):
|
|||
"""Initialize Sonarr Wanted sensor."""
|
||||
self._max_items = max_items
|
||||
self._results = None
|
||||
self._total = None
|
||||
self._total: Optional[int] = None
|
||||
|
||||
super().__init__(
|
||||
sonarr=sonarr,
|
||||
|
@ -485,6 +485,6 @@ class SonarrWantedSensor(SonarrSensor):
|
|||
return attrs
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> Optional[int]:
|
||||
"""Return the state of the sensor."""
|
||||
return self._total
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
"""Support for WLED sensors."""
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import Any, Callable, Dict, List, Optional, Union
|
||||
from typing import Any, Callable, Dict, List, Optional
|
||||
|
||||
from homeassistant.components.sensor import DEVICE_CLASS_CURRENT
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
|
@ -102,7 +102,7 @@ class WLEDEstimatedCurrentSensor(WLEDSensor):
|
|||
}
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> int:
|
||||
"""Return the state of the sensor."""
|
||||
return self.coordinator.data.info.leds.power
|
||||
|
||||
|
@ -127,7 +127,7 @@ class WLEDUptimeSensor(WLEDSensor):
|
|||
)
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> str:
|
||||
"""Return the state of the sensor."""
|
||||
uptime = utcnow() - timedelta(seconds=self.coordinator.data.info.uptime)
|
||||
return uptime.replace(microsecond=0).isoformat()
|
||||
|
@ -154,7 +154,7 @@ class WLEDFreeHeapSensor(WLEDSensor):
|
|||
)
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> int:
|
||||
"""Return the state of the sensor."""
|
||||
return self.coordinator.data.info.free_heap
|
||||
|
||||
|
@ -175,7 +175,7 @@ class WLEDWifiSignalSensor(WLEDSensor):
|
|||
)
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> int:
|
||||
"""Return the state of the sensor."""
|
||||
return self.coordinator.data.info.wifi.signal
|
||||
|
||||
|
@ -196,7 +196,7 @@ class WLEDWifiRSSISensor(WLEDSensor):
|
|||
)
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> int:
|
||||
"""Return the state of the sensor."""
|
||||
return self.coordinator.data.info.wifi.rssi
|
||||
|
||||
|
@ -221,7 +221,7 @@ class WLEDWifiChannelSensor(WLEDSensor):
|
|||
)
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> int:
|
||||
"""Return the state of the sensor."""
|
||||
return self.coordinator.data.info.wifi.channel
|
||||
|
||||
|
@ -241,6 +241,6 @@ class WLEDWifiBSSIDSensor(WLEDSensor):
|
|||
)
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> str:
|
||||
"""Return the state of the sensor."""
|
||||
return self.coordinator.data.info.wifi.bssid
|
||||
|
|
|
@ -5,7 +5,7 @@ from datetime import datetime, timedelta
|
|||
import functools as ft
|
||||
import logging
|
||||
from timeit import default_timer as timer
|
||||
from typing import Any, Awaitable, Dict, Iterable, List, Optional, Union
|
||||
from typing import Any, Awaitable, Dict, Iterable, List, Optional
|
||||
|
||||
from homeassistant.config import DATA_CUSTOMIZE
|
||||
from homeassistant.const import (
|
||||
|
@ -29,6 +29,7 @@ from homeassistant.exceptions import NoEntitySpecifiedError
|
|||
from homeassistant.helpers.entity_platform import EntityPlatform
|
||||
from homeassistant.helpers.entity_registry import RegistryEntry
|
||||
from homeassistant.helpers.event import Event, async_track_entity_registry_updated_event
|
||||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.util import dt as dt_util, ensure_unique_string, slugify
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -127,7 +128,7 @@ class Entity(ABC):
|
|||
return None
|
||||
|
||||
@property
|
||||
def state(self) -> Union[None, str, int, float]:
|
||||
def state(self) -> StateType:
|
||||
"""Return the state of the entity."""
|
||||
return STATE_UNKNOWN
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import logging
|
|||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Any,
|
||||
Awaitable,
|
||||
Callable,
|
||||
Dict,
|
||||
Iterable,
|
||||
|
@ -499,7 +500,7 @@ def async_register_admin_service(
|
|||
hass: HomeAssistantType,
|
||||
domain: str,
|
||||
service: str,
|
||||
service_func: Callable,
|
||||
service_func: Callable[[ha.ServiceCall], Optional[Awaitable]],
|
||||
schema: vol.Schema = vol.Schema({}, extra=vol.PREVENT_EXTRA),
|
||||
) -> None:
|
||||
"""Register a service that requires admin access."""
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
"""Typing Helpers for Home Assistant."""
|
||||
from typing import Any, Dict, Optional, Tuple
|
||||
from typing import Any, Dict, Optional, Tuple, Union
|
||||
|
||||
import homeassistant.core
|
||||
|
||||
|
@ -13,6 +13,7 @@ EventType = homeassistant.core.Event
|
|||
HomeAssistantType = homeassistant.core.HomeAssistant
|
||||
ServiceCallType = homeassistant.core.ServiceCall
|
||||
ServiceDataType = Dict[str, Any]
|
||||
StateType = Union[None, str, int, float]
|
||||
TemplateVarsType = Optional[Dict[str, Any]]
|
||||
|
||||
# Custom type for recorder Queries
|
||||
|
|
Loading…
Add table
Reference in a new issue