From f8274cd5c2f59f43fd7828c96568a309043182ba Mon Sep 17 00:00:00 2001 From: cnico Date: Wed, 18 Sep 2024 23:04:22 +0200 Subject: [PATCH] Addition of select platform for flipr hub (#126237) * Addition of select platform for flipr hub * Review corrections --- homeassistant/components/flipr/__init__.py | 2 +- homeassistant/components/flipr/select.py | 56 ++++++++++ homeassistant/components/flipr/strings.json | 10 ++ tests/components/flipr/test_select.py | 109 ++++++++++++++++++++ 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/flipr/select.py create mode 100644 tests/components/flipr/test_select.py diff --git a/homeassistant/components/flipr/__init__.py b/homeassistant/components/flipr/__init__.py index e775171bf06..99bddb5a0d0 100644 --- a/homeassistant/components/flipr/__init__.py +++ b/homeassistant/components/flipr/__init__.py @@ -15,7 +15,7 @@ from homeassistant.helpers import issue_registry as ir from .const import DOMAIN from .coordinator import FliprDataUpdateCoordinator, FliprHubDataUpdateCoordinator -PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH] +PLATFORMS = [Platform.BINARY_SENSOR, Platform.SELECT, Platform.SENSOR, Platform.SWITCH] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/flipr/select.py b/homeassistant/components/flipr/select.py new file mode 100644 index 00000000000..b8a8f0db60a --- /dev/null +++ b/homeassistant/components/flipr/select.py @@ -0,0 +1,56 @@ +"""Select platform for the Flipr's Hub.""" + +import logging + +from homeassistant.components.select import SelectEntity, SelectEntityDescription +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import FliprConfigEntry +from .entity import FliprEntity + +_LOGGER = logging.getLogger(__name__) + +SELECT_TYPES: tuple[SelectEntityDescription, ...] = ( + SelectEntityDescription( + key="hubMode", + translation_key="hub_mode", + options=["auto", "manual", "planning"], + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: FliprConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up select for Flipr hub mode.""" + coordinators = config_entry.runtime_data.hub_coordinators + + async_add_entities( + FliprHubSelect(coordinator, description, True) + for description in SELECT_TYPES + for coordinator in coordinators + ) + + +class FliprHubSelect(FliprEntity, SelectEntity): + """Select representing Hub mode.""" + + @property + def current_option(self) -> str | None: + """Return current select option.""" + _LOGGER.debug("coordinator data = %s", self.coordinator.data) + return self.coordinator.data["mode"] + + async def async_select_option(self, option: str) -> None: + """Select new mode for Hub.""" + _LOGGER.debug("Changing mode of %s to %s", self.device_id, option) + data = await self.hass.async_add_executor_job( + self.coordinator.client.set_hub_mode, + self.device_id, + option, + ) + _LOGGER.debug("New hub infos are %s", data) + self.coordinator.async_set_updated_data(data) diff --git a/homeassistant/components/flipr/strings.json b/homeassistant/components/flipr/strings.json index 8eebb62cb5c..631b0ce5488 100644 --- a/homeassistant/components/flipr/strings.json +++ b/homeassistant/components/flipr/strings.json @@ -39,6 +39,16 @@ "red_ox": { "name": "Red OX" } + }, + "select": { + "hub_mode": { + "name": "Mode", + "state": { + "auto": "Automatic", + "manual": "Manual", + "planning": "Planning" + } + } } }, "issues": { diff --git a/tests/components/flipr/test_select.py b/tests/components/flipr/test_select.py new file mode 100644 index 00000000000..d71297f4f1a --- /dev/null +++ b/tests/components/flipr/test_select.py @@ -0,0 +1,109 @@ +"""Test the Flipr select for Hub.""" + +import logging +from unittest.mock import AsyncMock + +from flipr_api.exceptions import FliprError + +from homeassistant.components.select import ( + ATTR_OPTION, + ATTR_OPTIONS, + DOMAIN as SELECT_DOMAIN, + SERVICE_SELECT_OPTION, +) +from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import setup_integration + +from tests.common import MockConfigEntry + +_LOGGER = logging.getLogger(__name__) + +SELECT_ENTITY_ID = "select.flipr_hub_myhubid_mode" + + +async def test_entities( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, + mock_flipr_client: AsyncMock, +) -> None: + """Test the creation and values of the Flipr select.""" + + mock_flipr_client.search_all_ids.return_value = {"flipr": [], "hub": ["myhubid"]} + + await setup_integration(hass, mock_config_entry) + + # Check entity unique_id value that is generated in FliprEntity base class. + entity = entity_registry.async_get(SELECT_ENTITY_ID) + _LOGGER.debug("Found entity = %s", entity) + assert entity.unique_id == "myhubid-hubMode" + + mode = hass.states.get(SELECT_ENTITY_ID) + _LOGGER.debug("Found mode = %s", mode) + assert mode + assert mode.state == "planning" + assert mode.attributes.get(ATTR_OPTIONS) == ["auto", "manual", "planning"] + + +async def test_select_actions( + hass: HomeAssistant, + mock_flipr_client: AsyncMock, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test the actions on the Flipr Hub select.""" + + mock_flipr_client.search_all_ids.return_value = {"flipr": [], "hub": ["myhubid"]} + + await setup_integration(hass, mock_config_entry) + + state = hass.states.get(SELECT_ENTITY_ID) + assert state.state == "planning" + + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + {ATTR_ENTITY_ID: SELECT_ENTITY_ID, ATTR_OPTION: "manual"}, + blocking=True, + ) + state = hass.states.get(SELECT_ENTITY_ID) + assert state.state == "manual" + + +async def test_no_select_found( + hass: HomeAssistant, + mock_flipr_client: AsyncMock, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test the select absence.""" + + mock_flipr_client.search_all_ids.return_value = {"flipr": [], "hub": []} + + await setup_integration(hass, mock_config_entry) + + assert not hass.states.async_entity_ids(SELECT_ENTITY_ID) + + +async def test_error_flipr_api( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, + mock_flipr_client: AsyncMock, +) -> None: + """Test the Flipr sensors error.""" + + mock_flipr_client.search_all_ids.return_value = {"flipr": [], "hub": ["myhubid"]} + + mock_flipr_client.get_hub_state.side_effect = FliprError( + "Error during flipr data retrieval..." + ) + + await setup_integration(hass, mock_config_entry) + + # Check entity is not generated because of the FliprError raised. + entity = entity_registry.async_get(SELECT_ENTITY_ID) + assert entity is None