mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Add favorite position buttons to Motion Blinds (#123489)
This commit is contained in:
parent
ae6ac31d02
commit
bba298a44d
71
homeassistant/components/motion_blinds/button.py
Normal file
71
homeassistant/components/motion_blinds/button.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
"""Support for Motionblinds button entity using their WLAN API."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from motionblinds.motion_blinds import LimitStatus, MotionBlind
|
||||||
|
|
||||||
|
from homeassistant.components.button import ButtonEntity
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.const import EntityCategory
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from .const import DOMAIN, KEY_COORDINATOR, KEY_GATEWAY
|
||||||
|
from .coordinator import DataUpdateCoordinatorMotionBlinds
|
||||||
|
from .entity import MotionCoordinatorEntity
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: ConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Perform the setup for Motionblinds."""
|
||||||
|
entities: list[ButtonEntity] = []
|
||||||
|
motion_gateway = hass.data[DOMAIN][config_entry.entry_id][KEY_GATEWAY]
|
||||||
|
coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR]
|
||||||
|
|
||||||
|
for blind in motion_gateway.device_list.values():
|
||||||
|
if blind.limit_status == LimitStatus.Limit3Detected.name:
|
||||||
|
entities.append(MotionGoFavoriteButton(coordinator, blind))
|
||||||
|
entities.append(MotionSetFavoriteButton(coordinator, blind))
|
||||||
|
|
||||||
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
|
class MotionGoFavoriteButton(MotionCoordinatorEntity, ButtonEntity):
|
||||||
|
"""Button entity to go to the favorite position of a blind."""
|
||||||
|
|
||||||
|
_attr_translation_key = "go_favorite"
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, coordinator: DataUpdateCoordinatorMotionBlinds, blind: MotionBlind
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the Motion Button."""
|
||||||
|
super().__init__(coordinator, blind)
|
||||||
|
self._attr_unique_id = f"{blind.mac}-go-favorite"
|
||||||
|
|
||||||
|
async def async_press(self) -> None:
|
||||||
|
"""Execute the button action."""
|
||||||
|
async with self._api_lock:
|
||||||
|
await self.hass.async_add_executor_job(self._blind.Go_favorite_position)
|
||||||
|
await self.async_request_position_till_stop()
|
||||||
|
|
||||||
|
|
||||||
|
class MotionSetFavoriteButton(MotionCoordinatorEntity, ButtonEntity):
|
||||||
|
"""Button entity to set the favorite position of a blind to the current position."""
|
||||||
|
|
||||||
|
_attr_entity_category = EntityCategory.CONFIG
|
||||||
|
_attr_translation_key = "set_favorite"
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, coordinator: DataUpdateCoordinatorMotionBlinds, blind: MotionBlind
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the Motion Button."""
|
||||||
|
super().__init__(coordinator, blind)
|
||||||
|
self._attr_unique_id = f"{blind.mac}-set-favorite"
|
||||||
|
|
||||||
|
async def async_press(self) -> None:
|
||||||
|
"""Execute the button action."""
|
||||||
|
async with self._api_lock:
|
||||||
|
await self.hass.async_add_executor_job(self._blind.Set_favorite_position)
|
@ -6,7 +6,7 @@ DOMAIN = "motion_blinds"
|
|||||||
MANUFACTURER = "Motionblinds, Coulisse B.V."
|
MANUFACTURER = "Motionblinds, Coulisse B.V."
|
||||||
DEFAULT_GATEWAY_NAME = "Motionblinds Gateway"
|
DEFAULT_GATEWAY_NAME = "Motionblinds Gateway"
|
||||||
|
|
||||||
PLATFORMS = [Platform.COVER, Platform.SENSOR]
|
PLATFORMS = [Platform.BUTTON, Platform.COVER, Platform.SENSOR]
|
||||||
|
|
||||||
CONF_WAIT_FOR_PUSH = "wait_for_push"
|
CONF_WAIT_FOR_PUSH = "wait_for_push"
|
||||||
CONF_INTERFACE = "interface"
|
CONF_INTERFACE = "interface"
|
||||||
|
@ -5,7 +5,7 @@ from __future__ import annotations
|
|||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from motionblinds import DEVICE_TYPES_WIFI, BlindType
|
from motionblinds import BlindType
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.cover import (
|
from homeassistant.components.cover import (
|
||||||
@ -16,10 +16,9 @@ from homeassistant.components.cover import (
|
|||||||
CoverEntityFeature,
|
CoverEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import config_validation as cv, entity_platform
|
from homeassistant.helpers import config_validation as cv, entity_platform
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.event import async_call_later
|
|
||||||
from homeassistant.helpers.typing import VolDictType
|
from homeassistant.helpers.typing import VolDictType
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -31,8 +30,6 @@ from .const import (
|
|||||||
KEY_GATEWAY,
|
KEY_GATEWAY,
|
||||||
SERVICE_SET_ABSOLUTE_POSITION,
|
SERVICE_SET_ABSOLUTE_POSITION,
|
||||||
UPDATE_DELAY_STOP,
|
UPDATE_DELAY_STOP,
|
||||||
UPDATE_INTERVAL_MOVING,
|
|
||||||
UPDATE_INTERVAL_MOVING_WIFI,
|
|
||||||
)
|
)
|
||||||
from .entity import MotionCoordinatorEntity
|
from .entity import MotionCoordinatorEntity
|
||||||
|
|
||||||
@ -179,14 +176,6 @@ class MotionBaseDevice(MotionCoordinatorEntity, CoverEntity):
|
|||||||
"""Initialize the blind."""
|
"""Initialize the blind."""
|
||||||
super().__init__(coordinator, blind)
|
super().__init__(coordinator, blind)
|
||||||
|
|
||||||
self._requesting_position: CALLBACK_TYPE | None = None
|
|
||||||
self._previous_positions = []
|
|
||||||
|
|
||||||
if blind.device_type in DEVICE_TYPES_WIFI:
|
|
||||||
self._update_interval_moving = UPDATE_INTERVAL_MOVING_WIFI
|
|
||||||
else:
|
|
||||||
self._update_interval_moving = UPDATE_INTERVAL_MOVING
|
|
||||||
|
|
||||||
self._attr_device_class = device_class
|
self._attr_device_class = device_class
|
||||||
self._attr_unique_id = blind.mac
|
self._attr_unique_id = blind.mac
|
||||||
|
|
||||||
@ -218,47 +207,6 @@ class MotionBaseDevice(MotionCoordinatorEntity, CoverEntity):
|
|||||||
return None
|
return None
|
||||||
return self._blind.position == 100
|
return self._blind.position == 100
|
||||||
|
|
||||||
async def async_scheduled_update_request(self, *_):
|
|
||||||
"""Request a state update from the blind at a scheduled point in time."""
|
|
||||||
# add the last position to the list and keep the list at max 2 items
|
|
||||||
self._previous_positions.append(self.current_cover_position)
|
|
||||||
if len(self._previous_positions) > 2:
|
|
||||||
del self._previous_positions[: len(self._previous_positions) - 2]
|
|
||||||
|
|
||||||
async with self._api_lock:
|
|
||||||
await self.hass.async_add_executor_job(self._blind.Update_trigger)
|
|
||||||
|
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|
||||||
if len(self._previous_positions) < 2 or not all(
|
|
||||||
self.current_cover_position == prev_position
|
|
||||||
for prev_position in self._previous_positions
|
|
||||||
):
|
|
||||||
# keep updating the position @self._update_interval_moving until the position does not change.
|
|
||||||
self._requesting_position = async_call_later(
|
|
||||||
self.hass,
|
|
||||||
self._update_interval_moving,
|
|
||||||
self.async_scheduled_update_request,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
self._previous_positions = []
|
|
||||||
self._requesting_position = None
|
|
||||||
|
|
||||||
async def async_request_position_till_stop(self, delay=None):
|
|
||||||
"""Request the position of the blind every self._update_interval_moving seconds until it stops moving."""
|
|
||||||
if delay is None:
|
|
||||||
delay = self._update_interval_moving
|
|
||||||
|
|
||||||
self._previous_positions = []
|
|
||||||
if self.current_cover_position is None:
|
|
||||||
return
|
|
||||||
if self._requesting_position is not None:
|
|
||||||
self._requesting_position()
|
|
||||||
|
|
||||||
self._requesting_position = async_call_later(
|
|
||||||
self.hass, delay, self.async_scheduled_update_request
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_open_cover(self, **kwargs: Any) -> None:
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
||||||
"""Open the cover."""
|
"""Open the cover."""
|
||||||
async with self._api_lock:
|
async with self._api_lock:
|
||||||
|
@ -5,8 +5,10 @@ from __future__ import annotations
|
|||||||
from motionblinds import DEVICE_TYPES_GATEWAY, DEVICE_TYPES_WIFI, MotionGateway
|
from motionblinds import DEVICE_TYPES_GATEWAY, DEVICE_TYPES_WIFI, MotionGateway
|
||||||
from motionblinds.motion_blinds import MotionBlind
|
from motionblinds.motion_blinds import MotionBlind
|
||||||
|
|
||||||
|
from homeassistant.core import CALLBACK_TYPE
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
|
from homeassistant.helpers.event import async_call_later
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -15,6 +17,8 @@ from .const import (
|
|||||||
DOMAIN,
|
DOMAIN,
|
||||||
KEY_GATEWAY,
|
KEY_GATEWAY,
|
||||||
MANUFACTURER,
|
MANUFACTURER,
|
||||||
|
UPDATE_INTERVAL_MOVING,
|
||||||
|
UPDATE_INTERVAL_MOVING_WIFI,
|
||||||
)
|
)
|
||||||
from .coordinator import DataUpdateCoordinatorMotionBlinds
|
from .coordinator import DataUpdateCoordinatorMotionBlinds
|
||||||
from .gateway import device_name
|
from .gateway import device_name
|
||||||
@ -36,6 +40,14 @@ class MotionCoordinatorEntity(CoordinatorEntity[DataUpdateCoordinatorMotionBlind
|
|||||||
self._blind = blind
|
self._blind = blind
|
||||||
self._api_lock = coordinator.api_lock
|
self._api_lock = coordinator.api_lock
|
||||||
|
|
||||||
|
self._requesting_position: CALLBACK_TYPE | None = None
|
||||||
|
self._previous_positions: list[int | dict | None] = []
|
||||||
|
|
||||||
|
if blind.device_type in DEVICE_TYPES_WIFI:
|
||||||
|
self._update_interval_moving = UPDATE_INTERVAL_MOVING_WIFI
|
||||||
|
else:
|
||||||
|
self._update_interval_moving = UPDATE_INTERVAL_MOVING
|
||||||
|
|
||||||
if blind.device_type in DEVICE_TYPES_GATEWAY:
|
if blind.device_type in DEVICE_TYPES_GATEWAY:
|
||||||
gateway = blind
|
gateway = blind
|
||||||
else:
|
else:
|
||||||
@ -95,3 +107,44 @@ class MotionCoordinatorEntity(CoordinatorEntity[DataUpdateCoordinatorMotionBlind
|
|||||||
"""Unsubscribe when removed."""
|
"""Unsubscribe when removed."""
|
||||||
self._blind.Remove_callback(self.unique_id)
|
self._blind.Remove_callback(self.unique_id)
|
||||||
await super().async_will_remove_from_hass()
|
await super().async_will_remove_from_hass()
|
||||||
|
|
||||||
|
async def async_scheduled_update_request(self, *_) -> None:
|
||||||
|
"""Request a state update from the blind at a scheduled point in time."""
|
||||||
|
# add the last position to the list and keep the list at max 2 items
|
||||||
|
self._previous_positions.append(self._blind.position)
|
||||||
|
if len(self._previous_positions) > 2:
|
||||||
|
del self._previous_positions[: len(self._previous_positions) - 2]
|
||||||
|
|
||||||
|
async with self._api_lock:
|
||||||
|
await self.hass.async_add_executor_job(self._blind.Update_trigger)
|
||||||
|
|
||||||
|
self.coordinator.async_update_listeners()
|
||||||
|
|
||||||
|
if len(self._previous_positions) < 2 or not all(
|
||||||
|
self._blind.position == prev_position
|
||||||
|
for prev_position in self._previous_positions
|
||||||
|
):
|
||||||
|
# keep updating the position @self._update_interval_moving until the position does not change.
|
||||||
|
self._requesting_position = async_call_later(
|
||||||
|
self.hass,
|
||||||
|
self._update_interval_moving,
|
||||||
|
self.async_scheduled_update_request,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self._previous_positions = []
|
||||||
|
self._requesting_position = None
|
||||||
|
|
||||||
|
async def async_request_position_till_stop(self, delay: int | None = None) -> None:
|
||||||
|
"""Request the position of the blind every self._update_interval_moving seconds until it stops moving."""
|
||||||
|
if delay is None:
|
||||||
|
delay = self._update_interval_moving
|
||||||
|
|
||||||
|
self._previous_positions = []
|
||||||
|
if self._blind.position is None:
|
||||||
|
return
|
||||||
|
if self._requesting_position is not None:
|
||||||
|
self._requesting_position()
|
||||||
|
|
||||||
|
self._requesting_position = async_call_later(
|
||||||
|
self.hass, delay, self.async_scheduled_update_request
|
||||||
|
)
|
||||||
|
@ -1,4 +1,14 @@
|
|||||||
{
|
{
|
||||||
|
"entity": {
|
||||||
|
"button": {
|
||||||
|
"go_favorite": {
|
||||||
|
"default": "mdi:star"
|
||||||
|
},
|
||||||
|
"set_favorite": {
|
||||||
|
"default": "mdi:star-cog"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"services": {
|
"services": {
|
||||||
"set_absolute_position": "mdi:set-square"
|
"set_absolute_position": "mdi:set-square"
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,14 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"entity": {
|
"entity": {
|
||||||
|
"button": {
|
||||||
|
"go_favorite": {
|
||||||
|
"name": "Go to favorite position"
|
||||||
|
},
|
||||||
|
"set_favorite": {
|
||||||
|
"name": "Set current position as favorite"
|
||||||
|
}
|
||||||
|
},
|
||||||
"cover": {
|
"cover": {
|
||||||
"top": {
|
"top": {
|
||||||
"name": "Top"
|
"name": "Top"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user