diff --git a/homeassistant/components/tradfri/cover.py b/homeassistant/components/tradfri/cover.py index 1a3bf841665b4..9b831dce0ec59 100644 --- a/homeassistant/components/tradfri/cover.py +++ b/homeassistant/components/tradfri/cover.py @@ -1,7 +1,4 @@ """Support for IKEA Tradfri covers.""" -import logging - -from pytradfri.error import PytradfriError from homeassistant.components.cover import ( CoverDevice, @@ -10,10 +7,8 @@ SUPPORT_CLOSE, SUPPORT_SET_POSITION, ) -from homeassistant.core import callback -from .const import DOMAIN, KEY_GATEWAY, KEY_API, CONF_GATEWAY_ID - -_LOGGER = logging.getLogger(__name__) +from .base_class import TradfriBaseDevice +from .const import KEY_GATEWAY, KEY_API, CONF_GATEWAY_ID async def async_setup_entry(hass, config_entry, async_add_entities): @@ -29,120 +24,51 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async_add_entities(TradfriCover(cover, api, gateway_id) for cover in covers) -class TradfriCover(CoverDevice): +class TradfriCover(TradfriBaseDevice, CoverDevice): """The platform class required by Home Assistant.""" - def __init__(self, cover, api, gateway_id): + def __init__(self, device, api, gateway_id): """Initialize a cover.""" - self._api = api - self._unique_id = f"{gateway_id}-{cover.id}" - self._cover = None - self._cover_control = None - self._cover_data = None - self._name = None - self._available = True - self._gateway_id = gateway_id + super().__init__(device, api, gateway_id) + self._unique_id = f"{gateway_id}-{device.id}" - self._refresh(cover) + self._refresh(device) @property def supported_features(self): """Flag supported features.""" return SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_SET_POSITION - @property - def unique_id(self): - """Return unique ID for cover.""" - return self._unique_id - - @property - def device_info(self): - """Return the device info.""" - info = self._cover.device_info - - return { - "identifiers": {(DOMAIN, self._cover.id)}, - "name": self._name, - "manufacturer": info.manufacturer, - "model": info.model_number, - "sw_version": info.firmware_version, - "via_device": (DOMAIN, self._gateway_id), - } - - async def async_added_to_hass(self): - """Start thread when added to hass.""" - self._async_start_observe() - - @property - def available(self): - """Return True if entity is available.""" - return self._available - - @property - def should_poll(self): - """No polling needed for tradfri cover.""" - return False - - @property - def name(self): - """Return the display name of this cover.""" - return self._name - @property def current_cover_position(self): """Return current position of cover. None is unknown, 0 is closed, 100 is fully open. """ - return 100 - self._cover_data.current_cover_position + return 100 - self._device_data.current_cover_position async def async_set_cover_position(self, **kwargs): """Move the cover to a specific position.""" - await self._api(self._cover_control.set_state(100 - kwargs[ATTR_POSITION])) + await self._api(self._device_control.set_state(100 - kwargs[ATTR_POSITION])) async def async_open_cover(self, **kwargs): """Open the cover.""" - await self._api(self._cover_control.set_state(0)) + await self._api(self._device_control.set_state(0)) async def async_close_cover(self, **kwargs): """Close cover.""" - await self._api(self._cover_control.set_state(100)) + await self._api(self._device_control.set_state(100)) @property def is_closed(self): """Return if the cover is closed or not.""" return self.current_cover_position == 0 - @callback - def _async_start_observe(self, exc=None): - """Start observation of cover.""" - if exc: - self._available = False - self.async_schedule_update_ha_state() - _LOGGER.warning("Observation failed for %s", self._name, exc_info=exc) - try: - cmd = self._cover.observe( - callback=self._observe_update, - err_callback=self._async_start_observe, - duration=0, - ) - self.hass.async_create_task(self._api(cmd)) - except PytradfriError as err: - _LOGGER.warning("Observation failed, trying again", exc_info=err) - self._async_start_observe() - - def _refresh(self, cover): + def _refresh(self, device): """Refresh the cover data.""" - self._cover = cover + super()._refresh(device) + self._device = device # Caching of BlindControl and cover object - self._available = cover.reachable - self._cover_control = cover.blind_control - self._cover_data = cover.blind_control.blinds[0] - self._name = cover.name - - @callback - def _observe_update(self, tradfri_device): - """Receive new state data for this cover.""" - self._refresh(tradfri_device) - self.async_schedule_update_ha_state() + self._device_control = device.blind_control + self._device_data = device.blind_control.blinds[0] diff --git a/homeassistant/components/tradfri/sensor.py b/homeassistant/components/tradfri/sensor.py index 56c1a464580ba..68a2c10291b6d 100644 --- a/homeassistant/components/tradfri/sensor.py +++ b/homeassistant/components/tradfri/sensor.py @@ -1,12 +1,9 @@ """Support for IKEA Tradfri sensors.""" -import logging from homeassistant.const import DEVICE_CLASS_BATTERY from .base_class import TradfriBaseDevice from .const import KEY_GATEWAY, KEY_API, CONF_GATEWAY_ID -_LOGGER = logging.getLogger(__name__) - async def async_setup_entry(hass, config_entry, async_add_entities): """Set up a Tradfri config entry."""