hass-core/homeassistant/components/smarthab/cover.py
Baptiste Candellier 3062312649
Add config flow + async support for SmartHab integration (#34387)
* Setup barebones SmartHab config flow

* Setup authentication flow

* Make setup async, add config flow receivers

* Add French translation

* Fix async issues

* Address review comments (thanks bdraco!)

* Fix unloading entries

* Migrate translations dir according to warning

* Create list of components

* Fix pylint false positive

* Fix bad copy-pastes 🤭

* Add async support to SmartHab component

* Address review comments (bdraco)

* Fix pylint

* Improve exception handling (bdraco)

* Apply suggestions from code review (bdraco)

Co-authored-by: J. Nick Koston <nick@koston.org>

* Don't log exceptions manually, fix error

* Reduce repeated lines in async_step_user (bdraco)

* Remove useless else (pylint)

* Remove broad exception handler

* Create strings.json + remove fr i18n

* Write tests for smarthab config flow

* Test import flow

* Fix import test

* Update homeassistant/components/smarthab/config_flow.py

Co-authored-by: J. Nick Koston <nick@koston.org>

Co-authored-by: J. Nick Koston <nick@koston.org>
2020-07-05 14:20:51 -05:00

96 lines
2.5 KiB
Python

"""Support for SmartHab device integration."""
from datetime import timedelta
import logging
import pysmarthab
from requests.exceptions import Timeout
from homeassistant.components.cover import (
ATTR_POSITION,
DEVICE_CLASS_WINDOW,
SUPPORT_CLOSE,
SUPPORT_OPEN,
SUPPORT_SET_POSITION,
CoverEntity,
)
from . import DATA_HUB, DOMAIN
_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=60)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up SmartHab covers from a config entry."""
hub = hass.data[DOMAIN][config_entry.entry_id][DATA_HUB]
entities = (
SmartHabCover(cover)
for cover in await hub.async_get_device_list()
if isinstance(cover, pysmarthab.Shutter)
)
async_add_entities(entities, True)
class SmartHabCover(CoverEntity):
"""Representation a cover."""
def __init__(self, cover):
"""Initialize a SmartHabCover."""
self._cover = cover
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._cover.device_id
@property
def name(self) -> str:
"""Return the display name of this cover."""
return self._cover.label
@property
def current_cover_position(self) -> int:
"""Return current position of cover.
None is unknown, 0 is closed, 100 is fully open.
"""
return self._cover.state
@property
def supported_features(self) -> int:
"""Flag supported features."""
return SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_SET_POSITION
@property
def is_closed(self) -> bool:
"""Return if the cover is closed or not."""
return self._cover.state == 0
@property
def device_class(self) -> str:
"""Return the class of this device, from component DEVICE_CLASSES."""
return DEVICE_CLASS_WINDOW
async def async_open_cover(self, **kwargs):
"""Open the cover."""
await self._cover.async_open()
async def async_close_cover(self, **kwargs):
"""Close cover."""
await self._cover.async_close()
async def async_set_cover_position(self, **kwargs):
"""Move the cover to a specific position."""
await self._cover.async_set_state(kwargs[ATTR_POSITION])
async def async_update(self):
"""Fetch new state data for this cover."""
try:
await self._cover.async_update()
except Timeout:
_LOGGER.error(
"Reached timeout while updating cover %s from API", self.entity_id
)