Improve type hints in template (#74294)

This commit is contained in:
epenet 2022-07-01 19:05:37 +02:00 committed by GitHub
parent 72917f1d2c
commit 9211ba8371
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 27 deletions

View File

@ -14,6 +14,7 @@ from homeassistant.components.binary_sensor import (
DOMAIN as BINARY_SENSOR_DOMAIN, DOMAIN as BINARY_SENSOR_DOMAIN,
ENTITY_ID_FORMAT, ENTITY_ID_FORMAT,
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
BinarySensorDeviceClass,
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.const import ( from homeassistant.const import (
@ -208,16 +209,18 @@ class BinarySensorTemplate(TemplateEntity, BinarySensorEntity, RestoreEntity):
ENTITY_ID_FORMAT, object_id, hass=hass ENTITY_ID_FORMAT, object_id, hass=hass
) )
self._device_class = config.get(CONF_DEVICE_CLASS) self._device_class: BinarySensorDeviceClass | None = config.get(
CONF_DEVICE_CLASS
)
self._template = config[CONF_STATE] self._template = config[CONF_STATE]
self._state = None self._state: bool | None = None
self._delay_cancel = None self._delay_cancel = None
self._delay_on = None self._delay_on = None
self._delay_on_raw = config.get(CONF_DELAY_ON) self._delay_on_raw = config.get(CONF_DELAY_ON)
self._delay_off = None self._delay_off = None
self._delay_off_raw = config.get(CONF_DELAY_OFF) self._delay_off_raw = config.get(CONF_DELAY_OFF)
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Restore state and register callbacks.""" """Restore state and register callbacks."""
if ( if (
(self._delay_on_raw is not None or self._delay_off_raw is not None) (self._delay_on_raw is not None or self._delay_off_raw is not None)
@ -283,12 +286,12 @@ class BinarySensorTemplate(TemplateEntity, BinarySensorEntity, RestoreEntity):
self._delay_cancel = async_call_later(self.hass, delay, _set_state) self._delay_cancel = async_call_later(self.hass, delay, _set_state)
@property @property
def is_on(self): def is_on(self) -> bool | None:
"""Return true if sensor is on.""" """Return true if sensor is on."""
return self._state return self._state
@property @property
def device_class(self): def device_class(self) -> BinarySensorDeviceClass | None:
"""Return the sensor class of the binary sensor.""" """Return the sensor class of the binary sensor."""
return self._device_class return self._device_class

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
from typing import Any
import voluptuous as vol import voluptuous as vol
@ -197,17 +198,17 @@ class LightTemplate(TemplateEntity, LightEntity):
self._supports_transition = False self._supports_transition = False
@property @property
def brightness(self): def brightness(self) -> int | None:
"""Return the brightness of the light.""" """Return the brightness of the light."""
return self._brightness return self._brightness
@property @property
def color_temp(self): def color_temp(self) -> int | None:
"""Return the CT color value in mireds.""" """Return the CT color value in mireds."""
return self._temperature return self._temperature
@property @property
def max_mireds(self): def max_mireds(self) -> int:
"""Return the max mireds value in mireds.""" """Return the max mireds value in mireds."""
if self._max_mireds is not None: if self._max_mireds is not None:
return self._max_mireds return self._max_mireds
@ -215,7 +216,7 @@ class LightTemplate(TemplateEntity, LightEntity):
return super().max_mireds return super().max_mireds
@property @property
def min_mireds(self): def min_mireds(self) -> int:
"""Return the min mireds value in mireds.""" """Return the min mireds value in mireds."""
if self._min_mireds is not None: if self._min_mireds is not None:
return self._min_mireds return self._min_mireds
@ -223,27 +224,27 @@ class LightTemplate(TemplateEntity, LightEntity):
return super().min_mireds return super().min_mireds
@property @property
def white_value(self): def white_value(self) -> int | None:
"""Return the white value.""" """Return the white value."""
return self._white_value return self._white_value
@property @property
def hs_color(self): def hs_color(self) -> tuple[float, float] | None:
"""Return the hue and saturation color value [float, float].""" """Return the hue and saturation color value [float, float]."""
return self._color return self._color
@property @property
def effect(self): def effect(self) -> str | None:
"""Return the effect.""" """Return the effect."""
return self._effect return self._effect
@property @property
def effect_list(self): def effect_list(self) -> list[str] | None:
"""Return the effect list.""" """Return the effect list."""
return self._effect_list return self._effect_list
@property @property
def supported_features(self): def supported_features(self) -> int:
"""Flag supported features.""" """Flag supported features."""
supported_features = 0 supported_features = 0
if self._level_script is not None: if self._level_script is not None:
@ -261,11 +262,11 @@ class LightTemplate(TemplateEntity, LightEntity):
return supported_features return supported_features
@property @property
def is_on(self): def is_on(self) -> bool | None:
"""Return true if device is on.""" """Return true if device is on."""
return self._state return self._state
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Register callbacks.""" """Register callbacks."""
if self._template: if self._template:
self.add_template_attribute( self.add_template_attribute(
@ -345,7 +346,7 @@ class LightTemplate(TemplateEntity, LightEntity):
) )
await super().async_added_to_hass() await super().async_added_to_hass()
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the light on.""" """Turn the light on."""
optimistic_set = False optimistic_set = False
# set optimistic states # set optimistic states
@ -448,7 +449,7 @@ class LightTemplate(TemplateEntity, LightEntity):
if optimistic_set: if optimistic_set:
self.async_write_ha_state() self.async_write_ha_state()
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the light off.""" """Turn the light off."""
if ATTR_TRANSITION in kwargs and self._supports_transition is True: if ATTR_TRANSITION in kwargs and self._supports_transition is True:
await self.async_run_script( await self.async_run_script(

View File

@ -211,7 +211,7 @@ class SensorTemplate(TemplateSensor):
ENTITY_ID_FORMAT, object_id, hass=hass ENTITY_ID_FORMAT, object_id, hass=hass
) )
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Register callbacks.""" """Register callbacks."""
self.add_template_attribute( self.add_template_attribute(
"_attr_native_value", self._template, None, self._update_state "_attr_native_value", self._template, None, self._update_state

View File

@ -1,6 +1,8 @@
"""Support for switches which integrates with other components.""" """Support for switches which integrates with other components."""
from __future__ import annotations from __future__ import annotations
from typing import Any
import voluptuous as vol import voluptuous as vol
from homeassistant.components.switch import ( from homeassistant.components.switch import (
@ -110,7 +112,7 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
self._template = config.get(CONF_VALUE_TEMPLATE) self._template = config.get(CONF_VALUE_TEMPLATE)
self._on_script = Script(hass, config[ON_ACTION], friendly_name, DOMAIN) self._on_script = Script(hass, config[ON_ACTION], friendly_name, DOMAIN)
self._off_script = Script(hass, config[OFF_ACTION], friendly_name, DOMAIN) self._off_script = Script(hass, config[OFF_ACTION], friendly_name, DOMAIN)
self._state = False self._state: bool | None = False
@callback @callback
def _update_state(self, result): def _update_state(self, result):
@ -129,7 +131,7 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
self._state = False self._state = False
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Register callbacks.""" """Register callbacks."""
if self._template is None: if self._template is None:
@ -147,18 +149,18 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
await super().async_added_to_hass() await super().async_added_to_hass()
@property @property
def is_on(self): def is_on(self) -> bool | None:
"""Return true if device is on.""" """Return true if device is on."""
return self._state return self._state
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs: Any) -> None:
"""Fire the on action.""" """Fire the on action."""
await self.async_run_script(self._on_script, context=self._context) await self.async_run_script(self._on_script, context=self._context)
if self._template is None: if self._template is None:
self._state = True self._state = True
self.async_write_ha_state() self.async_write_ha_state()
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs: Any) -> None:
"""Fire the off action.""" """Fire the off action."""
await self.async_run_script(self._off_script, context=self._context) await self.async_run_script(self._off_script, context=self._context)
if self._template is None: if self._template is None:
@ -166,6 +168,6 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
self.async_write_ha_state() self.async_write_ha_state()
@property @property
def assumed_state(self): def assumed_state(self) -> bool:
"""State is assumed, if no template given.""" """State is assumed, if no template given."""
return self._template is None return self._template is None

View File

@ -200,7 +200,7 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
self._attr_fan_speed_list = config[CONF_FAN_SPEED_LIST] self._attr_fan_speed_list = config[CONF_FAN_SPEED_LIST]
@property @property
def state(self): def state(self) -> str | None:
"""Return the status of the vacuum cleaner.""" """Return the status of the vacuum cleaner."""
return self._state return self._state
@ -263,7 +263,7 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
self._attr_fan_speed_list, self._attr_fan_speed_list,
) )
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Register callbacks.""" """Register callbacks."""
if self._template is not None: if self._template is not None:
self.add_template_attribute( self.add_template_attribute(