1 """Support for interfacing with Monoprice Blackbird 4k 8x8 HDBaseT Matrix."""
3 from __future__
import annotations
7 from pyblackbird
import get_blackbird
8 from serial
import SerialException
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as MEDIA_PLAYER_PLATFORM_SCHEMA,
14 MediaPlayerEntityFeature,
29 from .const
import DOMAIN, SERVICE_SETALLZONES
31 _LOGGER = logging.getLogger(__name__)
33 MEDIA_PLAYER_SCHEMA = vol.Schema({ATTR_ENTITY_ID: cv.comp_entity_ids})
35 ZONE_SCHEMA = vol.Schema({vol.Required(CONF_NAME): cv.string})
37 SOURCE_SCHEMA = vol.Schema({vol.Required(CONF_NAME): cv.string})
40 CONF_SOURCES =
"sources"
42 DATA_BLACKBIRD =
"blackbird"
44 ATTR_SOURCE =
"source"
46 BLACKBIRD_SETALLZONES_SCHEMA = MEDIA_PLAYER_SCHEMA.extend(
47 {vol.Required(ATTR_SOURCE): cv.string}
52 ZONE_IDS = vol.All(vol.Coerce(int), vol.Range(min=1, max=8))
55 SOURCE_IDS = vol.All(vol.Coerce(int), vol.Range(min=1, max=8))
57 PLATFORM_SCHEMA = vol.All(
58 cv.has_at_least_one_key(CONF_PORT, CONF_HOST),
59 MEDIA_PLAYER_PLATFORM_SCHEMA.extend(
61 vol.Exclusive(CONF_PORT, CONF_TYPE): cv.string,
62 vol.Exclusive(CONF_HOST, CONF_TYPE): cv.string,
63 vol.Required(CONF_ZONES): vol.Schema({ZONE_IDS: ZONE_SCHEMA}),
64 vol.Required(CONF_SOURCES): vol.Schema({SOURCE_IDS: SOURCE_SCHEMA}),
73 add_entities: AddEntitiesCallback,
74 discovery_info: DiscoveryInfoType |
None =
None,
76 """Set up the Monoprice Blackbird 4k 8x8 HDBaseT Matrix platform."""
77 if DATA_BLACKBIRD
not in hass.data:
78 hass.data[DATA_BLACKBIRD] = {}
80 port = config.get(CONF_PORT)
81 host = config.get(CONF_HOST)
86 blackbird = get_blackbird(port)
88 except SerialException:
89 _LOGGER.error(
"Error connecting to the Blackbird controller")
94 blackbird = get_blackbird(host,
False)
97 _LOGGER.error(
"Error connecting to the Blackbird controller")
101 source_id: extra[CONF_NAME]
for source_id, extra
in config[CONF_SOURCES].items()
105 for zone_id, extra
in config[CONF_ZONES].items():
106 _LOGGER.debug(
"Adding zone %d - %s", zone_id, extra[CONF_NAME])
107 unique_id = f
"{connection}-{zone_id}"
108 device =
BlackbirdZone(blackbird, sources, zone_id, extra[CONF_NAME])
109 hass.data[DATA_BLACKBIRD][unique_id] = device
110 devices.append(device)
115 """Handle for services."""
116 entity_ids = service.data.get(ATTR_ENTITY_ID)
117 source = service.data.get(ATTR_SOURCE)
121 for device
in hass.data[DATA_BLACKBIRD].values()
122 if device.entity_id
in entity_ids
126 devices = hass.data[DATA_BLACKBIRD].values()
128 for device
in devices:
129 if service.service == SERVICE_SETALLZONES:
130 device.set_all_zones(source)
132 hass.services.register(
133 DOMAIN, SERVICE_SETALLZONES, service_handle, schema=BLACKBIRD_SETALLZONES_SCHEMA
138 """Representation of a Blackbird matrix zone."""
140 _attr_supported_features = (
141 MediaPlayerEntityFeature.TURN_ON
142 | MediaPlayerEntityFeature.TURN_OFF
143 | MediaPlayerEntityFeature.SELECT_SOURCE
146 def __init__(self, blackbird, sources, zone_id, zone_name):
147 """Initialize new zone."""
161 """Retrieve latest state."""
165 self.
_attr_state_attr_state = MediaPlayerState.ON
if state.power
else MediaPlayerState.OFF
171 """Return the current source as media title."""
175 """Set all zones to one source."""
179 _LOGGER.debug(
"Setting all zones source to %s", idx)
180 self.
_blackbird_blackbird.set_all_zone_source(idx)
183 """Set input source."""
187 _LOGGER.debug(
"Setting zone %d source to %s", self.
_zone_id_zone_id, idx)
191 """Turn the media player on."""
192 _LOGGER.debug(
"Turning zone %d on", self.
_zone_id_zone_id)
196 """Turn the media player off."""
197 _LOGGER.debug(
"Turning zone %d off", self.
_zone_id_zone_id)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
web.Response get(self, web.Request request, str config_key)
def service_handle(HomeAssistant hass)