def get_api(hass, entry): """Return the api from glances_api.""" params = entry.copy() params.pop(CONF_NAME) verify_ssl = params.pop(CONF_VERIFY_SSL, True) httpx_client = get_async_client(hass, verify_ssl=verify_ssl) return Glances(httpx_client=httpx_client, **params)
async def async_setup_platform( hass, config, async_add_entities, discovery_info=None): """Set up the Glances sensors.""" from glances_api import Glances name = config[CONF_NAME] host = config[CONF_HOST] port = config[CONF_PORT] version = config[CONF_VERSION] var_conf = config[CONF_RESOURCES] username = config.get(CONF_USERNAME) password = config.get(CONF_PASSWORD) ssl = config[CONF_SSL] verify_ssl = config[CONF_VERIFY_SSL] session = async_get_clientsession(hass, verify_ssl) glances = GlancesData( Glances(hass.loop, session, host=host, port=port, version=version, username=username, password=password, ssl=ssl)) await glances.async_update() if glances.api.data is None: raise PlatformNotReady dev = [] for resource in var_conf: dev.append(GlancesSensor(glances, name, resource)) async_add_entities(dev, True)
def get_api(hass, entry): """Return the api from glances_api.""" params = entry.copy() params.pop(CONF_NAME) verify_ssl = params.pop(CONF_VERIFY_SSL) session = async_get_clientsession(hass, verify_ssl) return Glances(hass.loop, session, **params)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the Glances sensors.""" from glances_api import Glances name = config.get(CONF_NAME) host = config.get(CONF_HOST) port = config.get(CONF_PORT) version = config.get(CONF_VERSION) var_conf = config.get(CONF_RESOURCES) session = async_get_clientsession(hass) glances = GlancesData( Glances(hass.loop, session, host=host, port=port, version=version)) await glances.async_update() if glances.api.data is None: raise PlatformNotReady dev = [] for resource in var_conf: dev.append(GlancesSensor(glances, name, resource)) async_add_entities(dev, True)
async def test_plugins_list(httpx_mock: HTTPXMock): """Test the plugins list response.""" httpx_mock.add_response(json=PLUGINS_LIST_RESPONSE) client = Glances() await client.get_data("pluginslist") assert len(client.plugins) == 10
async def test_non_existing_endpoint(httpx_mock: HTTPXMock): """Test a non-exisiting endpoint.""" httpx_mock.add_response(json={}) client = Glances() await client.get_data("some-data") assert client.values == None
async def test_ha_sensor_data(httpx_mock: HTTPXMock): """Test the return value for ha sensors.""" httpx_mock.add_response(json=RESPONSE) client = Glances() result = await client.get_ha_sensor_data() assert result == HA_SENSOR_DATA
async def test_exisiting_endpoint(httpx_mock: HTTPXMock): """Test the a valid endpoint.""" httpx_mock.add_response(json=RESPONSE) client = Glances() await client.get_metrics("cpu") assert client.values["total"] == 10.6 assert client.values["system"] == 2.1
async def test_non_existing_endpoint(httpx_mock: HTTPXMock): """Test a non-exisiting endpoint.""" httpx_mock.add_response(status_code=400) client = Glances() with pytest.raises(GlancesApiNoDataAvailable): await client.get_data("some-data") assert not client.data
async def test_timeout(httpx_mock: HTTPXMock): """Test if the connection is hitting the timeout.""" def raise_timeout(request): """Set the timeout for the requests.""" raise httpx.ReadTimeout( f"Unable to read within {request.extensions['timeout']}", request=request) httpx_mock.add_callback(raise_timeout) with pytest.raises(exceptions.GlancesApiConnectionError): client = Glances() await client.get_metrics("mem")
async def main(): """The main part of the example script.""" data = Glances(version=VERSION) # Get the metrics for the memory await data.get_metrics("mem") # Print the values print("Memory values:", data.values) # Get the metrics about the disks await data.get_metrics("diskio") # Print the values print("Disk values:", data.values) # Get the data for Home Assistant print("Output to use with Home Assistant") print(await data.get_ha_sensor_data())