8000 Clean up input_boolean, removing typing exceptions by frenck · Pull Request #52181 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Clean up input_boolean, removing typing exceptions #52181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 22 additions & 33 deletions homeassistant/components/input_boolean/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import logging
from typing import Any

import voluptuous as vol

Expand Down Expand Up @@ -77,7 +78,7 @@ async def _update_data(self, data: dict, update_data: dict) -> dict:


@bind_hass
def is_on(hass, entity_id):
def is_on(hass: HomeAssistant, entity_id: str) -> bool:
"""Test if input_boolean is True."""
return hass.states.is_state(entity_id, STATE_ON)

Expand Down Expand Up @@ -144,71 +145,59 @@ async def reload_service_handler(service_call: ServiceCall) -> None:
class InputBoolean(ToggleEntity, RestoreEntity):
"""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."""
self._config = config
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
def from_yaml(cls, config: dict) -> InputBoolean:
def from_yaml(cls, config: ConfigType) -> InputBoolean:
"""Return entity instance initialized from yaml storage."""
input_bool = cls(config)
input_bool.entity_id = f"{DOMAIN}.{config[CONF_ID]}"
input_bool.editable = False
return input_bool

@property
def should_poll(self):
"""If entity should be polled."""
return False

@property
def name(self):
def name(self) -> str | None:
"""Return name of the boolean input."""
return self._config.get(CONF_NAME)

@property
def extra_state_attributes(self):
"""Return the state attributes of the entity."""
return {ATTR_EDITABLE: self.editable}

@property
def icon(self):
def icon(self) -> str | None:
"""Return the icon to be used for this entity."""
return self._config.get(CONF_ICON)

@property
def is_on(self):
"""Return true if entity is on."""
return self._state

@property
def unique_id(self):
"""Return a unique ID for the person."""
return self._config[CONF_ID]
def extra_state_attributes(self) -> dict[str, bool]:
"""Return the state attributes of the entity."""
return {ATTR_EDITABLE: self.editable}

async def async_added_to_hass(self):
async def async_added_to_hass(self) -> None:
"""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()
if self._state is not None:
if self._config.get(CONF_INITIAL) is not None:
return

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."""
self._state = True
self._attr_is_on = True
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."""
self._state = False
self._attr_is_on = False
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."""
self._config = config
self.async_write_ha_state()
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1229,9 +1229,6 @@ ignore_errors = true
[mypy-homeassistant.components.influxdb.*]
ignore_errors = true

[mypy-homeassistant.components.input_boolean.*]
ignore_errors = true

[mypy-homeassistant.components.input_datetime.*]
ignore_errors = true

Expand Down
1 change: 0 additions & 1 deletion script/hassfest/mypy_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
"homeassistant.components.image.*",
"homeassistant.components.incomfort.*",
"homeassistant.components.influxdb.*",
"homeassistant.components.input_boolean.*",
"homeassistant.components.input_datetime.*",
"homeassistant.components.input_number.*",
"homeassistant.components.insteon.*",
Expand Down
0