mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
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
@ -1,6 +1,6 @@
|
|||||||
"""Support for IPP sensors."""
|
"""Support for IPP sensors."""
|
||||||
from datetime import timedelta
|
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.config_entries import ConfigEntry
|
||||||
from homeassistant.const import DEVICE_CLASS_TIMESTAMP, UNIT_PERCENTAGE
|
from homeassistant.const import DEVICE_CLASS_TIMESTAMP, UNIT_PERCENTAGE
|
||||||
@ -133,7 +133,7 @@ class IPPMarkerSensor(IPPSensor):
|
|||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> Optional[int]:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
level = self.coordinator.data.markers[self.marker_index].level
|
level = self.coordinator.data.markers[self.marker_index].level
|
||||||
|
|
||||||
@ -174,7 +174,7 @@ class IPPPrinterSensor(IPPSensor):
|
|||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> str:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.coordinator.data.state.printer_state
|
return self.coordinator.data.state.printer_state
|
||||||
|
|
||||||
@ -197,7 +197,7 @@ class IPPUptimeSensor(IPPSensor):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> str:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
uptime = utcnow() - timedelta(seconds=self.coordinator.data.info.uptime)
|
uptime = utcnow() - timedelta(seconds=self.coordinator.data.info.uptime)
|
||||||
return uptime.replace(microsecond=0).isoformat()
|
return uptime.replace(microsecond=0).isoformat()
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"""Support for Sonarr sensors."""
|
"""Support for Sonarr sensors."""
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
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
|
from sonarr import Sonarr, SonarrConnectionError, SonarrError
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -236,7 +236,7 @@ class SonarrCommandsSensor(SonarrSensor):
|
|||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> int:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return len(self._commands)
|
return len(self._commands)
|
||||||
|
|
||||||
@ -287,7 +287,7 @@ class SonarrDiskspaceSensor(SonarrSensor):
|
|||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> str:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
free = self._to_unit(self._total_free)
|
free = self._to_unit(self._total_free)
|
||||||
return f"{free:.2f}"
|
return f"{free:.2f}"
|
||||||
@ -329,7 +329,7 @@ class SonarrQueueSensor(SonarrSensor):
|
|||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> int:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return len(self._queue)
|
return len(self._queue)
|
||||||
|
|
||||||
@ -367,7 +367,7 @@ class SonarrSeriesSensor(SonarrSensor):
|
|||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> int:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return len(self._items)
|
return len(self._items)
|
||||||
|
|
||||||
@ -426,7 +426,7 @@ class SonarrUpcomingSensor(SonarrSensor):
|
|||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> int:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return len(self._upcoming)
|
return len(self._upcoming)
|
||||||
|
|
||||||
@ -438,7 +438,7 @@ class SonarrWantedSensor(SonarrSensor):
|
|||||||
"""Initialize Sonarr Wanted sensor."""
|
"""Initialize Sonarr Wanted sensor."""
|
||||||
self._max_items = max_items
|
self._max_items = max_items
|
||||||
self._results = None
|
self._results = None
|
||||||
self._total = None
|
self._total: Optional[int] = None
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
sonarr=sonarr,
|
sonarr=sonarr,
|
||||||
@ -485,6 +485,6 @@ class SonarrWantedSensor(SonarrSensor):
|
|||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> Optional[int]:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self._total
|
return self._total
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"""Support for WLED sensors."""
|
"""Support for WLED sensors."""
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
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.components.sensor import DEVICE_CLASS_CURRENT
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -102,7 +102,7 @@ class WLEDEstimatedCurrentSensor(WLEDSensor):
|
|||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> int:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.coordinator.data.info.leds.power
|
return self.coordinator.data.info.leds.power
|
||||||
|
|
||||||
@ -127,7 +127,7 @@ class WLEDUptimeSensor(WLEDSensor):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> str:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
uptime = utcnow() - timedelta(seconds=self.coordinator.data.info.uptime)
|
uptime = utcnow() - timedelta(seconds=self.coordinator.data.info.uptime)
|
||||||
return uptime.replace(microsecond=0).isoformat()
|
return uptime.replace(microsecond=0).isoformat()
|
||||||
@ -154,7 +154,7 @@ class WLEDFreeHeapSensor(WLEDSensor):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> int:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.coordinator.data.info.free_heap
|
return self.coordinator.data.info.free_heap
|
||||||
|
|
||||||
@ -175,7 +175,7 @@ class WLEDWifiSignalSensor(WLEDSensor):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> int:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.coordinator.data.info.wifi.signal
|
return self.coordinator.data.info.wifi.signal
|
||||||
|
|
||||||
@ -196,7 +196,7 @@ class WLEDWifiRSSISensor(WLEDSensor):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> int:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.coordinator.data.info.wifi.rssi
|
return self.coordinator.data.info.wifi.rssi
|
||||||
|
|
||||||
@ -221,7 +221,7 @@ class WLEDWifiChannelSensor(WLEDSensor):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> int:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.coordinator.data.info.wifi.channel
|
return self.coordinator.data.info.wifi.channel
|
||||||
|
|
||||||
@ -241,6 +241,6 @@ class WLEDWifiBSSIDSensor(WLEDSensor):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> str:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.coordinator.data.info.wifi.bssid
|
return self.coordinator.data.info.wifi.bssid
|
||||||
|
@ -5,7 +5,7 @@ from datetime import datetime, timedelta
|
|||||||
import functools as ft
|
import functools as ft
|
||||||
import logging
|
import logging
|
||||||
from timeit import default_timer as timer
|
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.config import DATA_CUSTOMIZE
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
@ -29,6 +29,7 @@ from homeassistant.exceptions import NoEntitySpecifiedError
|
|||||||
from homeassistant.helpers.entity_platform import EntityPlatform
|
from homeassistant.helpers.entity_platform import EntityPlatform
|
||||||
from homeassistant.helpers.entity_registry import RegistryEntry
|
from homeassistant.helpers.entity_registry import RegistryEntry
|
||||||
from homeassistant.helpers.event import Event, async_track_entity_registry_updated_event
|
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
|
from homeassistant.util import dt as dt_util, ensure_unique_string, slugify
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -127,7 +128,7 @@ class Entity(ABC):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Union[None, str, int, float]:
|
def state(self) -> StateType:
|
||||||
"""Return the state of the entity."""
|
"""Return the state of the entity."""
|
||||||
return STATE_UNKNOWN
|
return STATE_UNKNOWN
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import logging
|
|||||||
from typing import (
|
from typing import (
|
||||||
TYPE_CHECKING,
|
TYPE_CHECKING,
|
||||||
Any,
|
Any,
|
||||||
|
Awaitable,
|
||||||
Callable,
|
Callable,
|
||||||
Dict,
|
Dict,
|
||||||
Iterable,
|
Iterable,
|
||||||
@ -499,7 +500,7 @@ def async_register_admin_service(
|
|||||||
hass: HomeAssistantType,
|
hass: HomeAssistantType,
|
||||||
domain: str,
|
domain: str,
|
||||||
service: str,
|
service: str,
|
||||||
service_func: Callable,
|
service_func: Callable[[ha.ServiceCall], Optional[Awaitable]],
|
||||||
schema: vol.Schema = vol.Schema({}, extra=vol.PREVENT_EXTRA),
|
schema: vol.Schema = vol.Schema({}, extra=vol.PREVENT_EXTRA),
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Register a service that requires admin access."""
|
"""Register a service that requires admin access."""
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
"""Typing Helpers for Home Assistant."""
|
"""Typing Helpers for Home Assistant."""
|
||||||
from typing import Any, Dict, Optional, Tuple
|
from typing import Any, Dict, Optional, Tuple, Union
|
||||||
|
|
||||||
import homeassistant.core
|
import homeassistant.core
|
||||||
|
|
||||||
@ -13,6 +13,7 @@ EventType = homeassistant.core.Event
|
|||||||
HomeAssistantType = homeassistant.core.HomeAssistant
|
HomeAssistantType = homeassistant.core.HomeAssistant
|
||||||
ServiceCallType = homeassistant.core.ServiceCall
|
ServiceCallType = homeassistant.core.ServiceCall
|
||||||
ServiceDataType = Dict[str, Any]
|
ServiceDataType = Dict[str, Any]
|
||||||
|
StateType = Union[None, str, int, float]
|
||||||
TemplateVarsType = Optional[Dict[str, Any]]
|
TemplateVarsType = Optional[Dict[str, Any]]
|
||||||
|
|
||||||
# Custom type for recorder Queries
|
# Custom type for recorder Queries
|
||||||
|
Loading…
x
Reference in New Issue
Block a user