Exemplo n.º 1
0
async def test_incomplete_json(hass, parsed_json, message):
    """Existing data is not updated if JSON is invalid."""

    print(message)
    coordinator = YahooSymbolUpdateCoordinator(None, hass, DEFAULT_SCAN_INTERVAL)
    coordinator.get_json = AsyncMock(return_value=parsed_json)

    existing_data = {TEST_SYMBOL: {DATA_REGULAR_MARKET_PRICE: random.random()}}
    coordinator.data = existing_data

    # last_update_success is initially True
    assert coordinator.last_update_success is True

    await coordinator.async_refresh()
    await hass.async_block_till_done()

    # Data was invalid, existing data was left unchanged and last_update_success becomes False
    assert coordinator.data is existing_data
    assert coordinator.last_update_success is False
Exemplo n.º 2
0
async def test_data_from_json(hass, mock_json):
    """Tests data update all the way from from json."""
    symbol = "BABA"
    coordinator = YahooSymbolUpdateCoordinator([symbol], hass,
                                               DEFAULT_SCAN_INTERVAL)
    coordinator.get_json = AsyncMock(return_value=mock_json)

    await coordinator.async_refresh()
    await hass.async_block_till_done()

    sensor = YahooFinanceSensor(hass, coordinator, symbol, SAMPLE_VALID_CONFIG)

    # Accessing `available` triggers data population
    assert sensor.available is True

    attributes = sensor.device_state_attributes

    assert sensor.state == 232.73
    assert attributes["regularMarketChange"] == -5.66
    assert attributes["twoHundredDayAverageChangePercent"] == -0.13