1 """Cover entities for the Motionblinds Bluetooth integration."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
9 from motionblindsble.const
import MotionBlindType, MotionRunningType
10 from motionblindsble.device
import MotionDevice
17 CoverEntityDescription,
24 from .const
import CONF_BLIND_TYPE, CONF_MAC_CODE, DOMAIN, ICON_VERTICAL_BLIND
25 from .entity
import MotionblindsBLEEntity
27 _LOGGER = logging.getLogger(__name__)
30 @dataclass(frozen=True, kw_only=True)
32 """Entity description of a cover entity with default values."""
34 key: str = CoverDeviceClass.BLIND.value
35 translation_key: str = CoverDeviceClass.BLIND.value
39 device_class=CoverDeviceClass.SHADE
42 device_class=CoverDeviceClass.BLIND
45 device_class=CoverDeviceClass.CURTAIN
48 device_class=CoverDeviceClass.CURTAIN, icon=ICON_VERTICAL_BLIND
51 BLIND_TYPE_TO_ENTITY_DESCRIPTION: dict[str, MotionblindsBLECoverEntityDescription] = {
52 MotionBlindType.HONEYCOMB.name: SHADE_ENTITY_DESCRIPTION,
53 MotionBlindType.ROMAN.name: SHADE_ENTITY_DESCRIPTION,
54 MotionBlindType.ROLLER.name: SHADE_ENTITY_DESCRIPTION,
55 MotionBlindType.DOUBLE_ROLLER.name: SHADE_ENTITY_DESCRIPTION,
56 MotionBlindType.VENETIAN.name: BLIND_ENTITY_DESCRIPTION,
57 MotionBlindType.VENETIAN_TILT_ONLY.name: BLIND_ENTITY_DESCRIPTION,
58 MotionBlindType.CURTAIN.name: CURTAIN_ENTITY_DESCRIPTION,
59 MotionBlindType.VERTICAL.name: VERTICAL_ENTITY_DESCRIPTION,
64 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
66 """Set up cover entity based on a config entry."""
68 cover_class: type[MotionblindsBLECoverEntity] = BLIND_TYPE_TO_CLASS[
69 entry.data[CONF_BLIND_TYPE].upper()
71 device: MotionDevice = hass.data[DOMAIN][entry.entry_id]
72 entity_description: MotionblindsBLECoverEntityDescription = (
73 BLIND_TYPE_TO_ENTITY_DESCRIPTION[entry.data[CONF_BLIND_TYPE].upper()]
75 entity: MotionblindsBLECoverEntity = cover_class(device, entry, entity_description)
81 """Representation of a cover entity."""
83 _attr_is_closed: bool |
None =
None
87 """Register device callbacks."""
89 "(%s) Added %s cover entity (%s)",
90 self.
entryentry.data[CONF_MAC_CODE],
91 MotionBlindType[self.
entryentry.data[CONF_BLIND_TYPE].upper()].value.lower(),
92 BLIND_TYPE_TO_CLASS[self.
entryentry.data[CONF_BLIND_TYPE].upper()].__name__,
98 """Stop moving the cover entity."""
99 _LOGGER.debug(
"(%s) Stopping", self.
entryentry.data[CONF_MAC_CODE])
100 await self.
devicedevice.stop()
104 self, running_type: MotionRunningType |
None, write_state: bool =
True
106 """Update the running type (e.g. opening/closing) of the cover entity."""
107 if running_type
in {
None, MotionRunningType.STILL, MotionRunningType.UNKNOWN}:
111 self.
_attr_is_opening_attr_is_opening = running_type
is MotionRunningType.OPENING
112 self.
_attr_is_closing_attr_is_closing = running_type
is not MotionRunningType.OPENING
113 if running_type
is not MotionRunningType.STILL:
121 position: int |
None,
124 """Update the position of the cover entity."""
139 """Representation of a cover entity with position capability."""
141 _attr_supported_features = (
142 CoverEntityFeature.OPEN
143 | CoverEntityFeature.CLOSE
144 | CoverEntityFeature.STOP
145 | CoverEntityFeature.SET_POSITION
149 """Open the cover entity."""
150 _LOGGER.debug(
"(%s) Opening", self.
entryentry.data[CONF_MAC_CODE])
154 """Close the cover entity."""
155 _LOGGER.debug(
"(%s) Closing", self.
entryentry.data[CONF_MAC_CODE])
156 await self.
devicedevice.close()
159 """Move the cover entity to a specific position."""
160 new_position: int = 100 -
int(kwargs[ATTR_POSITION])
163 "(%s) Setting position to %i",
164 self.
entryentry.data[CONF_MAC_CODE],
167 await self.
devicedevice.position(new_position)
171 """Representation of a cover entity with tilt capability."""
173 _attr_supported_features = (
174 CoverEntityFeature.OPEN_TILT
175 | CoverEntityFeature.CLOSE_TILT
176 | CoverEntityFeature.STOP_TILT
177 | CoverEntityFeature.SET_TILT_POSITION
181 """Tilt the cover entity open."""
182 _LOGGER.debug(
"(%s) Tilt opening", self.
entryentry.data[CONF_MAC_CODE])
183 await self.
devicedevice.open_tilt()
186 """Tilt the cover entity closed."""
187 _LOGGER.debug(
"(%s) Tilt closing", self.
entryentry.data[CONF_MAC_CODE])
188 await self.
devicedevice.close_tilt()
191 """Stop tilting the cover entity."""
195 """Tilt the cover entity to a specific position."""
196 new_tilt: int = 100 -
int(kwargs[ATTR_TILT_POSITION])
199 "(%s) Setting tilt position to %i",
200 self.
entryentry.data[CONF_MAC_CODE],
203 await self.
devicedevice.tilt(round(180 * new_tilt / 100))
207 """Representation of a cover entity with position & tilt capabilities."""
209 _attr_supported_features = (
210 CoverEntityFeature.OPEN
211 | CoverEntityFeature.CLOSE
212 | CoverEntityFeature.STOP
213 | CoverEntityFeature.SET_POSITION
214 | CoverEntityFeature.OPEN_TILT
215 | CoverEntityFeature.CLOSE_TILT
216 | CoverEntityFeature.STOP_TILT
217 | CoverEntityFeature.SET_TILT_POSITION
221 BLIND_TYPE_TO_CLASS: dict[str, type[MotionblindsBLECoverEntity]] = {
222 MotionBlindType.ROLLER.name: PositionCover,
223 MotionBlindType.HONEYCOMB.name: PositionCover,
224 MotionBlindType.ROMAN.name: PositionCover,
225 MotionBlindType.VENETIAN.name: PositionTiltCover,
226 MotionBlindType.VENETIAN_TILT_ONLY.name: TiltCover,
227 MotionBlindType.DOUBLE_ROLLER.name: PositionTiltCover,
228 MotionBlindType.CURTAIN.name: PositionCover,
229 MotionBlindType.VERTICAL.name: PositionTiltCover,
None async_stop_cover(self, **Any kwargs)
None async_stop_cover(self, **Any kwargs)
None async_update_position(self, int|None position, int|None tilt)
_attr_current_cover_position
None async_added_to_hass(self)
_attr_current_cover_tilt_position
None async_update_running(self, MotionRunningType|None running_type, bool write_state=True)
None async_close_cover(self, **Any kwargs)
None async_open_cover(self, **Any kwargs)
None async_set_cover_position(self, **Any kwargs)
None async_open_cover_tilt(self, **Any kwargs)
None async_stop_cover_tilt(self, **Any kwargs)
None async_set_cover_tilt_position(self, **Any kwargs)
None async_close_cover_tilt(self, **Any kwargs)
None async_write_ha_state(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
None open(self, **Any kwargs)