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,
ENTITY_ID_FORMAT,
PLATFORM_SCHEMA,
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.const import (
@ -208,16 +209,18 @@ class BinarySensorTemplate(TemplateEntity, BinarySensorEntity, RestoreEntity):
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._state = None
self._state: bool | None = None
self._delay_cancel = None
self._delay_on = None
self._delay_on_raw = config.get(CONF_DELAY_ON)
self._delay_off = None
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."""
if (
(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)
@property
def is_on(self):
def is_on(self) -> bool | None:
"""Return true if sensor is on."""
return self._state
@property
def device_class(self):
def device_class(self) -> BinarySensorDeviceClass | None:
"""Return the sensor class of the binary sensor."""
return self._device_class

View file

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

View file

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

View file

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