Repository: PokemonGoF/PokemonGo-Bot

Issue:
wake_up_at_location doesn't use favorite_locations
It would be better if sleep_schedule.wake_up_at_location could use a location from favorite_locations in auth.json.



Discussion:

Resolving patch:
diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py
--- a/pokemongo_bot/__init__.py
+++ b/pokemongo_bot/__init__.py
@@ -94,6 +94,7 @@ def __init__(self, db, config):
         self.recent_forts = [None] * config.forts_max_circle_size
         self.tick_count = 0
         self.softban = False
+        self.wake_location = None
         self.start_position = None
         self.last_map_object = None
         self.last_time_map_object = 0
@@ -1107,6 +1108,41 @@ def _set_starting_position(self):
             # TODO: Add unit tests
             return
 
+        if self.wake_location:
+            msg = "Wake up location found: {location} {position}"
+            self.event_manager.emit(
+                'location_found',
+                sender=self,
+                level='info',
+                formatted=msg,
+                data={
+                    'location': self.wake_location['raw'],
+                    'position': self.wake_location['coord']
+                }
+            )
+
+            self.api.set_position(*self.wake_location['coord'])
+
+            self.event_manager.emit(
+                'position_update',
+                sender=self,
+                level='info',
+                formatted="Now at {current_position}",
+                data={
+                    'current_position': self.position,
+                    'last_position': '',
+                    'distance': '',
+                    'distance_unit': ''
+                }
+            )
+
+            self.start_position = self.position
+
+            has_position = True
+
+            return
+
+
         if self.config.location:
             location_str = self.config.location
             location = self.get_pos_by_name(location_str.replace(" ", ""))
diff --git a/pokemongo_bot/sleep_schedule.py b/pokemongo_bot/sleep_schedule.py
--- a/pokemongo_bot/sleep_schedule.py
+++ b/pokemongo_bot/sleep_schedule.py
@@ -65,7 +65,32 @@ def work(self):
             self._schedule_next_sleep()
             if wake_up_at_location:
                 if hasattr(self.bot, 'api'): # Check if api is already initialized
-                    self.bot.api.set_position(wake_up_at_location[0],wake_up_at_location[1],wake_up_at_location[2])
+                    msg = "Wake up location found: {location} {position}"
+                    self.bot.event_manager.emit(
+                        'location_found',
+                        sender=self,
+                        level='info',
+                        formatted=msg,
+                        data={
+                            'location': wake_up_at_location['raw'],
+                            'position': wake_up_at_location['coord']
+                        }
+                    )
+
+                    self.bot.api.set_position(*wake_up_at_location['coord'])
+
+                    self.bot.event_manager.emit(
+                        'position_update',
+                        sender=self,
+                        level='info',
+                        formatted="Now at {current_position}",
+                        data={
+                            'current_position': self.bot.position,
+                            'last_position': '',
+                            'distance': '',
+                            'distance_unit': ''
+                        }
+                    )
                 else:
                     self.bot.wake_location = wake_up_at_location
             if hasattr(self.bot, 'api'): self.bot.login() # Same here
@@ -129,18 +154,15 @@ def testkey(entry, key, offset=False, defval=''):
                 timedelta(
                     hours=raw_duration_random_offset.hour, minutes=raw_duration_random_offset.minute).total_seconds())
 
-            raw_wake_up_at_location = entry['wake_up_at_location'] if 'wake_up_at_location' in entry else ''
+            raw_wake_up_at_location = entry['wake_up_at_location'] if 'wake_up_at_location' in entry else None
             if raw_wake_up_at_location:
                 try:
-                    wake_up_at_location = raw_wake_up_at_location.split(',',2)
-                    lat=float(wake_up_at_location[0])
-                    lng=float(wake_up_at_location[1])
-                    if len(wake_up_at_location) == 3:
-                        alt=float(wake_up_at_location[2])
-                    else:
-                        alt = uniform(self.bot.config.alt_min, self.bot.config.alt_max)
-                    prepared['wake_up_at_location'] = [lat, lng, alt]
-                except ValueError:
+                    wake_up_at_location = self.bot.get_pos_by_name(raw_wake_up_at_location)
+                    lat = float(wake_up_at_location[0])
+                    lng = float(wake_up_at_location[1])
+                    alt = float(wake_up_at_location[2]) if wake_up_at_location[2] else uniform(self.bot.config.alt_min, self.bot.config.alt_max)
+                    prepared['wake_up_at_location'] = { 'raw': raw_wake_up_at_location, 'coord': (lat, lng, alt) }
+                except:
                     index = config.index(entry)
                     self.bot.warning('SleepSchedule: error parsing wake_up_at_location in entry %d' % index)
 
@@ -208,7 +230,7 @@ def _get_next_sleep_schedule(self):
             prev_day_time = next_time - timedelta(days=1)
             prev_day_end = next_end - timedelta(days=1)
 
-            location = self.entries[index]['wake_up_at_location'] if 'wake_up_at_location' in self.entries[index] else ''
+            location = self.entries[index]['wake_up_at_location'] if 'wake_up_at_location' in self.entries[index] else None
 
             diff = next_time - now
 


Test patch:
diff --git a/tests/location_parser_test.py b/tests/location_parser_test.py
--- a/tests/location_parser_test.py
+++ b/tests/location_parser_test.py
@@ -21,6 +21,7 @@ def setUp(self):
             username='Foobar',
         )
         self.bot.alt = 8
+        self.bot.wake_location = None
         self.bot.logger = MagicMock(return_value="")
         self.bot.updateConfig(config)