async def async_step_user(self, user_input=None): """Handle a flow initiated by the user.""" if user_input is None: return self.async_show_form( step_id="user", data_schema=vol.Schema({vol.Required(CONF_ENDPOINT): str}), ) # Validate input endpoint = user_input[CONF_ENDPOINT] parsed_url = urlparse(endpoint) # Try to connect and get device name try: device = Device(endpoint) await device.get_supported_methods() interface_info = await device.get_interface_information() name = interface_info.modelName except SongpalException as ex: _LOGGER.debug("Connection failed: %s", ex) return self.async_show_form( step_id="user", data_schema=vol.Schema({ vol.Required(CONF_ENDPOINT, default=user_input.get(CONF_ENDPOINT, "")): str, }), errors={"base": "cannot_connect"}, ) self.conf = SongpalConfig(name, parsed_url.hostname, endpoint) return await self.async_step_init(user_input)
async def cli(ctx, endpoint, debug, websocket, post): """Songpal CLI.""" lvl = logging.INFO if debug: lvl = logging.DEBUG click.echo("Setting debug level to %s" % debug) logging.basicConfig(level=lvl) if ctx.invoked_subcommand == "discover": ctx.obj = {"debug": debug} return if endpoint is None: err("Endpoint is required except when with 'discover'!") return protocol = None if post and websocket: err("You can force either --post or --websocket") return elif websocket: protocol = ProtocolType.WebSocket elif post: protocol = ProtocolType.XHRPost logging.debug("Using endpoint %s", endpoint) x = Device(endpoint, force_protocol=protocol, debug=debug) try: await x.get_supported_methods() except (requests.exceptions.ConnectionError, SongpalException) as ex: err("Unable to get supported methods: %s" % ex) sys.exit(-1) ctx.obj = x
async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities) -> None: """Set up songpal media player.""" name = config_entry.data[CONF_NAME] endpoint = config_entry.data[CONF_ENDPOINT] device = Device(endpoint) try: async with async_timeout.timeout( 10): # set timeout to avoid blocking the setup process await device.get_supported_methods() except (SongpalException, asyncio.TimeoutError) as ex: _LOGGER.warning("[%s(%s)] Unable to connect", name, endpoint) _LOGGER.debug("Unable to get methods from songpal: %s", ex) raise PlatformNotReady from ex songpal_entity = SongpalEntity(name, device) async_add_entities([songpal_entity], True) platform = entity_platform.current_platform.get() platform.async_register_entity_service( SET_SOUND_SETTING, { vol.Required(PARAM_NAME): cv.string, vol.Required(PARAM_VALUE): cv.string }, "async_set_sound_setting", )
def __init__(self, name, endpoint, poll=False): """Init.""" self._name = name self._endpoint = endpoint self._poll = poll self.dev = Device(self._endpoint) self._sysinfo = None self._state = False self._available = False self._initialized = False self._volume_control = None self._volume_min = 0 self._volume_max = 1 self._volume = 0 self._is_muted = False self._active_source = None self._sources = {}
async def async_step_import(self, user_input=None): """Import a config entry.""" name = user_input.get(CONF_NAME) endpoint = user_input.get(CONF_ENDPOINT) parsed_url = urlparse(endpoint) # Try to connect to test the endpoint try: device = Device(endpoint) await device.get_supported_methods() # Get name if name is None: interface_info = await device.get_interface_information() name = interface_info.modelName except SongpalException as ex: _LOGGER.error("Import from yaml configuration failed: %s", ex) return self.async_abort(reason="cannot_connect") self.conf = SongpalConfig(name, parsed_url.hostname, endpoint) return await self.async_step_init(user_input)