Clean up input_boolean, removing typing exceptions (#52181)
* Clean up input_boolean, removing typing exceptions * Now pushing all local changes...
This commit is contained in:
parent
b939570c9c
commit
958016c44f
3 changed files with 22 additions and 37 deletions
|
@ -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
|
||||||
|
|
||||||
|
@ -77,7 +78,7 @@ class InputBooleanStorageCollection(collection.StorageCollection):
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
def is_on(hass, entity_id):
|
def is_on(hass: HomeAssistant, entity_id: str) -> bool:
|
||||||
"""Test if input_boolean is True."""
|
"""Test if input_boolean is True."""
|
||||||
return hass.states.is_state(entity_id, STATE_ON)
|
return hass.states.is_state(entity_id, STATE_ON)
|
||||||
|
|
||||||
|
@ -144,14 +145,17 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
class InputBoolean(ToggleEntity, RestoreEntity):
|
class InputBoolean(ToggleEntity, RestoreEntity):
|
||||||
"""Representation of a boolean input."""
|
"""Representation of a boolean input."""
|
||||||
|
|
||||||
def __init__(self, config: dict | None) -> None:
|
_attr_should_poll = False
|
||||||
|
|
||||||
|
def __init__(self, config: ConfigType) -> None:
|
||||||
"""Initialize a boolean input."""
|
"""Initialize a boolean input."""
|
||||||
self._config = config
|
self._config = config
|
||||||
self.editable = True
|
self.editable = True
|
||||||
self._state = config.get(CONF_INITIAL)
|
self._attr_is_on = config.get(CONF_INITIAL, False)
|
||||||
|
self._attr_unique_id = config[CONF_ID]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_yaml(cls, config: dict) -> InputBoolean:
|
def from_yaml(cls, config: ConfigType) -> InputBoolean:
|
||||||
"""Return entity instance initialized from yaml storage."""
|
"""Return entity instance initialized from yaml storage."""
|
||||||
input_bool = cls(config)
|
input_bool = cls(config)
|
||||||
input_bool.entity_id = f"{DOMAIN}.{config[CONF_ID]}"
|
input_bool.entity_id = f"{DOMAIN}.{config[CONF_ID]}"
|
||||||
|
@ -159,56 +163,41 @@ class InputBoolean(ToggleEntity, RestoreEntity):
|
||||||
return input_bool
|
return input_bool
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def name(self) -> str | None:
|
||||||
"""If entity should be polled."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return name of the boolean input."""
|
"""Return name of the boolean input."""
|
||||||
return self._config.get(CONF_NAME)
|
return self._config.get(CONF_NAME)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def icon(self) -> str | None:
|
||||||
"""Return the state attributes of the entity."""
|
|
||||||
return {ATTR_EDITABLE: self.editable}
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Return the icon to be used for this entity."""
|
"""Return the icon to be used for this entity."""
|
||||||
return self._config.get(CONF_ICON)
|
return self._config.get(CONF_ICON)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def extra_state_attributes(self) -> dict[str, bool]:
|
||||||
"""Return true if entity is on."""
|
"""Return the state attributes of the entity."""
|
||||||
return self._state
|
return {ATTR_EDITABLE: self.editable}
|
||||||
|
|
||||||
@property
|
async def async_added_to_hass(self) -> None:
|
||||||
def unique_id(self):
|
|
||||||
"""Return a unique ID for the person."""
|
|
||||||
return self._config[CONF_ID]
|
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
|
||||||
"""Call when entity about to be added to hass."""
|
"""Call when entity about to be added to hass."""
|
||||||
# If not None, we got an initial value.
|
# Don't restore if we got an initial value.
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
if self._state is not None:
|
if self._config.get(CONF_INITIAL) is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
state = await self.async_get_last_state()
|
state = await self.async_get_last_state()
|
||||||
self._state = state and state.state == STATE_ON
|
self._attr_is_on = state is not None and state.state == STATE_ON
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the entity on."""
|
"""Turn the entity on."""
|
||||||
self._state = True
|
self._attr_is_on = 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:
|
||||||
"""Turn the entity off."""
|
"""Turn the entity off."""
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_update_config(self, config: dict) -> None:
|
async def async_update_config(self, config: ConfigType) -> None:
|
||||||
"""Handle when the config is updated."""
|
"""Handle when the config is updated."""
|
||||||
self._config = config
|
self._config = config
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
3
mypy.ini
3
mypy.ini
|
@ -1229,9 +1229,6 @@ ignore_errors = true
|
||||||
[mypy-homeassistant.components.influxdb.*]
|
[mypy-homeassistant.components.influxdb.*]
|
||||||
ignore_errors = true
|
ignore_errors = true
|
||||||
|
|
||||||
[mypy-homeassistant.components.input_boolean.*]
|
|
||||||
ignore_errors = true
|
|
||||||
|
|
||||||
[mypy-homeassistant.components.input_datetime.*]
|
[mypy-homeassistant.components.input_datetime.*]
|
||||||
ignore_errors = true
|
ignore_errors = true
|
||||||
|
|
||||||
|
|
|
@ -101,7 +101,6 @@ IGNORED_MODULES: Final[list[str]] = [
|
||||||
"homeassistant.components.image.*",
|
"homeassistant.components.image.*",
|
||||||
"homeassistant.components.incomfort.*",
|
"homeassistant.components.incomfort.*",
|
||||||
"homeassistant.components.influxdb.*",
|
"homeassistant.components.influxdb.*",
|
||||||
"homeassistant.components.input_boolean.*",
|
|
||||||
"homeassistant.components.input_datetime.*",
|
"homeassistant.components.input_datetime.*",
|
||||||
"homeassistant.components.input_number.*",
|
"homeassistant.components.input_number.*",
|
||||||
"homeassistant.components.insteon.*",
|
"homeassistant.components.insteon.*",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue