1 """Support for interfacing with Monoprice 6 zone home audio controller."""
5 from serial
import SerialException
7 from homeassistant
import core
9 MediaPlayerDeviceClass,
11 MediaPlayerEntityFeature,
30 _LOGGER = logging.getLogger(__name__)
38 sources_config = data[CONF_SOURCES]
40 source_id_name = {
int(index): name
for index, name
in sources_config.items()}
42 source_name_id = {v: k
for k, v
in source_id_name.items()}
44 source_names = sorted(source_name_id.keys(), key=
lambda v: source_name_id[v])
46 return [source_id_name, source_name_id, source_names]
51 if CONF_SOURCES
in config_entry.options:
52 data = config_entry.options
54 data = config_entry.data
60 config_entry: ConfigEntry,
61 async_add_entities: AddEntitiesCallback,
63 """Set up the Monoprice 6-zone amplifier platform."""
64 port = config_entry.data[CONF_PORT]
66 monoprice = hass.data[DOMAIN][config_entry.entry_id][MONOPRICE_OBJECT]
73 zone_id = (i * 10) + j
74 _LOGGER.debug(
"Adding zone %d for port %s", zone_id, port)
76 MonopriceZone(monoprice, sources, config_entry.entry_id, zone_id)
80 first_run = hass.data[DOMAIN][config_entry.entry_id][FIRST_RUN]
83 platform = entity_platform.async_get_current_platform()
85 def _call_service(entities, service_call):
86 for entity
in entities:
87 if service_call.service == SERVICE_SNAPSHOT:
89 elif service_call.service == SERVICE_RESTORE:
92 @service.verify_domain_control(hass, DOMAIN)
93 async
def async_service_handle(service_call: core.ServiceCall) ->
None:
94 """Handle for services."""
95 entities = await platform.async_extract_from_service(service_call)
100 hass.async_add_executor_job(_call_service, entities, service_call)
102 hass.services.async_register(
105 async_service_handle,
106 schema=cv.make_entity_service_schema({}),
109 hass.services.async_register(
112 async_service_handle,
113 schema=cv.make_entity_service_schema({}),
118 """Representation of a Monoprice amplifier zone."""
120 _attr_device_class = MediaPlayerDeviceClass.RECEIVER
121 _attr_supported_features = (
122 MediaPlayerEntityFeature.VOLUME_MUTE
123 | MediaPlayerEntityFeature.VOLUME_SET
124 | MediaPlayerEntityFeature.VOLUME_STEP
125 | MediaPlayerEntityFeature.TURN_ON
126 | MediaPlayerEntityFeature.TURN_OFF
127 | MediaPlayerEntityFeature.SELECT_SOURCE
129 _attr_has_entity_name =
True
132 def __init__(self, monoprice, sources, namespace, zone_id):
133 """Initialize new zone."""
145 manufacturer=
"Monoprice",
146 model=
"6-Zone Amplifier",
147 name=f
"Zone {self._zone_id}",
154 """Retrieve latest state."""
157 except SerialException:
159 _LOGGER.warning(
"Could not update zone %d", self.
_zone_id_zone_id)
166 self.
_attr_state_attr_state = MediaPlayerState.ON
if state.power
else MediaPlayerState.OFF
174 """Return if the entity should be enabled when first added to the entity registry."""
179 """Return the current source as medial title."""
183 """Save zone's current state."""
187 """Restore saved state."""
193 """Set input source."""
200 """Turn the media player on."""
204 """Turn the media player off."""
208 """Mute (true) or unmute (false) media player."""
212 """Set volume level, range 0..1."""
213 self.
_monoprice_monoprice.set_volume(self.
_zone_id_zone_id, round(volume * MAX_VOLUME))
216 """Volume up the media player."""
219 volume = round(self.
volume_levelvolume_level * MAX_VOLUME)
223 """Volume down media player."""
226 volume = round(self.
volume_levelvolume_level * MAX_VOLUME)
None schedule_update_ha_state(self, bool force_refresh=False)
web.Response get(self, web.Request request, str config_key)