-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic cover virtual entity (#64)
This entity only supports cover_open and cover_closed services (therefore, only STATE_CLOSED is considered)
- Loading branch information
1 parent
26c94df
commit 0eacb77
Showing
2 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
This component provides support for a virtual cover. | ||
""" | ||
|
||
import logging | ||
import voluptuous as vol | ||
from typing import Any | ||
|
||
import homeassistant.helpers.config_validation as cv | ||
from homeassistant.components.cover import CoverEntity, DOMAIN | ||
from homeassistant.helpers.config_validation import (PLATFORM_SCHEMA) | ||
from homeassistant.const import ( | ||
ATTR_DEVICE_CLASS, | ||
STATE_CLOSED, | ||
) | ||
|
||
from .const import ( | ||
COMPONENT_DOMAIN, | ||
CONF_INITIAL_VALUE, | ||
CONF_CLASS, | ||
) | ||
from .entity import VirtualEntity, virtual_schema | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
DEFAULT_INITIAL_VALUE = "open" | ||
DEPENDENCIES = [COMPONENT_DOMAIN] | ||
|
||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(virtual_schema(DEFAULT_INITIAL_VALUE, { | ||
vol.Optional(CONF_CLASS): cv.string, | ||
})) | ||
|
||
|
||
async def async_setup_platform(_hass, config, async_add_entities, _discovery_info=None): | ||
covers = [VirtualCover(config)] | ||
async_add_entities(covers, True) | ||
|
||
|
||
class VirtualCover(VirtualEntity, CoverEntity): | ||
"""Representation of a Virtual cover.""" | ||
|
||
def __init__(self, config): | ||
"""Initialize the Virtual cover device.""" | ||
super().__init__(config, DOMAIN) | ||
|
||
self._attr_device_class = config.get(CONF_CLASS) | ||
|
||
_LOGGER.info('VirtualCover: {} created'.format(self.name)) | ||
|
||
def _create_state(self, config): | ||
super()._create_state(config) | ||
|
||
self._attr_is_closed = config.get(CONF_INITIAL_VALUE).lower() == STATE_CLOSED | ||
|
||
def _restore_state(self, state, config): | ||
super()._restore_state(state, config) | ||
|
||
self._attr_is_closed = state.state.lower() == STATE_CLOSED | ||
|
||
def _update_attributes(self): | ||
super()._update_attributes(); | ||
self._attr_extra_state_attributes.update({ | ||
name: value for name, value in ( | ||
(ATTR_DEVICE_CLASS, self._attr_device_class), | ||
) if value is not None | ||
}) | ||
|
||
def open_cover(self, **kwargs: Any) -> None: | ||
_LOGGER.info(f'opening {self.name}') | ||
self._attr_is_closed = False | ||
|
||
def close_cover(self, **kwargs: Any) -> None: | ||
_LOGGER.info(f'closing {self.name}') | ||
self._attr_is_closed = True |