## Description: Add ESPHome cover position and tilt support. The aioesphomeapi also received a small refactor for these changes and those are part of this PR (constants were refactored into enums and optimistic was renamed to assumed_state). If possible, I'd like to include those in this PR because: 1. It's mostly just very simple changes 2. Because of the new position change the dev branch would be in a non-working state for a while until the split PR is merged (unless I write some temporary glue logic, but I'd prefer to avoid that) ## Checklist: - [x] The code change is tested and works locally. - [x] Local tests pass with `tox`. **Your PR cannot be merged unless tests pass** - [x] There is no commented out code in this PR. If the code communicates with devices, web services, or third-party tools: - [x] [_The manifest file_][manifest-docs] has all fields filled out correctly ([example][ex-manifest]). - [x] New dependencies have been added to `requirements` in the manifest ([example][ex-requir]). - [x] New or updated dependencies have been added to `requirements_all.txt` by running `script/gen_requirements_all.py`. [ex-manifest]: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/mobile_app/manifest.json [ex-requir]: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/mobile_app/manifest.json#L5 [ex-import]: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/keyboard/__init__.py#L23 [manifest-docs]: https://developers.home-assistant.io/docs/en/development_checklist.html#_the-manifest-file_
67 lines
2 KiB
Python
67 lines
2 KiB
Python
"""Support for ESPHome switches."""
|
|
import logging
|
|
from typing import TYPE_CHECKING, Optional
|
|
|
|
from homeassistant.components.switch import SwitchDevice
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
|
|
|
from . import EsphomeEntity, platform_async_setup_entry
|
|
|
|
if TYPE_CHECKING:
|
|
# pylint: disable=unused-import
|
|
from aioesphomeapi import SwitchInfo, SwitchState # noqa
|
|
|
|
DEPENDENCIES = ['esphome']
|
|
_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."""
|
|
# pylint: disable=redefined-outer-name
|
|
from aioesphomeapi import SwitchInfo, SwitchState # noqa
|
|
|
|
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
|
|
|
|
@property
|
|
def is_on(self):
|
|
"""Return true if the switch is on."""
|
|
if self._state is None:
|
|
return None
|
|
return self._state.state
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
"""Turn the entity on."""
|
|
await self._client.switch_command(self._static_info.key, True)
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
"""Turn the entity off."""
|
|
await self._client.switch_command(self._static_info.key, False)
|