hass-core/homeassistant/components/smarthab/light.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

67 lines
1.8 KiB
Python

"""Support for SmartHab device integration."""
from datetime import timedelta
import logging
import pysmarthab
from requests.exceptions import Timeout
from homeassistant.components.light import LightEntity
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 lights from a config entry."""
hub = hass.data[DOMAIN][config_entry.entry_id][DATA_HUB]
entities = (
SmartHabLight(light)
for light in await hub.async_get_device_list()
if isinstance(light, pysmarthab.Light)
)
async_add_entities(entities, True)
class SmartHabLight(LightEntity):
"""Representation of a SmartHab Light."""
def __init__(self, light):
"""Initialize a SmartHabLight."""
self._light = light
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._light.device_id
@property
def name(self) -> str:
"""Return the display name of this light."""
return self._light.label
@property
def is_on(self) -> bool:
"""Return true if light is on."""
return self._light.state
async def async_turn_on(self, **kwargs):
"""Instruct the light to turn on."""
await self._light.async_turn_on()
async def async_turn_off(self, **kwargs):
"""Instruct the light to turn off."""
await self._light.async_turn_off()
async def async_update(self):
"""Fetch new state data for this light."""
try:
await self._light.async_update()
except Timeout:
_LOGGER.error(
"Reached timeout while updating light %s from API", self.entity_id
)