mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add action enums to blackbird (#190028)
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
"""Constants for the Monoprice Blackbird Matrix Switch component."""
|
||||
|
||||
DOMAIN = "blackbird"
|
||||
SERVICE_SETALLZONES = "set_all_zones"
|
||||
|
||||
DATA_BLACKBIRD = "blackbird"
|
||||
|
||||
@@ -1,26 +1,39 @@
|
||||
"""Services for the Monoprice Blackbird Matrix Switch integration."""
|
||||
|
||||
from enum import StrEnum
|
||||
|
||||
import probatio
|
||||
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
from .const import DATA_BLACKBIRD, DOMAIN, SERVICE_SETALLZONES
|
||||
from .const import DATA_BLACKBIRD, DOMAIN
|
||||
|
||||
|
||||
class BlackbirdService(StrEnum):
|
||||
"""Store keys for Blackbird services."""
|
||||
|
||||
SET_ALL_ZONES = "set_all_zones"
|
||||
|
||||
|
||||
class BlackbirdServiceArgument(StrEnum):
|
||||
"""Store keys for Blackbird service arguments."""
|
||||
|
||||
SOURCE = "source"
|
||||
|
||||
|
||||
MEDIA_PLAYER_SCHEMA = probatio.Schema({ATTR_ENTITY_ID: cv.comp_entity_ids})
|
||||
|
||||
ATTR_SOURCE = "source"
|
||||
|
||||
BLACKBIRD_SETALLZONES_SCHEMA = MEDIA_PLAYER_SCHEMA.extend(
|
||||
{probatio.Required(ATTR_SOURCE): cv.string}
|
||||
{probatio.Required(BlackbirdServiceArgument.SOURCE): cv.string}
|
||||
)
|
||||
|
||||
|
||||
def _service_handle(service: ServiceCall) -> None:
|
||||
"""Handle for services."""
|
||||
entity_ids = service.data.get(ATTR_ENTITY_ID)
|
||||
source = service.data.get(ATTR_SOURCE)
|
||||
source = service.data.get(BlackbirdServiceArgument.SOURCE)
|
||||
zones = service.hass.data.get(DATA_BLACKBIRD, {})
|
||||
if entity_ids:
|
||||
devices = [
|
||||
@@ -31,7 +44,7 @@ def _service_handle(service: ServiceCall) -> None:
|
||||
devices = zones.values()
|
||||
|
||||
for device in devices:
|
||||
if service.service == SERVICE_SETALLZONES:
|
||||
if service.service == BlackbirdService.SET_ALL_ZONES:
|
||||
device.set_all_zones(source)
|
||||
|
||||
|
||||
@@ -39,7 +52,7 @@ def setup_services(hass: HomeAssistant) -> None:
|
||||
"""Register the blackbird services."""
|
||||
hass.services.register(
|
||||
DOMAIN,
|
||||
SERVICE_SETALLZONES,
|
||||
BlackbirdService.SET_ALL_ZONES,
|
||||
_service_handle,
|
||||
schema=BLACKBIRD_SETALLZONES_SCHEMA,
|
||||
)
|
||||
|
||||
@@ -7,12 +7,9 @@ import probatio
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockEntityPlatform
|
||||
|
||||
from homeassistant.components.blackbird.const import (
|
||||
DATA_BLACKBIRD,
|
||||
DOMAIN,
|
||||
SERVICE_SETALLZONES,
|
||||
)
|
||||
from homeassistant.components.blackbird.const import DATA_BLACKBIRD, DOMAIN
|
||||
from homeassistant.components.blackbird.media_player import PLATFORM_SCHEMA
|
||||
from homeassistant.components.blackbird.services import BlackbirdService
|
||||
from homeassistant.components.media_player import (
|
||||
MediaPlayerEntity,
|
||||
MediaPlayerEntityFeature,
|
||||
@@ -218,7 +215,7 @@ def media_player_entity(
|
||||
async def test_setup_platform(hass: HomeAssistant) -> None:
|
||||
"""Test setting up platform."""
|
||||
# One service must be registered
|
||||
assert hass.services.has_service(DOMAIN, SERVICE_SETALLZONES)
|
||||
assert hass.services.has_service(DOMAIN, BlackbirdService.SET_ALL_ZONES)
|
||||
assert len(hass.data[DATA_BLACKBIRD]) == 1
|
||||
assert hass.data[DATA_BLACKBIRD]["/dev/ttyUSB0-3"].name == "Zone name"
|
||||
|
||||
@@ -237,7 +234,7 @@ async def test_setallzones_service_call_with_entity_id(
|
||||
# Call set all zones service
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SETALLZONES,
|
||||
BlackbirdService.SET_ALL_ZONES,
|
||||
{"entity_id": "media_player.zone_3", "source": "three"},
|
||||
blocking=True,
|
||||
)
|
||||
@@ -261,7 +258,7 @@ async def test_setallzones_service_call_without_entity_id(
|
||||
|
||||
# Call set all zones service
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_SETALLZONES, {"source": "three"}, blocking=True
|
||||
DOMAIN, BlackbirdService.SET_ALL_ZONES, {"source": "three"}, blocking=True
|
||||
)
|
||||
|
||||
# Check that source was changed
|
||||
|
||||
Reference in New Issue
Block a user