def setup_bravia(config, pin, hass, add_entities): """Set up a Sony Bravia TV based on host parameter.""" host = config.get(CONF_HOST) name = config.get(CONF_NAME) if pin is None: request_configuration(config, hass, add_entities) return try: if ipaddress.ip_address(host).version == 6: mode = "ip6" else: mode = "ip" except ValueError: mode = "hostname" mac = get_mac_address(**{mode: host}) # If we came here and configuring this host, mark as done if host in _CONFIGURING: request_id = _CONFIGURING.pop(host) configurator = hass.components.configurator configurator.request_done(request_id) _LOGGER.info("Discovery configuration done") # Save config save_json( hass.config.path(BRAVIA_CONFIG_FILE), {host: { "pin": pin, "host": host, "mac": mac }}, ) braviarc = BraviaRC(host, mac) braviarc.connect(pin, CLIENTID_PREFIX, NICKNAME) unique_id = braviarc.get_system_info()["cid"].lower() add_entities([BraviaTVDevice(braviarc, name, pin, unique_id)])
def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Sony Bravia TV platform.""" host = config.get(CONF_HOST) if host is None: return pin = None bravia_config = load_json(hass.config.path(BRAVIA_CONFIG_FILE)) while bravia_config: # Set up a configured TV host_ip, host_config = bravia_config.popitem() if host_ip == host: pin = host_config["pin"] mac = host_config["mac"] name = config.get(CONF_NAME) braviarc = BraviaRC(host, mac) braviarc.connect(pin, CLIENTID_PREFIX, NICKNAME) unique_id = braviarc.get_system_info()["cid"].lower() add_entities([BraviaTVDevice(braviarc, name, pin, unique_id)]) return setup_bravia(config, pin, hass, add_entities)