Example #1
0
def on_clock(req):
  city = req.intent.slot('location').first().value
  if not city:
    current_time = datetime.now().time()
    #resp = req._('It\'s {}').format(current_time.strftime(req._('%I:%M %p')))
    resp = req._('It\'s {}').format(req._d(current_time, time_only=True))
    req.agent.answer(resp)
    return req.agent.done()
  else:
    try:
      g = geocoder.osm(city)
      if not g:
        resp = req._('Hummm! It seems {0} doesn\'t exists as city name').format(city)
        req.agent.answer(resp)
        return req.agent.done()
    except:
        resp = req._('Hummm! I encountered an error during the city information gathering')
        req.agent.answer(resp)
        return req.agent.done()
    tf = TimezoneFinder()
    tzStr = tf.timezone_at(lng=g.lng, lat=g.lat)
    if tzStr == '':
        resp = req._('Hummm! I can\'t retrieve time zone information of {0}').format(city)
        req.agent.answer(resp)
        return req.agent.done()
    tzObj = timezone(tzStr)
    current_time = datetime.now(tzObj)
    #resp = req._('It\'s {0} in {1}').format(current_time.strftime(req._('%I:%M %p'), city)
    resp = req._('It\'s {0} in {1}').format(req._d(current_time, time_only=True), city)
  req.agent.answer(resp)
  return req.agent.done()
Example #2
0
 def add_future(self, load: pd.Series) -> pd.Series:
     future = pd.date_range(start=load.index[-1],
                            end=(load.index[-1] + timedelta(days=1)),
                            freq="H").to_frame(name="load_MW")
     tz_finder = TimezoneFinder()
     lon = float(GEO_COORDS[self.iso_name]["lon"])
     lat = float(GEO_COORDS[self.iso_name]["lat"])
     tz_name = tz_finder.timezone_at(lng=lon, lat=lat)
     future["load_MW"] = None
     future.index = future.index.tz_convert(tz_name)
     return future
Example #3
0
 def get_historical_load(self) -> pd.DataFrame:
     if self.iso_name == "CAISO":
         load = self.get_caiso_load()
     elif (
         self.iso_name == "MISO"
         or self.iso_name == "PJM"
         or self.iso_name == "ERCOT"
     ):
         load = self.get_eia_load()
     else:
         load = pd.DataFrame(
             self.iso.get_load(
                 latest=False, yesterday=False, start_at=self.start, end_at=self.end
             )
         )[LOAD_COLS].set_index("timestamp")
     tz_finder = TimezoneFinder()
     tz_name = tz_finder.timezone_at(lng=float(self.lon), lat=float(self.lat))
     load.index = load.index.tz_convert(tz_name)
     return load.resample("H").mean()
Example #4
0
def timezone_offset(lat, lng, date_time):
    tf = TimezoneFinder()
    """
	returns a location's time zone offset from UTC in minutes.
	"""
    tz_target = pytz.timezone(tf.timezone_at(lng=lng, lat=lat))
    if tz_target is None:
        print("No timezone found in", str((lat, lng)))
        return ()
    # ATTENTION: tz_target could be None! handle error case
    #date_time = date_time.tzinfo=None#tzinfo=tz_target)#.utcoffset()
    #print("tzinfo = ",str(date_time.tzinfo))
    dt = date_time
    if dt.tzinfo is None:
        dated_target = tz_target.localize(dt)
        utc = pytz.utc
        dated_utc = utc.localize(dt)
        #return (dated_utc - dated_target).total_seconds() / 60 / 60
        return (strfdelta(dated_utc - dated_target, "%s%H:%M:%S"))
    else:
        print(dt.tzinfo)
        return ()
