* Upgrade pylint to 2.4.2 and astroid to 2.3.1 https://pylint.readthedocs.io/en/latest/whatsnew/2.4.html https://pylint.readthedocs.io/en/latest/whatsnew/changelog.html#what-s-new-in-pylint-2-4-1 https://pylint.readthedocs.io/en/latest/whatsnew/changelog.html#what-s-new-in-pylint-2-4-2 * unnecessary-comprehension fixes * invalid-name fixes * self-assigning-variable fixes * Re-enable not-an-iterable * used-before-assignment fix * invalid-overridden-method fixes * undefined-variable __class__ workarounds https://github.com/PyCQA/pylint/issues/3090 * no-member false positive disabling * Remove some no longer needed disables * using-constant-test fix * Disable import-outside-toplevel for now * Disable some apparent no-value-for-parameter false positives * invalid-overridden-method false positive disables https://github.com/PyCQA/pylint/issues/3150 * Fix unintentional Entity.force_update override in AfterShipSensor
65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
"""Support for ESPHome switches."""
|
|
import logging
|
|
from typing import Optional
|
|
|
|
from aioesphomeapi import SwitchInfo, SwitchState
|
|
|
|
from homeassistant.components.switch import SwitchDevice
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
|
|
|
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
|
|
) -> None:
|
|
"""Set up ESPHome switches based on a config entry."""
|
|
await platform_async_setup_entry(
|
|
hass,
|
|
entry,
|
|
async_add_entities,
|
|
component_key="switch",
|
|
info_type=SwitchInfo,
|
|
entity_type=EsphomeSwitch,
|
|
state_type=SwitchState,
|
|
)
|
|
|
|
|
|
class EsphomeSwitch(EsphomeEntity, SwitchDevice):
|
|
"""A switch implementation for ESPHome."""
|
|
|
|
@property
|
|
def _static_info(self) -> SwitchInfo:
|
|
return super()._static_info
|
|
|
|
@property
|
|
def _state(self) -> Optional[SwitchState]:
|
|
return super()._state
|
|
|
|
@property
|
|
def icon(self) -> str:
|
|
"""Return the icon."""
|
|
return self._static_info.icon
|
|
|
|
@property
|
|
def assumed_state(self) -> bool:
|
|
"""Return true if we do optimistic updates."""
|
|
return self._static_info.assumed_state
|
|
|
|
# https://github.com/PyCQA/pylint/issues/3150 for @esphome_state_property
|
|
# pylint: disable=invalid-overridden-method
|
|
@esphome_state_property
|
|
def is_on(self) -> Optional[bool]:
|
|
"""Return true if the switch is on."""
|
|
return self._state.state
|
|
|
|
async def async_turn_on(self, **kwargs) -> None:
|
|
"""Turn the entity on."""
|
|
await self._client.switch_command(self._static_info.key, True)
|
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
|
"""Turn the entity off."""
|
|
await self._client.switch_command(self._static_info.key, False)
|