'Initialize the Statistics sensor.'

def __init__(self, hass, entity_id, name, sampling_size):

    self._hass = hass
    self._entity_id = entity_id
    self.is_binary = (True if (self._entity_id.split('.')[0] == 'binary_sensor') else False)
    if (not self.is_binary):
        self._name = '{}    {}'.format(name, ATTR_MEAN)
    else:
        self._name = '{}    {}'.format(name, ATTR_COUNT)
    self._sampling_size = sampling_size
    self._unit_of_measurement = None
    if (self._sampling_size == 0):
        self.states = deque()
    else:
        self.states = deque(maxlen=self._sampling_size)
    self.median = self.mean = self.variance = self.stdev = 0
    self.min = self.max = self.total = self.count = 0
    self.average_change = self.change = 0
    @callback
    def async_stats_sensor_state_listener(entity, old_state, new_state):
        'Handle    the    sensor    state    changes.'
        self._unit_of_measurement = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
        try:
            self.states.append(float(new_state.state))
            self.count = (self.count + 1)
        except ValueError:
            self.count = (self.count + 1)
        hass.async_add_job(self.async_update_ha_state, True)
    async_track_state_change(hass, entity_id, async_stats_sensor_state_listener)