Example #5
0
class Life360Scanner:
    def __init__(self, hass, config, see, interval, home_place, members, api):
        self._hass = hass
        self._see = see
        self._show_as_state = config[CONF_SHOW_AS_STATE]
        self._home_place = home_place
        self._max_gps_accuracy = config.get(CONF_MAX_GPS_ACCURACY)
        self._max_update_wait = config.get(CONF_MAX_UPDATE_WAIT)
        prefix = config.get(CONF_PREFIX)
        self._prefix = '' if not prefix else prefix + '_'
        self._members = members
        self._driving_speed = config.get(CONF_DRIVING_SPEED)
        self._time_as = config[CONF_TIME_AS]
        self._api = api

        self._errs = {}
        self._error_threshold = config[CONF_ERROR_THRESHOLD]
        self._warning_threshold = config.get(CONF_WARNING_THRESHOLD,
                                             self._error_threshold)
        if self._warning_threshold > self._error_threshold:
            _LOGGER.warning('Ignoring {}: ({}) > {} ({})'.format(
                CONF_WARNING_THRESHOLD, self._warning_threshold,
                CONF_ERROR_THRESHOLD, self._error_threshold))

        self._max_errs = self._error_threshold + 2
        self._dev_data = {}
        if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]:
            from timezonefinderL import TimezoneFinder
            self._tf = TimezoneFinder()
        self._started = dt_util.utcnow()

        self._update_life360()
        track_time_interval(self._hass, self._update_life360, interval)

    def _ok(self, key):
        if self._errs.get(key, 0) >= self._max_errs:
            _LOGGER.error('{}: OK again'.format(key))
        self._errs[key] = 0

    def _err(self, key, err_msg):
        _errs = self._errs.get(key, 0)
        if _errs < self._max_errs:
            self._errs[key] = _errs = _errs + 1
            msg = '{}: {}'.format(key, err_msg)
            if _errs > self._error_threshold:
                if _errs == self._max_errs:
                    msg = 'Suppressing further errors until OK: ' + msg
                _LOGGER.error(msg)
            elif _errs > self._warning_threshold:
                _LOGGER.warning(msg)

    def _exc(self, key, exc):
        self._err(key, exc_msg(exc))

    def _dt_attr_from_utc(self, utc, tz):
        if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL] and tz:
            return utc.astimezone(tz)
        if self._time_as in [TZ_LOCAL, TZ_DEVICE_LOCAL]:
            return dt_util.as_local(utc)
        return utc

    def _dt_attr_from_ts(self, ts, tz):
        utc = utc_from_ts(ts)
        if utc:
            return self._dt_attr_from_utc(utc, tz)
        return STATE_UNKNOWN

    def _update_member(self, m, name):
        name = name.replace(',', '_').replace('-', '_')

        dev_id = slugify(self._prefix + name)
        prev_seen, reported = self._dev_data.get(dev_id, (None, False))

        loc = m.get('location')
        try:
            last_seen = utc_from_ts(loc.get('timestamp'))
        except AttributeError:
            last_seen = None

        if self._max_update_wait:
            update = last_seen or prev_seen or self._started
            overdue = dt_util.utcnow() - update > self._max_update_wait
            if overdue and not reported:
                self._hass.bus.fire(
                    'life360_update_overdue',
                    {'entity_id': DT_ENTITY_ID_FORMAT.format(dev_id)})
                reported = True
            elif not overdue and reported:
                self._hass.bus.fire(
                    'life360_update_restored', {
                        'entity_id':
                        DT_ENTITY_ID_FORMAT.format(dev_id),
                        'wait':
                        str(last_seen -
                            (prev_seen or self._started)).split('.')[0]
                    })
                reported = False

        self._dev_data[dev_id] = last_seen or prev_seen, reported

        if not loc:
            err_msg = m['issues']['title']
            if err_msg:
                if m['issues']['dialog']:
                    err_msg += ': ' + m['issues']['dialog']
            else:
                err_msg = 'Location information missing'
            self._err(dev_id, err_msg)
            return

        if last_seen and (not prev_seen or last_seen > prev_seen):
            lat = loc.get('latitude')
            lon = loc.get('longitude')
            gps_accuracy = loc.get('accuracy')
            try:
                lat = float(lat)
                lon = float(lon)
                # Life360 reports accuracy in feet, but Device Tracker expects
                # gps_accuracy in meters.
                gps_accuracy = round(
                    convert(float(gps_accuracy), LENGTH_FEET, LENGTH_METERS))
            except (TypeError, ValueError):
                self._err(
                    dev_id, 'GPS data invalid: {}, {}, {}'.format(
                        lat, lon, gps_accuracy))
                return

            self._ok(dev_id)

            msg = 'Updating {}'.format(dev_id)
            if prev_seen:
                msg += '; Time since last update: {}'.format(last_seen -
                                                             prev_seen)
            _LOGGER.debug(msg)

            if (self._max_gps_accuracy is not None
                    and gps_accuracy > self._max_gps_accuracy):
                _LOGGER.info('{}: Ignoring update because expected GPS '
                             'accuracy {} is not met: {}'.format(
                                 dev_id, gps_accuracy, self._max_gps_accuracy))
                return

            place_name = loc.get('name') or None

            # Does user want location name to be shown as state?
            if SHOW_PLACES in self._show_as_state:
                loc_name = place_name
                # Make sure Home Place is always seen exactly as home,
                # which is the special device_tracker state for home.
                if loc_name and loc_name.lower() == self._home_place:
                    loc_name = STATE_HOME
            else:
                loc_name = None

            # If a place name is given, then address will just be a copy of
            # it, so don't bother with address. Otherwise, piece address
            # lines together, depending on which are present.
            if place_name:
                address = None
            else:
                address1 = loc.get('address1') or None
                address2 = loc.get('address2') or None
                if address1 and address2:
                    address = ', '.join([address1, address2])
                else:
                    address = address1 or address2

            raw_speed = loc.get('speed')
            try:
                speed = float(raw_speed) * SPEED_FACTOR_MPH
                if self._hass.config.units.is_metric:
                    speed = convert(speed, LENGTH_MILES, LENGTH_KILOMETERS)
                speed = max(0, round(speed))
            except (TypeError, ValueError):
                speed = STATE_UNKNOWN
            driving = bool_attr_from_int(loc.get('isDriving'))
            if (driving in (STATE_UNKNOWN, False)
                    and self._driving_speed is not None
                    and speed != STATE_UNKNOWN):
                driving = speed >= self._driving_speed
            moving = bool_attr_from_int(loc.get('inTransit'))

            if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]:
                # timezone_at will return a string or None.
                tzname = self._tf.timezone_at(lng=lon, lat=lat)
                # get_time_zone will return a tzinfo or None.
                tz = dt_util.get_time_zone(tzname)
                attrs = {ATTR_TIME_ZONE: tzname or STATE_UNKNOWN}
            else:
                tz = None
                attrs = {}

            attrs.update({
                ATTR_ADDRESS:
                address,
                ATTR_AT_LOC_SINCE:
                self._dt_attr_from_ts(loc.get('since'), tz),
                ATTR_BATTERY_CHARGING:
                bool_attr_from_int(loc.get('charge')),
                ATTR_DRIVING:
                driving,
                ATTR_LAST_SEEN:
                self._dt_attr_from_utc(last_seen, tz),
                ATTR_MOVING:
                moving,
                ATTR_RAW_SPEED:
                raw_speed,
                ATTR_SPEED:
                speed,
                ATTR_WIFI_ON:
                bool_attr_from_int(loc.get('wifiState')),
            })

            # If we don't have a location name yet and user wants driving or moving
            # to be shown as state, and current location is not in a HA zone,
            # then update location name accordingly.
            if not loc_name and not active_zone(self._hass, lat, lon,
                                                gps_accuracy):
                if SHOW_DRIVING in self._show_as_state and driving is True:
                    loc_name = SHOW_DRIVING.capitalize()
                elif SHOW_MOVING in self._show_as_state and moving is True:
                    loc_name = SHOW_MOVING.capitalize()

            try:
                battery = int(float(loc.get('battery')))
            except (TypeError, ValueError):
                battery = None

            self._see(dev_id=dev_id,
                      location_name=loc_name,
                      gps=(lat, lon),
                      gps_accuracy=gps_accuracy,
                      battery=battery,
                      attributes=attrs,
                      picture=m.get('avatar'))

    def _update_life360(self, now=None):
        checked_ids = []

        err_key = 'get_circles'
        try:
            circles = self._api.get_circles()
        except _API_EXCS as exc:
            self._exc(err_key, exc)
            return
        self._ok(err_key)

        for circle in circles:
            err_key = 'get_circle "{}"'.format(
                circle.get('name') or circle.get('id'))
            try:
                members = self._api.get_circle_members(circle['id'])
            except _API_EXCS as exc:
                self._exc(err_key, exc)
                continue
            except KeyError:
                self._err(err_key, circle)
                continue
            self._ok(err_key)

            for m in members:
                err_key = 'Member data'
                try:
                    m_id = m['id']
                    sharing = bool(int(m['features']['shareLocation']))
                    name = m_name(m.get('firstName'), m.get('lastName'))
                except (KeyError, TypeError, ValueError):
                    self._err(err_key, m)
                    continue
                self._ok(err_key)

                if (m_id not in checked_ids
                        and (not self._members or name in self._members)
                        and sharing):
                    checked_ids.append(m_id)
                    self._update_member(m, name)
