async def prometheus_client(loop, hass, hass_client): """Initialize an hass_client with Prometheus component.""" await async_setup_component(hass, prometheus.DOMAIN, {prometheus.DOMAIN: {}}) await setup.async_setup_component( hass, sensor.DOMAIN, {"sensor": [{"platform": "demo"}]} ) await setup.async_setup_component( hass, climate.DOMAIN, {"climate": [{"platform": "demo"}]} ) sensor1 = DemoSensor("Television Energy", 74, None, ENERGY_KILO_WATT_HOUR, None) sensor1.hass = hass sensor1.entity_id = "sensor.television_energy" await sensor1.async_update_ha_state() sensor2 = DemoSensor( "Radio Energy", 14, DEVICE_CLASS_POWER, ENERGY_KILO_WATT_HOUR, None ) sensor2.hass = hass sensor2.entity_id = "sensor.radio_energy" await sensor2.async_update_ha_state() sensor3 = DemoSensor("Electricity price", 0.123, None, "SEK/kWh", None) sensor3.hass = hass sensor3.entity_id = "sensor.electricity_price" await sensor3.async_update_ha_state() return await hass_client()
async def test_attributes(hass, hass_client): """Test prometheus metrics for entity attributes.""" client = await setup_prometheus_client(hass, hass_client, "") switch1 = DemoSensor(None, "Boolean", 74, None, None, None, None) switch1.hass = hass switch1.entity_id = "switch.boolean" switch1._attr_extra_state_attributes = {"boolean": True} await switch1.async_update_ha_state() switch2 = DemoSensor(None, "Number", 42, None, None, None, None) switch2.hass = hass switch2.entity_id = "switch.number" switch2._attr_extra_state_attributes = {"Number": 10.2} await switch2.async_update_ha_state() await hass.async_block_till_done() body = await generate_latest_metrics(client) assert ('switch_state{domain="switch",' 'entity="switch.boolean",' 'friendly_name="Boolean"} 74.0' in body) assert ('switch_attr_boolean{domain="switch",' 'entity="switch.boolean",' 'friendly_name="Boolean"} 1.0' in body) assert ('switch_state{domain="switch",' 'entity="switch.number",' 'friendly_name="Number"} 42.0' in body) assert ('switch_attr_number{domain="switch",' 'entity="switch.number",' 'friendly_name="Number"} 10.2' in body)
async def test_sensor_without_unit(hass, hass_client): """Test prometheus metrics for sensors without a unit.""" client = await setup_prometheus_client(hass, hass_client, "") sensor6 = DemoSensor(None, "Trend Gradient", 0.002, None, None, None, None) sensor6.hass = hass sensor6.entity_id = "sensor.trend_gradient" await sensor6.async_update_ha_state() sensor7 = DemoSensor(None, "Text", "should_not_work", None, None, None, None) sensor7.hass = hass sensor7.entity_id = "sensor.text" await sensor7.async_update_ha_state() sensor8 = DemoSensor(None, "Text Unit", "should_not_work", None, None, "Text", None) sensor8.hass = hass sensor8.entity_id = "sensor.text_unit" await sensor8.async_update_ha_state() body = await generate_latest_metrics(client) assert ('sensor_state{domain="sensor",' 'entity="sensor.trend_gradient",' 'friendly_name="Trend Gradient"} 0.002' in body) assert ('sensor_state{domain="sensor",' 'entity="sensor.text",' 'friendly_name="Text"} 0' not in body) assert ('sensor_unit_text{domain="sensor",' 'entity="sensor.text_unit",' 'friendly_name="Text Unit"} 0' not in body)
async def prometheus_client(loop, hass, hass_client): """Initialize an hass_client with Prometheus component.""" await async_setup_component(hass, prometheus.DOMAIN, {prometheus.DOMAIN: {}}) await setup.async_setup_component(hass, sensor.DOMAIN, {"sensor": [{ "platform": "demo" }]}) await setup.async_setup_component(hass, climate.DOMAIN, {"climate": [{ "platform": "demo" }]}) await hass.async_block_till_done() sensor1 = DemoSensor(None, "Television Energy", 74, None, ENERGY_KILO_WATT_HOUR, None) sensor1.hass = hass sensor1.entity_id = "sensor.television_energy" await sensor1.async_update_ha_state() sensor2 = DemoSensor(None, "Radio Energy", 14, DEVICE_CLASS_POWER, ENERGY_KILO_WATT_HOUR, None) sensor2.hass = hass sensor2.entity_id = "sensor.radio_energy" await sensor2.async_update_ha_state() sensor3 = DemoSensor(None, "Electricity price", 0.123, None, f"SEK/{ENERGY_KILO_WATT_HOUR}", None) sensor3.hass = hass sensor3.entity_id = "sensor.electricity_price" await sensor3.async_update_ha_state() sensor4 = DemoSensor(None, "Wind Direction", 25, None, DEGREE, None) sensor4.hass = hass sensor4.entity_id = "sensor.wind_direction" await sensor4.async_update_ha_state() sensor5 = DemoSensor( None, "SPS30 PM <1µm Weight concentration", 3.7069, None, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, None, ) sensor5.hass = hass sensor5.entity_id = "sensor.sps30_pm_1um_weight_concentration" await sensor5.async_update_ha_state() return await hass_client()
async def test_sensor_device_class(hass, hass_client): """Test prometheus metrics for sensor with a device_class.""" assert await async_setup_component( hass, "conversation", {}, ) client = await setup_prometheus_client(hass, hass_client, "") await async_setup_component(hass, sensor.DOMAIN, {"sensor": [{ "platform": "demo" }]}) await hass.async_block_till_done() sensor1 = DemoSensor(None, "Fahrenheit", 50, DEVICE_CLASS_TEMPERATURE, None, TEMP_FAHRENHEIT, None) sensor1.hass = hass sensor1.entity_id = "sensor.fahrenheit" await sensor1.async_update_ha_state() sensor2 = DemoSensor(None, "Radio Energy", 14, DEVICE_CLASS_POWER, None, ENERGY_KILO_WATT_HOUR, None) sensor2.hass = hass sensor2.entity_id = "sensor.radio_energy" with mock.patch( "homeassistant.util.dt.utcnow", return_value=datetime.datetime(1970, 1, 2, tzinfo=dt_util.UTC), ): await sensor2.async_update_ha_state() await hass.async_block_till_done() body = await generate_latest_metrics(client) assert ('sensor_temperature_celsius{domain="sensor",' 'entity="sensor.fahrenheit",' 'friendly_name="Fahrenheit"} 10.0' in body) assert ('sensor_temperature_celsius{domain="sensor",' 'entity="sensor.outside_temperature",' 'friendly_name="Outside Temperature"} 15.6' in body) assert ('sensor_humidity_percent{domain="sensor",' 'entity="sensor.outside_humidity",' 'friendly_name="Outside Humidity"} 54.0' in body) assert ('sensor_power_kwh{domain="sensor",' 'entity="sensor.radio_energy",' 'friendly_name="Radio Energy"} 14.0' in body)
async def test_view_empty_namespace(hass, hass_client): """Test prometheus metrics view.""" client = await setup_prometheus_client(hass, hass_client, "") sensor2 = DemoSensor( None, "Radio Energy", 14, SensorDeviceClass.POWER, None, ENERGY_KILO_WATT_HOUR, None, ) sensor2.hass = hass sensor2.entity_id = "sensor.radio_energy" with mock.patch( "homeassistant.util.dt.utcnow", return_value=datetime.datetime(1970, 1, 2, tzinfo=dt_util.UTC), ): await sensor2.async_update_ha_state() await hass.async_block_till_done() body = await generate_latest_metrics(client) assert "# HELP python_info Python platform information" in body assert ("# HELP python_gc_objects_collected_total " "Objects collected during gc" in body) assert ('entity_available{domain="sensor",' 'entity="sensor.radio_energy",' 'friendly_name="Radio Energy"} 1.0' in body) assert ('last_updated_time_seconds{domain="sensor",' 'entity="sensor.radio_energy",' 'friendly_name="Radio Energy"} 86400.0' in body)
async def test_input_number(hass, hass_client): """Test prometheus metrics for input_number.""" client = await setup_prometheus_client(hass, hass_client, "") number1 = DemoNumber(None, "Threshold", 5.2, None, False, 0, 10, 0.1) number1.hass = hass number1.entity_id = "input_number.threshold" await number1.async_update_ha_state() number2 = DemoNumber(None, None, 60, None, False, 0, 100) number2.hass = hass number2.entity_id = "input_number.brightness" number2._attr_name = None await number2.async_update_ha_state() number3 = DemoSensor(None, "Retry count", 5, None, None, None, None) number3.hass = hass number3.entity_id = "input_number.retry_count" await number3.async_update_ha_state() await hass.async_block_till_done() body = await generate_latest_metrics(client) assert ('input_number_state{domain="input_number",' 'entity="input_number.threshold",' 'friendly_name="Threshold"} 5.2' in body) assert ('input_number_state{domain="input_number",' 'entity="input_number.brightness",' 'friendly_name="None"} 60.0' in body) assert ('input_number_state{domain="input_number",' 'entity="input_number.retry_count",' 'friendly_name="Retry count"} 5.0' in body)
async def test_light(hass, hass_client): """Test prometheus metrics for lights.""" client = await setup_prometheus_client(hass, hass_client, "") light1 = DemoSensor(None, "Desk", 1, None, None, None, None) light1.hass = hass light1.entity_id = "light.desk" await light1.async_update_ha_state() light2 = DemoSensor(None, "Wall", 0, None, None, None, None) light2.hass = hass light2.entity_id = "light.wall" await light2.async_update_ha_state() light3 = DemoLight(None, "TV", True, True, 255, None, None) light3.hass = hass light3.entity_id = "light.tv" await light3.async_update_ha_state() light4 = DemoLight(None, "PC", True, True, 180, None, None) light4.hass = hass light4.entity_id = "light.pc" await light4.async_update_ha_state() await hass.async_block_till_done() body = await generate_latest_metrics(client) assert ('light_brightness_percent{domain="light",' 'entity="light.desk",' 'friendly_name="Desk"} 100.0' in body) assert ('light_brightness_percent{domain="light",' 'entity="light.wall",' 'friendly_name="Wall"} 0.0' in body) assert ('light_brightness_percent{domain="light",' 'entity="light.tv",' 'friendly_name="TV"} 100.0' in body) assert ('light_brightness_percent{domain="light",' 'entity="light.pc",' 'friendly_name="PC"} 70.58823529411765' in body)
async def test_input_boolean(hass, hass_client): """Test prometheus metrics for input_boolean.""" client = await setup_prometheus_client(hass, hass_client, "") input_boolean1 = DemoSensor(None, "Test", 1, None, None, None, None) input_boolean1.hass = hass input_boolean1.entity_id = "input_boolean.test" await input_boolean1.async_update_ha_state() input_boolean2 = DemoSensor(None, "Helper", 0, None, None, None, None) input_boolean2.hass = hass input_boolean2.entity_id = "input_boolean.helper" await input_boolean2.async_update_ha_state() await hass.async_block_till_done() body = await generate_latest_metrics(client) assert ('input_boolean_state{domain="input_boolean",' 'entity="input_boolean.test",' 'friendly_name="Test"} 1.0' in body) assert ('input_boolean_state{domain="input_boolean",' 'entity="input_boolean.helper",' 'friendly_name="Helper"} 0.0' in body)
def hass_platform_cloud_connection(loop, hass, config_entry_cloud_connection): demo_sensor = DemoSensor( unique_id='outside_temp', name='Outside Temperature', state=15.6, device_class=DEVICE_CLASS_TEMPERATURE, state_class=STATE_CLASS_MEASUREMENT, unit_of_measurement=TEMP_CELSIUS, battery=None ) demo_sensor.hass = hass demo_sensor.entity_id = 'sensor.outside_temp' demo_light = DemoLight( unique_id='light_kitchen', name='Kitchen Light', available=True, state=True, ) demo_light.hass = hass demo_light.entity_id = 'light.kitchen' loop.run_until_complete( demo_sensor.async_update_ha_state() ) loop.run_until_complete( demo_light.async_update_ha_state() ) loop.run_until_complete( async_setup_component(hass, http.DOMAIN, {http.DOMAIN: {}}) ) loop.run_until_complete( hass.async_block_till_done() ) with patch.object(hass.config_entries.flow, 'async_init', return_value=None), patch_yaml_files({ YAML_CONFIG_FILE: 'yandex_smart_home:' }): config_entry_cloud_connection.add_to_hass(hass) loop.run_until_complete(async_setup(hass, {DOMAIN: {}})) with patch('custom_components.yandex_smart_home.cloud.CloudManager.connect', return_value=None): loop.run_until_complete(async_setup_entry(hass, config_entry_cloud_connection)) return hass
async def prometheus_client(hass, hass_client, namespace): """Initialize an hass_client with Prometheus component.""" config = {} if namespace is not None: config[prometheus.CONF_PROM_NAMESPACE] = namespace await async_setup_component(hass, prometheus.DOMAIN, {prometheus.DOMAIN: config}) await async_setup_component(hass, sensor.DOMAIN, {"sensor": [{ "platform": "demo" }]}) await async_setup_component(hass, climate.DOMAIN, {"climate": [{ "platform": "demo" }]}) await hass.async_block_till_done() await async_setup_component(hass, humidifier.DOMAIN, {"humidifier": [{ "platform": "demo" }]}) sensor1 = DemoSensor(None, "Television Energy", 74, None, None, ENERGY_KILO_WATT_HOUR, None) sensor1.hass = hass sensor1.entity_id = "sensor.television_energy" await sensor1.async_update_ha_state() sensor2 = DemoSensor(None, "Radio Energy", 14, DEVICE_CLASS_POWER, None, ENERGY_KILO_WATT_HOUR, None) sensor2.hass = hass sensor2.entity_id = "sensor.radio_energy" with mock.patch( "homeassistant.util.dt.utcnow", return_value=datetime.datetime(1970, 1, 2, tzinfo=dt_util.UTC), ): await sensor2.async_update_ha_state() sensor3 = DemoSensor( None, "Electricity price", 0.123, None, None, f"SEK/{ENERGY_KILO_WATT_HOUR}", None, ) sensor3.hass = hass sensor3.entity_id = "sensor.electricity_price" await sensor3.async_update_ha_state() sensor4 = DemoSensor(None, "Wind Direction", 25, None, None, DEGREE, None) sensor4.hass = hass sensor4.entity_id = "sensor.wind_direction" await sensor4.async_update_ha_state() sensor5 = DemoSensor( None, "SPS30 PM <1µm Weight concentration", 3.7069, None, None, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, None, ) sensor5.hass = hass sensor5.entity_id = "sensor.sps30_pm_1um_weight_concentration" await sensor5.async_update_ha_state() return await hass_client()
async def test_sensor_unit(hass, hass_client): """Test prometheus metrics for sensors with a unit.""" client = await setup_prometheus_client(hass, hass_client, "") sensor1 = DemoSensor(None, "Television Energy", 74, None, None, ENERGY_KILO_WATT_HOUR, None) sensor1.hass = hass sensor1.entity_id = "sensor.television_energy" await sensor1.async_update_ha_state() sensor2 = DemoSensor( None, "Radio Energy", 14, SensorDeviceClass.POWER, None, ENERGY_KILO_WATT_HOUR, None, ) sensor2.hass = hass sensor2.entity_id = "sensor.radio_energy" with mock.patch( "homeassistant.util.dt.utcnow", return_value=datetime.datetime(1970, 1, 2, tzinfo=dt_util.UTC), ): await sensor2.async_update_ha_state() sensor3 = DemoSensor( None, "Electricity price", 0.123, None, None, f"SEK/{ENERGY_KILO_WATT_HOUR}", None, ) sensor3.hass = hass sensor3.entity_id = "sensor.electricity_price" await sensor3.async_update_ha_state() sensor4 = DemoSensor(None, "Wind Direction", 25, None, None, DEGREE, None) sensor4.hass = hass sensor4.entity_id = "sensor.wind_direction" await sensor4.async_update_ha_state() sensor5 = DemoSensor( None, "SPS30 PM <1µm Weight concentration", 3.7069, None, None, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, None, ) sensor5.hass = hass sensor5.entity_id = "sensor.sps30_pm_1um_weight_concentration" await sensor5.async_update_ha_state() sensor6 = DemoSensor(None, "Target temperature", 22.7, None, None, TEMP_CELSIUS, None) sensor6.hass = hass sensor6.entity_id = "input_number.target_temperature" await sensor6.async_update_ha_state() await hass.async_block_till_done() body = await generate_latest_metrics(client) assert ('sensor_unit_kwh{domain="sensor",' 'entity="sensor.television_energy",' 'friendly_name="Television Energy"} 74.0' in body) assert ('sensor_unit_sek_per_kwh{domain="sensor",' 'entity="sensor.electricity_price",' 'friendly_name="Electricity price"} 0.123' in body) assert ('sensor_unit_u0xb0{domain="sensor",' 'entity="sensor.wind_direction",' 'friendly_name="Wind Direction"} 25.0' in body) assert ('sensor_unit_u0xb5g_per_mu0xb3{domain="sensor",' 'entity="sensor.sps30_pm_1um_weight_concentration",' 'friendly_name="SPS30 PM <1µm Weight concentration"} 3.7069' in body) assert ('input_number_state_celsius{domain="input_number",' 'entity="input_number.target_temperature",' 'friendly_name="Target temperature"} 22.7' in body)