'Get the latest data and updates the states.'

def update(self):

    (p_start, p_end) = self._period
    self.update_period()
    (start, end) = self._period
    start = dt_util.as_utc(start)
    end = dt_util.as_utc(end)
    p_start = dt_util.as_utc(p_start)
    p_end = dt_util.as_utc(p_end)
    now = datetime.datetime.now()
    start_timestamp = math.floor(dt_util.as_timestamp(start))
    end_timestamp = math.floor(dt_util.as_timestamp(end))
    p_start_timestamp = math.floor(dt_util.as_timestamp(p_start))
    p_end_timestamp = math.floor(dt_util.as_timestamp(p_end))
    now_timestamp = math.floor(dt_util.as_timestamp(now))
    if ((start_timestamp == p_start_timestamp) and (end_timestamp == p_end_timestamp) and (end_timestamp <= now_timestamp)):
        return
    history_list = history.state_changes_during_period(self.hass, start, end, str(self._entity_id))
    if (self._entity_id not in history_list.keys()):
        return
    last_state = history.get_state(self.hass, start, self._entity_id)
    last_state = ((last_state is not None) and (last_state == self._entity_state))
    last_time = start_timestamp
    elapsed = 0
    count = 0
    for item in history_list.get(self._entity_id):
        current_state = (item.state == self._entity_state)
        current_time = item.last_changed.timestamp()
        if last_state:
            elapsed += (current_time - last_time)
        if (current_state and (not last_state)):
            count += 1
        last_state = current_state
        last_time = current_time
    if last_state:
        measure_end = min(end_timestamp, now_timestamp)
        elapsed += (measure_end - last_time)
    self.value = (elapsed / 3600)
    self.count = count
