Add and corrects typehints in Entity helper & core class (#26805)

* Add and corrects typehints in Entity class

* Adjust state type based on comments
This commit is contained in:
Franck Nijhof 2019-09-24 23:20:04 +02:00 committed by Paulus Schoutsen
parent b1118cb8ff
commit 6f9ccb5434
2 changed files with 18 additions and 18 deletions

View File

@ -703,7 +703,7 @@ class State:
def __init__( def __init__(
self, self,
entity_id: str, entity_id: str,
state: Any, state: str,
attributes: Optional[Dict] = None, attributes: Optional[Dict] = None,
last_changed: Optional[datetime.datetime] = None, last_changed: Optional[datetime.datetime] = None,
last_updated: Optional[datetime.datetime] = None, last_updated: Optional[datetime.datetime] = None,
@ -732,7 +732,7 @@ class State:
) )
self.entity_id = entity_id.lower() self.entity_id = entity_id.lower()
self.state: str = state self.state = state
self.attributes = MappingProxyType(attributes or {}) self.attributes = MappingProxyType(attributes or {})
self.last_updated = last_updated or dt_util.utcnow() self.last_updated = last_updated or dt_util.utcnow()
self.last_changed = last_changed or self.last_updated self.last_changed = last_changed or self.last_updated
@ -924,7 +924,7 @@ class StateMachine:
def set( def set(
self, self,
entity_id: str, entity_id: str,
new_state: Any, new_state: str,
attributes: Optional[Dict] = None, attributes: Optional[Dict] = None,
force_update: bool = False, force_update: bool = False,
context: Optional[Context] = None, context: Optional[Context] = None,
@ -950,7 +950,7 @@ class StateMachine:
def async_set( def async_set(
self, self,
entity_id: str, entity_id: str,
new_state: Any, new_state: str,
attributes: Optional[Dict] = None, attributes: Optional[Dict] = None,
force_update: bool = False, force_update: bool = False,
context: Optional[Context] = None, context: Optional[Context] = None,

View File

@ -3,7 +3,7 @@ from datetime import timedelta
import logging import logging
import functools as ft import functools as ft
from timeit import default_timer as timer from timeit import default_timer as timer
from typing import Any, Optional, List, Iterable from typing import Any, Dict, Iterable, List, Optional, Union
from homeassistant.const import ( from homeassistant.const import (
ATTR_ASSUMED_STATE, ATTR_ASSUMED_STATE,
@ -26,7 +26,7 @@ from homeassistant.helpers.entity_registry import (
EVENT_ENTITY_REGISTRY_UPDATED, EVENT_ENTITY_REGISTRY_UPDATED,
RegistryEntry, RegistryEntry,
) )
from homeassistant.core import HomeAssistant, callback, CALLBACK_TYPE from homeassistant.core import HomeAssistant, callback, CALLBACK_TYPE, Context
from homeassistant.config import DATA_CUSTOMIZE from homeassistant.config import DATA_CUSTOMIZE
from homeassistant.exceptions import NoEntitySpecifiedError from homeassistant.exceptions import NoEntitySpecifiedError
from homeassistant.util import ensure_unique_string, slugify from homeassistant.util import ensure_unique_string, slugify
@ -137,12 +137,12 @@ class Entity:
return None return None
@property @property
def state(self) -> str: def state(self) -> Union[None, str, int, float]:
"""Return the state of the entity.""" """Return the state of the entity."""
return STATE_UNKNOWN return STATE_UNKNOWN
@property @property
def state_attributes(self): def state_attributes(self) -> Optional[Dict[str, Any]]:
"""Return the state attributes. """Return the state attributes.
Implemented by component base class. Implemented by component base class.
@ -150,7 +150,7 @@ class Entity:
return None return None
@property @property
def device_state_attributes(self): def device_state_attributes(self) -> Optional[Dict[str, Any]]:
"""Return device specific state attributes. """Return device specific state attributes.
Implemented by platform classes. Implemented by platform classes.
@ -158,7 +158,7 @@ class Entity:
return None return None
@property @property
def device_info(self): def device_info(self) -> Optional[Dict[str, Any]]:
"""Return device specific attributes. """Return device specific attributes.
Implemented by platform classes. Implemented by platform classes.
@ -171,17 +171,17 @@ class Entity:
return None return None
@property @property
def unit_of_measurement(self): def unit_of_measurement(self) -> Optional[str]:
"""Return the unit of measurement of this entity, if any.""" """Return the unit of measurement of this entity, if any."""
return None return None
@property @property
def icon(self): def icon(self) -> Optional[str]:
"""Return the icon to use in the frontend, if any.""" """Return the icon to use in the frontend, if any."""
return None return None
@property @property
def entity_picture(self): def entity_picture(self) -> Optional[str]:
"""Return the entity picture to use in the frontend, if any.""" """Return the entity picture to use in the frontend, if any."""
return None return None
@ -215,12 +215,12 @@ class Entity:
return None return None
@property @property
def context_recent_time(self): def context_recent_time(self) -> timedelta:
"""Time that a context is considered recent.""" """Time that a context is considered recent."""
return timedelta(seconds=5) return timedelta(seconds=5)
@property @property
def entity_registry_enabled_default(self): def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry.""" """Return if the entity should be enabled when first added to the entity registry."""
return True return True
@ -230,12 +230,12 @@ class Entity:
# produce undesirable effects in the entity's operation. # produce undesirable effects in the entity's operation.
@property @property
def enabled(self): def enabled(self) -> bool:
"""Return if the entity is enabled in the entity registry.""" """Return if the entity is enabled in the entity registry."""
return self.registry_entry is None or not self.registry_entry.disabled return self.registry_entry is None or not self.registry_entry.disabled
@callback @callback
def async_set_context(self, context): def async_set_context(self, context: Context) -> None:
"""Set the context the entity currently operates under.""" """Set the context the entity currently operates under."""
self._context = context self._context = context
self._context_set = dt_util.utcnow() self._context_set = dt_util.utcnow()
@ -540,7 +540,7 @@ class Entity:
return self.unique_id == other.unique_id return self.unique_id == other.unique_id
def __repr__(self): def __repr__(self) -> str:
"""Return the representation.""" """Return the representation."""
return "<Entity {}: {}>".format(self.name, self.state) return "<Entity {}: {}>".format(self.name, self.state)