2023-01-03 00:22:40 +00:00
|
|
|
"""Support for ISY covers."""
|
2024-03-08 13:52:48 +00:00
|
|
|
|
2022-02-03 16:02:05 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-11-21 17:07:30 +00:00
|
|
|
from typing import Any, cast
|
2021-05-18 19:15:47 +00:00
|
|
|
|
2020-05-08 04:15:42 +00:00
|
|
|
from pyisy.constants import ISY_VALUE_UNKNOWN
|
|
|
|
|
2020-05-28 13:51:56 +00:00
|
|
|
from homeassistant.components.cover import (
|
|
|
|
ATTR_POSITION,
|
|
|
|
CoverEntity,
|
2022-04-07 07:35:27 +00:00
|
|
|
CoverEntityFeature,
|
2020-05-28 13:51:56 +00:00
|
|
|
)
|
2020-05-09 19:49:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-01-06 23:16:14 +00:00
|
|
|
from homeassistant.const import Platform
|
2021-04-23 08:11:58 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-05-09 19:49:00 +00:00
|
|
|
|
2023-04-25 23:17:11 +00:00
|
|
|
from .const import _LOGGER, DOMAIN, UOM_8_BIT_RANGE
|
2020-05-05 04:03:12 +00:00
|
|
|
from .entity import ISYNodeEntity, ISYProgramEntity
|
2023-08-27 10:33:00 +00:00
|
|
|
from .models import IsyData
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-12 07:49:46 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2023-01-03 00:22:40 +00:00
|
|
|
"""Set up the ISY cover platform."""
|
2023-08-27 10:33:00 +00:00
|
|
|
isy_data: IsyData = hass.data[DOMAIN][entry.entry_id]
|
2023-01-12 23:09:04 +00:00
|
|
|
devices: dict[str, DeviceInfo] = isy_data.devices
|
2024-03-13 15:56:33 +00:00
|
|
|
entities: list[ISYCoverEntity | ISYCoverProgramEntity] = [
|
|
|
|
ISYCoverEntity(node, devices.get(node.primary_node))
|
|
|
|
for node in isy_data.nodes[Platform.COVER]
|
|
|
|
]
|
|
|
|
|
|
|
|
entities.extend(
|
|
|
|
ISYCoverProgramEntity(name, status, actions)
|
|
|
|
for name, status, actions in isy_data.programs[Platform.COVER]
|
|
|
|
)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2022-02-03 16:02:05 +00:00
|
|
|
async_add_entities(entities)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
class ISYCoverEntity(ISYNodeEntity, CoverEntity):
|
2023-01-03 00:22:40 +00:00
|
|
|
"""Representation of an ISY cover device."""
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2022-04-07 07:35:27 +00:00
|
|
|
_attr_supported_features = (
|
|
|
|
CoverEntityFeature.OPEN
|
|
|
|
| CoverEntityFeature.CLOSE
|
|
|
|
| CoverEntityFeature.SET_POSITION
|
|
|
|
)
|
|
|
|
|
2016-09-11 18:18:53 +00:00
|
|
|
@property
|
2022-02-03 16:02:05 +00:00
|
|
|
def current_cover_position(self) -> int | None:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Return the current cover position."""
|
2020-05-12 02:32:19 +00:00
|
|
|
if self._node.status == ISY_VALUE_UNKNOWN:
|
|
|
|
return None
|
2020-05-28 13:51:56 +00:00
|
|
|
if self._node.uom == UOM_8_BIT_RANGE:
|
2022-11-21 17:07:30 +00:00
|
|
|
return round(cast(float, self._node.status) * 100.0 / 255.0)
|
2022-02-03 16:02:05 +00:00
|
|
|
return int(sorted((0, self._node.status, 100))[1])
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
@property
|
2022-02-03 16:02:05 +00:00
|
|
|
def is_closed(self) -> bool | None:
|
2023-01-03 00:22:40 +00:00
|
|
|
"""Get whether the ISY cover device is closed."""
|
2020-05-12 02:32:19 +00:00
|
|
|
if self._node.status == ISY_VALUE_UNKNOWN:
|
|
|
|
return None
|
2022-02-03 16:02:05 +00:00
|
|
|
return bool(self._node.status == 0)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2022-02-03 16:02:05 +00:00
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
2023-01-03 00:22:40 +00:00
|
|
|
"""Send the open cover command to the ISY cover device."""
|
2023-04-25 23:17:11 +00:00
|
|
|
if not await self._node.turn_on():
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Unable to open the cover")
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2022-02-03 16:02:05 +00:00
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
2023-01-03 00:22:40 +00:00
|
|
|
"""Send the close cover command to the ISY cover device."""
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._node.turn_off():
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Unable to close the cover")
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2022-02-03 16:02:05 +00:00
|
|
|
async def async_set_cover_position(self, **kwargs: Any) -> None:
|
2020-05-28 13:51:56 +00:00
|
|
|
"""Move the cover to a specific position."""
|
2020-06-04 22:22:35 +00:00
|
|
|
position = kwargs[ATTR_POSITION]
|
|
|
|
if self._node.uom == UOM_8_BIT_RANGE:
|
2020-11-06 20:07:37 +00:00
|
|
|
position = round(position * 255.0 / 100.0)
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._node.turn_on(val=position):
|
2020-05-28 13:51:56 +00:00
|
|
|
_LOGGER.error("Unable to set cover position")
|
|
|
|
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
class ISYCoverProgramEntity(ISYProgramEntity, CoverEntity):
|
2023-01-03 00:22:40 +00:00
|
|
|
"""Representation of an ISY cover program."""
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
@property
|
2020-05-12 02:32:19 +00:00
|
|
|
def is_closed(self) -> bool:
|
2023-01-03 00:22:40 +00:00
|
|
|
"""Get whether the ISY cover program is closed."""
|
2020-05-12 02:32:19 +00:00
|
|
|
return bool(self._node.status)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2022-02-03 16:02:05 +00:00
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
2023-01-03 00:22:40 +00:00
|
|
|
"""Send the open cover command to the ISY cover program."""
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._actions.run_then():
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Unable to open the cover")
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2022-02-03 16:02:05 +00:00
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
2023-01-03 00:22:40 +00:00
|
|
|
"""Send the close cover command to the ISY cover program."""
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._actions.run_else():
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Unable to close the cover")
|