def __init__(self, hass, see, interval, show_as_state, home_place, max_gps_accuracy, max_update_wait, prefix, members, driving_speed, time_as, api): self._hass = hass self._see = see self._show_as_state = show_as_state self._home_place = home_place self._max_gps_accuracy = max_gps_accuracy self._max_update_wait = max_update_wait self._prefix = '' if not prefix else prefix + '_' self._members = members self._driving_speed = driving_speed self._time_as = time_as self._api = api self._errs = {} self._max_errs = 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 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()
def setup(hass, config): if any(conf[CONF_TIME_AS] in (TZ_DEVICE_UTC, TZ_DEVICE_LOCAL) for conf in (config.get(DT_DOMAIN) or []) if conf[CONF_PLATFORM] == DOMAIN): pkg = config[DOMAIN][CONF_TZ_FINDER] try: asyncio.run_coroutine_threadsafe( async_process_requirements(hass, "{}.{}".format(DOMAIN, DT_DOMAIN), [pkg]), hass.loop, ).result() except RequirementsNotFound: _LOGGER.debug("Process requirements failed: %s", pkg) return False else: _LOGGER.debug("Process requirements suceeded: %s", pkg) if pkg.split("==")[0].strip().endswith("L"): from timezonefinderL import TimezoneFinder tf = TimezoneFinder() elif config[DOMAIN][CONF_TZ_FINDER_CLASS] == "TimezoneFinder": from timezonefinder import TimezoneFinder tf = TimezoneFinder() else: from timezonefinder import TimezoneFinderL tf = TimezoneFinderL() hass.data[DOMAIN] = tf return True
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, SEEN: None, SOURCE_TYPE: None, DATA: 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._req_movement = config[CONF_REQ_MOVEMENT] self._lock = threading.Lock() self._prev_seen = None self._init_complete = False 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)) def init_complete(event): self._init_complete = True hass.bus.listen_once(EVENT_HOMEASSISTANT_START, init_complete)
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 __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 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
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()
def __init__(self, hass, config, see, interval, home_place_name, api): """Initialize Life360Scanner.""" self._hass = hass self._see = see self._show_as_state = config[CONF_SHOW_AS_STATE] self._home_place_name = home_place_name 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 = config.get(CONF_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) 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._seen_members = set() if self._members is not None: _LOGGER.debug( 'Including: %s', ', '.join([ self._prefix + slugify(name.replace(',', '_').replace('-', '_')) for name in self._members ])) self._started = dt_util.utcnow() self._update_life360() track_time_interval(self._hass, self._update_life360, interval)
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.certain_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 ()
def main(): print(TimezoneFinder.using_numba()) parser = argparse.ArgumentParser() parser.add_argument('-q', '--utc', dest='utc', default=DEFAULT_TERM, type=float, help='Search term (default: %(default)s)') parser.add_argument('-lon', '--longitude', dest='lon', default=DEFAULT_LONGITUDE, type=float, help='Search longitude (default: %(default)f)') parser.add_argument('-lat', '--latitude', dest='lat', default=DEFAULT_LATITUDE, type=float, help='Search latitude (default: %(default)f') input_values = parser.parse_args() try: query_local(input_values.lat, input_values.lon, input_values.utc) except HTTPError as error: sys.exit( 'Encountered HTTP error {0} on {1}:\n {2}\nAbort program.'.format( error.code, error.url, error.read(), )) with open('11_16_US_Thanksgiving.csv') as csvfile: zipreader = csv.reader(csvfile) flag = False with open('output.csv', 'w', newline='') as csvout: for row in zipreader: if flag is False: flag = True continue lat = float(row[3]) lon = float(row[4]) utcTim = int(row[0]) aware_time = query_local(lat, lon, utcTim) write_row = list(row) write_row.append(str(aware_time)) spamwriter = csv.writer(csvout) spamwriter.writerow(write_row)
def setup(hass, config): if (any(conf[CONF_TIME_AS] in (TZ_DEVICE_UTC, TZ_DEVICE_LOCAL) for conf in (config.get(DT_DOMAIN) or []) if conf[CONF_PLATFORM] == DOMAIN)): pkg = config[DOMAIN][CONF_TZ_FINDER] try: asyncio.run_coroutine_threadsafe( async_process_requirements(hass, '{}.{}'.format(DOMAIN, DT_DOMAIN), [pkg]), hass.loop).result() except RequirementsNotFound: _LOGGER.debug('Process requirements failed: %s', pkg) return False else: _LOGGER.debug('Process requirements suceeded: %s', pkg) if pkg.split('==')[0].strip().endswith('L'): from timezonefinderL import TimezoneFinder else: from timezonefinder import TimezoneFinder hass.data[DOMAIN] = TimezoneFinder() return True
import re import datetime from importlib import import_module from itertools import chain from dateutil.parser import parse from urllib.parse import urlparse from timezonefinderL import TimezoneFinder from datacube.utils import geometry from pytz import timezone, utc import logging _LOG = logging.getLogger(__name__) tf = TimezoneFinder(in_memory=True) # Use metadata time if possible as this is what WMS uses to calculate it's temporal extents # datacube-core center time accessed through the dataset API is calculated and may # not agree with the metadata document def dataset_center_time(dataset): center_time = dataset.center_time try: metadata_time = dataset.metadata_doc['extent']['center_dt'] center_time = parse(metadata_time) except KeyError: try: metadata_time = dataset.metadata_doc['properties'][ 'dtr:start_datetime'] center_time = parse(metadata_time)
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)
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)
import csv import argparse import datetime import pytz from timezonefinderL import TimezoneFinder from pytz import timezone DEFAULT_TERM = 1480042675 DEFAULT_LONGITUDE = 122.41 DEFAULT_LATITUDE = -37.76 tf = TimezoneFinder() def main(): print(TimezoneFinder.using_numba()) parser = argparse.ArgumentParser() parser.add_argument('-q', '--utc', dest='utc', default=DEFAULT_TERM, type=float, help='Search term (default: %(default)s)') parser.add_argument('-lon', '--longitude', dest='lon', default=DEFAULT_LONGITUDE, type=float, help='Search longitude (default: %(default)f)') parser.add_argument('-lat', '--latitude',
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
# ---------- OUTPUT ------------ path_results = "/home/lmoldon/data/weekenders.json" # ------------------------------ # ---------- CONFIG ------------ datetimeFormat = "%Y-%m-%d %H:%M:%S" # ------------------------------ # ---------- INITIAL ----------- logging.basicConfig(format='%(asctime)s [%(levelname)s] - %(message)s', datefmt='%d-%m-%y %H:%M:%S', level=logging.INFO) usergroups = {} # key = userID, value = {"f": cur_usr_freetime, "w": cur_usr_worktime} userdata = {} # for location (timezone) tf = TimezoneFinder() valid_users = 0 standard_zone = timezone("UTC") # according to "Why do People Give Up Flossing? A Study of Contributor Disengagement in Open Source" GHTorrent only uses UTC as timezone # ------------------------------ log_starttime = datetime.datetime.now() logging.info("Accessing userdata ...") with open(path_source_userdata, "r") as fp: userdata = json.load(fp) logging.info("Done. (1/2)")
from timezonefinderL import TimezoneFinder tf = TimezoneFinder() longitude, latitude = 13.358, 52.5061 tf.timezone_at(lng=longitude, lat=latitude) # returns 'Europe/Berlin'
def parse_fields_2017_coord(tweet_obj): # parse files of 2017 data, where coord exists tzfinder = TimezoneFinder() utc = pytz.utc def parse_text(tw_obj): # remove use mentions, urls from the text # use extended tweet if presents if 'extended_tweet' in tw_obj: text = tw_obj['extended_tweet']['full_text'] # or use normal text else: text = tw_obj['text'] # process quoted tweet and append to text if tw_obj['is_quote_status'] and 'quoted_status' in tw_obj: # process quoted tweet qt_obj = tw_obj['quoted_status'] if 'extended_tweet' in qt_obj: qt_text = qt_obj['extended_tweet']['full_text'] # or use normal text else: qt_text = qt_obj['text'] text = ''.join([text, ' %QUOTES% ', qt_text]) text_norm = normalizeTextForTagger(replace_sp_tokens(text)) # process text into list of keywords text_tokens = get_tokens(text) text_tokens = [t for t in text_tokens if t not in stopwords] token_counts = dict(Counter(itertools.chain(*[text_tokens]))) # text_tokens = [lemma(t) for t in text_tokens] return text, text_norm, text_tokens, token_counts def parse_time(tw_obj): # parse timestamp to needed format # we need an actual timestamp in utc # and a fake local timestamp in utc # get timezone info point = tw_obj['coordinates']['coordinates'] try: tz_name = tzfinder.timezone_at(lng=point[0], lat=point[1]) tz_info = timezone(tz_name) except Exception as e: # if there is error when converting timezone # give default timezone as: US/Central tz_name = 'US/Central' tz_info = timezone(tz_name) # parse the utc timestamp time_obj = datetime.strptime(tw_obj['created_at'], time_format) # convert to local timestamp local_time_obj = time_obj.astimezone(tz_info) # get local hour mark for "time-of-day" query hour_mark = local_time_obj.time().hour # make a fake local timestamp with UTC timezone fake_time_obj = utc.localize(local_time_obj.replace(tzinfo=None)) return time_obj, local_time_obj, fake_time_obj, hour_mark, tz_name def parse_category(tw_obj): # parse category into str return 'Positive' # label = int(row['label']) # if label == 1: # return 'Positive' # else: # return 'Negative' def parse_drug_category(text_norm): cats = {} for cat in drug_category: for t in drug_category[cat]: if t in text_norm: if cat in cats: cats[cat].append(t) else: cats[cat] = [t] return cats result_dict = {} time_obj, local_time_obj, fake_time_obj, hour_mark, tz_name = parse_time( tweet_obj) try: text, text_norm, text_tokens, token_counts = parse_text(tweet_obj) except: print(tweet_obj) return None # put up dict result_dict['tweetID'] = tweet_obj['id_str'] result_dict['utcTime'] = time_obj # actual local time is not neede since db always saves utc # result_dict['localTime'] = local_time_obj result_dict['fakeLocalTime'] = fake_time_obj # result_dict['hourMark'] = hour_mark result_dict['timezone'] = tz_name result_dict['Followers'] = tweet_obj['user']['followers_count'] result_dict['Friends'] = tweet_obj['user']['friends_count'] result_dict['Statuses'] = tweet_obj['user']['statuses_count'] result_dict['textRaw'] = text result_dict['textNorm'] = text_norm result_dict['textTokens'] = text_tokens result_dict['textTokenCounts'] = token_counts result_dict['drugCategory'] = parse_drug_category(text_norm) result_dict['geo'] = tweet_obj['coordinates'] result_dict['geometry'] = Point(tweet_obj['coordinates']['coordinates']) result_dict['category'] = parse_category(tweet_obj) return result_dict
from peaky_finders.training_pipeline import MODEL_OUTPUT_DIR, MODEL_INPUT_DIR from peaky_finders.data_acquisition.train_model import GEO_COORDS ISO_MAP_IDS = { 56669: "MISO", 14725: "PJM", 2775: "CAISO", 13434: "ISONE", 13501: "NYISO", } ISO_LIST = ["NYISO", "ISONE", "PJM", "MISO", "CAISO"] PEAK_DATA_PATH = os.path.join(os.path.dirname(__file__), "historical_peaks") tz_finder = TimezoneFinder() def get_iso_map(): iso_df = pd.read_csv("iso_map_final.csv") iso_df["geometry"] = iso_df["geometry"].apply(wkt.loads) iso_gdf = gpd.GeoDataFrame(iso_df, crs="EPSG:4326", geometry="geometry") return iso_gdf class Predictor: def __init__(self, iso_name: str, start: str, end: str) -> None: self.start = start self.end = end self.iso_name = iso_name self.load_collector: LoadCollector = None