class CompositeScanner:
    def __init__(self, hass, config, see):
        self._hass = hass
        self._see = see
        entities = config[CONF_ENTITY_ID]
        self._entities = {}
        for entity_id in entities:
            self._entities[entity_id] = {
                WARNED: False,
                SOURCE_TYPE: None,
                STATE: None
            }
        self._dev_id = config[CONF_NAME]
        self._entity_id = ENTITY_ID_FORMAT.format(self._dev_id)
        self._time_as = config[CONF_TIME_AS]
        if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]:
            from timezonefinderL import TimezoneFinder
            self._tf = TimezoneFinder()
        self._lock = threading.Lock()
        self._prev_seen = None

        self._remove = track_state_change(hass, entities, self._update_info)

        for entity_id in entities:
            self._update_info(entity_id,
                              None,
                              hass.states.get(entity_id),
                              init=True)

    def _bad_entity(self, entity_id, message, init):
        msg = '{} {}'.format(entity_id, message)
        # Has there already been a warning for this entity?
        if self._entities[entity_id][WARNED]:
            _LOGGER.error(msg)
            self._remove()
            self._entities.pop(entity_id)
            # Are there still any entities to watch?
            if len(self._entities):
                self._remove = track_state_change(self._hass,
                                                  self._entities.keys(),
                                                  self._update_info)
        else:
            _LOGGER.warning(msg)
            # Don't count warnings during init.
            self._entities[entity_id][WARNED] = not init

    def _good_entity(self, entity_id, source_type, state):
        self._entities[entity_id].update({
            WARNED: False,
            SOURCE_TYPE: source_type,
            STATE: state
        })

    def _use_non_gps_data(self, state):
        if state == STATE_HOME:
            return True
        entities = self._entities.values()
        if any(entity[SOURCE_TYPE] == SOURCE_TYPE_GPS for entity in entities):
            return False
        return all(entity[STATE] != STATE_HOME for entity in entities
                   if entity[SOURCE_TYPE] in SOURCE_TYPE_NON_GPS)

    def _dt_attr_from_utc(self, utc, tz):
        if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL] and tz:
            return utc.astimezone(tz)
        if self._time_as in [TZ_LOCAL, TZ_DEVICE_LOCAL]:
            return dt_util.as_local(utc)
        return utc

    def _update_info(self, entity_id, old_state, new_state, init=False):
        if new_state is None:
            return

        with self._lock:
            # Get time device was last seen, which is the entity's last_seen
            # attribute, or if that doesn't exist, then last_updated from the
            # new state object. Make sure last_seen is timezone aware in UTC.
            # Note that dt_util.as_utc assumes naive datetime is in local
            # timezone.
            last_seen = new_state.attributes.get(ATTR_LAST_SEEN)
            if isinstance(last_seen, datetime):
                last_seen = dt_util.as_utc(last_seen)
            else:
                try:
                    last_seen = dt_util.utc_from_timestamp(float(last_seen))
                except (TypeError, ValueError):
                    last_seen = new_state.last_updated

            # Is this newer info than last update?
            if self._prev_seen and last_seen <= self._prev_seen:
                _LOGGER.debug(
                    'For {} skipping update from {}: '
                    'last_seen not newer than previous update ({} <= {})'.
                    format(self._entity_id, entity_id, last_seen,
                           self._prev_seen))
                return

            # Try to get GPS and battery data.
            try:
                gps = (new_state.attributes[ATTR_LATITUDE],
                       new_state.attributes[ATTR_LONGITUDE])
            except KeyError:
                gps = None
            gps_accuracy = new_state.attributes.get(ATTR_GPS_ACCURACY)
            battery = new_state.attributes.get(
                ATTR_BATTERY, new_state.attributes.get(ATTR_BATTERY_LEVEL))
            charging = new_state.attributes.get(
                ATTR_BATTERY_CHARGING, new_state.attributes.get(ATTR_CHARGING))
            # Don't use location_name unless we have to.
            location_name = None

            # What type of tracker is this?
            if new_state.domain == BS_DOMAIN:
                source_type = SOURCE_TYPE_BINARY_SENSOR
            else:
                source_type = new_state.attributes.get(ATTR_SOURCE_TYPE)

            state = new_state.state

            if source_type == SOURCE_TYPE_GPS:
                # GPS coordinates and accuracy are required.
                if gps is None:
                    self._bad_entity(entity_id, 'missing gps attributes', init)
                    return
                if gps_accuracy is None:
                    self._bad_entity(entity_id,
                                     'missing gps_accuracy attribute', init)
                    return
                self._good_entity(entity_id, SOURCE_TYPE_GPS, state)

            elif source_type in SOURCE_TYPE_NON_GPS:
                # Convert 'on'/'off' state of binary_sensor
                # to 'home'/'not_home'.
                if source_type == SOURCE_TYPE_BINARY_SENSOR:
                    if state == STATE_BINARY_SENSOR_HOME:
                        state = STATE_HOME
                    else:
                        state = STATE_NOT_HOME

                self._good_entity(entity_id, source_type, state)
                if not self._use_non_gps_data(state):
                    return

                # Don't use new GPS data if it's not complete.
                if gps is None or gps_accuracy is None:
                    gps = gps_accuracy = None
                # Get current GPS data, if any, and determine if it is in
                # 'zone.home'.
                cur_state = self._hass.states.get(self._entity_id)
                try:
                    cur_lat = cur_state.attributes[ATTR_LATITUDE]
                    cur_lon = cur_state.attributes[ATTR_LONGITUDE]
                    cur_acc = cur_state.attributes[ATTR_GPS_ACCURACY]
                    cur_gps_is_home = (active_zone(
                        self._hass, cur_lat, cur_lon,
                        cur_acc).entity_id == ENTITY_ID_HOME)
                except (AttributeError, KeyError):
                    cur_gps_is_home = False

                # It's important, for this composite tracker, to avoid the
                # component level code's "stale processing." This can be done
                # one of two ways: 1) provide GPS data w/ source_type of gps,
                # or 2) provide a location_name (that will be used as the new
                # state.)

                # If router entity's state is 'home' and current GPS data from
                # composite entity is available and is in 'zone.home',
                # use it and make source_type gps.
                if state == STATE_HOME and cur_gps_is_home:
                    gps = cur_lat, cur_lon
                    gps_accuracy = cur_acc
                    source_type = SOURCE_TYPE_GPS
                # Otherwise, if new GPS data is valid (which is unlikely if
                # new state is not 'home'),
                # use it and make source_type gps.
                elif gps:
                    source_type = SOURCE_TYPE_GPS
                # Otherwise, don't use any GPS data, but set location_name to
                # new state.
                else:
                    location_name = state

            else:
                self._bad_entity(
                    entity_id,
                    'unsupported source_type: {}'.format(source_type), init)
                return

            tz = None
            if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]:
                tzname = None
                if gps:
                    # timezone_at will return a string or None.
                    tzname = self._tf.timezone_at(lng=gps[1], lat=gps[0])
                    # get_time_zone will return a tzinfo or None.
                    tz = dt_util.get_time_zone(tzname)
                attrs = {ATTR_TIME_ZONE: tzname or STATE_UNKNOWN}
            else:
                attrs = {}

            attrs.update({
                ATTR_ENTITY_ID:
                tuple(entity_id
                      for entity_id, entity in self._entities.items()
                      if entity[ATTR_SOURCE_TYPE] is not None),
                ATTR_LAST_ENTITY_ID:
                entity_id,
                ATTR_LAST_SEEN:
                self._dt_attr_from_utc(last_seen.replace(microsecond=0), tz)
            })
            if charging is not None:
                attrs[ATTR_BATTERY_CHARGING] = charging
            self._see(dev_id=self._dev_id,
                      location_name=location_name,
                      gps=gps,
                      gps_accuracy=gps_accuracy,
                      battery=battery,
                      attributes=attrs,
                      source_type=source_type)

            self._prev_seen = last_seen
