Fix broken forecast trend attribute in IQVIA (#23454)

This commit is contained in:
Aaron Bach 2019-04-27 09:17:57 -06:00 committed by Andrew Sayre
parent c174b83f54
commit bdc95e76d0

View File

@ -46,6 +46,7 @@ RATING_MAPPING = [{
'maximum': 12 'maximum': 12
}] }]
TREND_FLAT = 'Flat'
TREND_INCREASING = 'Increasing' TREND_INCREASING = 'Increasing'
TREND_SUBSIDING = 'Subsiding' TREND_SUBSIDING = 'Subsiding'
@ -76,17 +77,18 @@ async def async_setup_platform(
def calculate_trend(indices): def calculate_trend(indices):
"""Calculate the "moving average" of a set of indices.""" """Calculate the "moving average" of a set of indices."""
def moving_average(data, samples): index_range = np.arange(0, len(indices))
"""Determine the "moving average" (http://tinyurl.com/yaereb3c).""" index_array = np.array(indices)
ret = np.cumsum(data, dtype=float) linear_fit = np.polyfit(index_range, index_array, 1)
ret[samples:] = ret[samples:] - ret[:-samples] slope = round(linear_fit[0], 2)
return ret[samples - 1:] / samples
increasing = np.all(np.diff(moving_average(np.array(indices), 4)) > 0) if slope > 0:
if increasing:
return TREND_INCREASING return TREND_INCREASING
return TREND_SUBSIDING
if slope < 0:
return TREND_SUBSIDING
return TREND_FLAT
class ForecastSensor(IQVIAEntity): class ForecastSensor(IQVIAEntity):