Addition of select platform for flipr hub (#126237)

* Addition of select platform for flipr hub

* Review corrections
This commit is contained in:
cnico 2024-09-18 23:04:22 +02:00 committed by GitHub
parent 931c8f9e66
commit f8274cd5c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 176 additions and 1 deletions

View File

@ -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__)

View File

@ -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)

View File

@ -39,6 +39,16 @@
"red_ox": {
"name": "Red OX"
}
},
"select": {
"hub_mode": {
"name": "Mode",
"state": {
"auto": "Automatic",
"manual": "Manual",
"planning": "Planning"
}
}
}
},
"issues": {

View File

@ -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