Example #7
0
cnt = 0
for prefix, event, value in jsonfile:
    if prefix == "" and event == "map_key":
        # new user dict found
        cur_usr_id = value
        cur_usr_lat = float(userdata[str(cur_usr_id)]["lat"])
        cur_usr_long = float(userdata[str(cur_usr_id)]["long"])
        cur_usr_weekdays = 0
        cur_usr_weekend = 0

    elif prefix == cur_usr_id and event == "map_key":
        # value = new date of cur user
        try:
            timestamp_utc = datetime.datetime.strptime(value, datetimeFormat) # convert timestamp to datetime format
            timestamp_utc = standard_zone.localize(timestamp_utc) # add timezone (UTC) to datetime format
            usr_zone = timezone(tf.timezone_at(lng=cur_usr_long, lat=cur_usr_lat)) # get timezone of the user
            usr_time = timestamp_utc.astimezone(usr_zone) # convert UTC time to user timezone
            usr_time = usr_time.replace(tzinfo=None) # remove timezone tag for further operations (comparison)
            # work or freetime?
            if usr_time.weekday() < 5:
                cur_usr_weekdays += 1
            else:
                cur_usr_weekend += 1
        except:
            pass

    elif prefix == cur_usr_id and event == "end_map":
        # end of user dict
        cnt += 1
        if cnt % 1000 == 0: 
            logging.info(str(int(cnt/1000)) + " thousand users computed.")
