diff --git a/.coveragerc b/.coveragerc index 1bee5f26b37..9da1e6fb0a7 100644 --- a/.coveragerc +++ b/.coveragerc @@ -809,6 +809,7 @@ omit = homeassistant/components/motionblinds_ble/__init__.py homeassistant/components/motionblinds_ble/cover.py homeassistant/components/motionblinds_ble/entity.py + homeassistant/components/motionblinds_ble/select.py homeassistant/components/motionmount/__init__.py homeassistant/components/motionmount/binary_sensor.py homeassistant/components/motionmount/entity.py diff --git a/homeassistant/components/motionblinds_ble/__init__.py b/homeassistant/components/motionblinds_ble/__init__.py index beef6d7d665..bb89b468a5b 100644 --- a/homeassistant/components/motionblinds_ble/__init__.py +++ b/homeassistant/components/motionblinds_ble/__init__.py @@ -28,9 +28,7 @@ from .const import CONF_BLIND_TYPE, CONF_MAC_CODE, DOMAIN _LOGGER = logging.getLogger(__name__) -PLATFORMS: list[Platform] = [ - Platform.COVER, -] +PLATFORMS: list[Platform] = [Platform.COVER, Platform.SELECT] CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) diff --git a/homeassistant/components/motionblinds_ble/const.py b/homeassistant/components/motionblinds_ble/const.py index 1b396dd544d..5feaf59845f 100644 --- a/homeassistant/components/motionblinds_ble/const.py +++ b/homeassistant/components/motionblinds_ble/const.py @@ -1,5 +1,7 @@ """Constants for the Motionblinds BLE integration.""" +ATTR_SPEED = "speed" + CONF_LOCAL_NAME = "local_name" CONF_MAC_CODE = "mac_code" CONF_BLIND_TYPE = "blind_type" diff --git a/homeassistant/components/motionblinds_ble/icons.json b/homeassistant/components/motionblinds_ble/icons.json new file mode 100644 index 00000000000..2ea620d3947 --- /dev/null +++ b/homeassistant/components/motionblinds_ble/icons.json @@ -0,0 +1,9 @@ +{ + "entity": { + "select": { + "speed": { + "default": "mdi:run-fast" + } + } + } +} diff --git a/homeassistant/components/motionblinds_ble/select.py b/homeassistant/components/motionblinds_ble/select.py new file mode 100644 index 00000000000..2ba2b8df2d4 --- /dev/null +++ b/homeassistant/components/motionblinds_ble/select.py @@ -0,0 +1,79 @@ +"""Select entities for the Motionblinds BLE integration.""" + +from __future__ import annotations + +import logging + +from motionblindsble.const import MotionBlindType, MotionSpeedLevel +from motionblindsble.device import MotionDevice + +from homeassistant.components.select import SelectEntity, SelectEntityDescription +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import ATTR_SPEED, CONF_MAC_CODE, DOMAIN +from .entity import MotionblindsBLEEntity + +_LOGGER = logging.getLogger(__name__) + +PARALLEL_UPDATES = 0 + + +SELECT_TYPES: dict[str, SelectEntityDescription] = { + ATTR_SPEED: SelectEntityDescription( + key=ATTR_SPEED, + translation_key=ATTR_SPEED, + entity_category=EntityCategory.CONFIG, + options=["1", "2", "3"], + ) +} + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + """Set up select entities based on a config entry.""" + + device: MotionDevice = hass.data[DOMAIN][entry.entry_id] + + if device.blind_type not in {MotionBlindType.CURTAIN, MotionBlindType.VERTICAL}: + async_add_entities([SpeedSelect(device, entry, SELECT_TYPES[ATTR_SPEED])]) + + +class SpeedSelect(MotionblindsBLEEntity, SelectEntity): + """Representation of a speed select entity.""" + + def __init__( + self, + device: MotionDevice, + entry: ConfigEntry, + entity_description: SelectEntityDescription, + ) -> None: + """Initialize the speed select entity.""" + super().__init__( + device, entry, entity_description, unique_id_suffix=entity_description.key + ) + self._attr_current_option = None + + async def async_added_to_hass(self) -> None: + """Register device callbacks.""" + _LOGGER.debug( + "(%s) Setting up speed select entity", + self.entry.data[CONF_MAC_CODE], + ) + self.device.register_speed_callback(self.async_update_speed) + + @callback + def async_update_speed(self, speed_level: MotionSpeedLevel | None) -> None: + """Update the speed sensor value.""" + self._attr_current_option = str(speed_level.value) if speed_level else None + self.async_write_ha_state() + + async def async_select_option(self, option: str) -> None: + """Change the selected speed sensor value.""" + speed_level = MotionSpeedLevel(int(option)) + await self.device.speed(speed_level) + self._attr_current_option = str(speed_level.value) if speed_level else None + self.async_write_ha_state() diff --git a/homeassistant/components/motionblinds_ble/strings.json b/homeassistant/components/motionblinds_ble/strings.json index e876d64d568..bcc06eeb181 100644 --- a/homeassistant/components/motionblinds_ble/strings.json +++ b/homeassistant/components/motionblinds_ble/strings.json @@ -33,5 +33,17 @@ "vertical": "Vertical blind" } } + }, + "entity": { + "select": { + "speed": { + "name": "Speed", + "state": { + "1": "Low", + "2": "Medium", + "3": "High" + } + } + } } }