Example #8
0
from timezonefinderL import TimezoneFinder

tf = TimezoneFinder()

longitude, latitude = 13.358, 52.5061
tf.timezone_at(lng=longitude, lat=latitude)  # returns 'Europe/Berlin'
Example #9
0
    def insights(self, ip):
        """
        Get insights in ip
        :param ip:  The ip
        :return:    Insights
        :rtype:     geoip2.models.City
        """
        if request.remote_addr != ip:
            raise RuntimeError(
                "Can only use GoogleAppEngine-location-driver for looking up location of request-ip (=%s) (lookup=%s)"
                % (request.remote_addr, ip))

        raw_response = {}

        # Country
        country_iso = request.headers[
            'X-AppEngine-Country'] if 'X-AppEngine-Country' in request.headers else None
        country_iso = country_iso if country_iso and country_iso != 'ZZ' else None
        if country_iso:
            country_iso = Encoding.normalize(country_iso)
            raw_response['country'] = {'iso_code': country_iso}
            raw_response['registered_country'] = raw_response['country']
            raw_response['represented_country'] = raw_response['country']

        # Region
        region_iso = request.headers[
            'X-AppEngine-Region'] if 'X-AppEngine-Region' in request.headers else None
        region_iso = region_iso if region_iso else None
        if region_iso:
            region_iso = Encoding.normalize(region_iso)
            raw_response['subdivisions'] = [{'iso_code': region_iso}]

        # City
        city = request.headers[
            'X-AppEngine-City'] if 'X-AppEngine-City' in request.headers else None
        city = city if city else None
        if city:
            city = Encoding.normalize(city)
            raw_response['city'] = {'names': {'en': city}}

        # Location
        city_lat_long = request.headers[
            'X-AppEngine-CityLatLong'] if 'X-AppEngine-CityLatLong' in request.headers else None
        city_lat_long = city_lat_long if city_lat_long else None
        latitude, longitude = city_lat_long.split(',') if city_lat_long else (
            None, None)
        if latitude and longitude:
            latitude = float(Encoding.normalize(latitude))
            longitude = float(Encoding.normalize(longitude))

            raw_response['location'] = {
                'latitude': latitude,
                'longitude': longitude,
            }

            timezone_finder = TimezoneFinder()
            timezone = timezone_finder.timezone_at(lat=latitude, lng=longitude)
            timezone = timezone if timezone else None
            if timezone:
                raw_response['location']['time_zone'] = timezone

        return City(raw_response)