Skip to content

Commit

Permalink
🐛 v2.7.1 (#166)
Browse files Browse the repository at this point in the history
Signed-off-by: Ludy87 <[email protected]>
  • Loading branch information
Ludy87 authored Feb 14, 2023
1 parent b245220 commit 8ee09df
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
31 changes: 23 additions & 8 deletions custom_components/xplora_watch/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ async def init(self, session=None) -> None:
async def _async_update_data(self, targets: list[str] = None):
"""Fetch data from Xplora."""
# Initialize the watch entry data
self.watch_entry = {}
if self.data:
self.watch_entry.update(self.data)

await self.init(aiohttp_client.async_create_clientsession(self.hass))
_LOGGER.debug("pyxplora_api lib version: %s", self.controller.version())

Expand All @@ -120,10 +124,12 @@ async def _async_update_data(self, targets: list[str] = None):
remove_message = self._entry.options.get(CONF_REMOVE_MESSAGE, False)

# Loop through the list of watches and fetch data
data = await self.data_loop(wuids, message_limit, remove_message)
return data
self.watch_entry.update(await self.data_loop(wuids, message_limit, remove_message))
self.data = self.watch_entry
return self.data

async def data_loop(self, wuids, message_limit, remove_message):
data = {}
for wuid in wuids:
_LOGGER.debug("Fetch data from Xplora: %s", wuid[25:])
device = self.controller.getDevice(wuid=wuid)
Expand All @@ -148,7 +154,7 @@ async def data_loop(self, wuids, message_limit, remove_message):

self.get_watch_functions(wuid, device)
await self.get_map()
data = self.get_data(wuid, chats)
data.update(self.get_data(wuid, chats))
return data

def get_watch_functions(self, wuid, device):
Expand All @@ -166,11 +172,9 @@ def get_watch_functions(self, wuid, device):
self._xcoin = device.get("getWatchUserXcoins", 0)

def get_location(self, device, watch_location):
if watch_location.get(ATTR_TRACKER_LAT, None):
self.lat = float(watch_location.get(ATTR_TRACKER_LAT, 0.0))
if watch_location.get(ATTR_TRACKER_LNG, None):
self.lng = float(watch_location.get(ATTR_TRACKER_LNG, 0.0))
self.poi = watch_location.get(ATTR_TRACKER_POI, "")
self.lat = float(watch_location.get(ATTR_TRACKER_LAT, 0.0)) if watch_location.get(ATTR_TRACKER_LAT, None) else None
self.lng = float(watch_location.get(ATTR_TRACKER_LNG, 0.0)) if watch_location.get(ATTR_TRACKER_LNG, None) else None
self.poi = watch_location.get(ATTR_TRACKER_POI, None)
self.location_accuracy = watch_location.get(ATTR_TRACKER_RAD, -1)
self.locateType = watch_location.get("locateType", PXA.LocationType.UNKNOWN.value)
self.lastTrackTime = device.get("lastTrackTime", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
Expand Down Expand Up @@ -225,3 +229,14 @@ def get_data(self, wuid, chats):
SENSOR_MESSAGE: chats,
}
}

async def message_data(self, wuid, message_limit, remove_message):
self.watch_entry = {}
if self.data:
self.watch_entry.update(self.data)
_LOGGER.debug("Fetch message data from Xplora: %s", wuid[25:])
res_chats = await self.controller.getWatchChatsRaw(wuid, limit=message_limit, show_del_msg=remove_message)
chats = ChatsNew.from_dict(res_chats).to_dict()
self.watch_entry.update({wuid: {SENSOR_MESSAGE: chats}})
self.data = self.watch_entry
return res_chats
4 changes: 2 additions & 2 deletions custom_components/xplora_watch/manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"domain": "xplora_watch",
"name": "Xplora® Watch",
"version": "v2.7.0",
"version": "v2.7.1",
"config_flow": true,
"documentation": "https://github.com/Ludy87/xplora_watch/tree/main",
"issue_tracker": "https://github.com/Ludy87/xplora_watch/issues",
"dependencies": [],
"requirements": [
"pyxplora_api==2.5.17",
"pyxplora_api==2.5.20",
"geopy==2.2.0",
"dataclasses-json",
"pydub"
Expand Down
49 changes: 22 additions & 27 deletions custom_components/xplora_watch/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,33 +170,28 @@ async def async_read_message(self, targets: list[str] | None = None, **kwargs):
if "all" in targets:
targets = self._coordinator.controller.getWatchUserIDs()
for watch in targets:
w: dict[str, Any] = old_state.get(watch, None)
if w:
res_chats = await self._coordinator.controller.getWatchChatsRaw(watch, limit, show_del_msg=show_remove_msg)
if res_chats:
for chat in res_chats.get("list"):
chat_type = chat.get("type")
msg_id = chat.get("msgId")
if chat_type == "VOICE":
voice = await self._coordinator.controller._gql_handler.fetchChatVoice_a(watch, msg_id)
encoded_base64_string_to_mp3_file(self._hass, voice.get("fetchChatVoice"), msg_id)
elif chat_type == "SHORT_VIDEO":
video = await self._coordinator.controller._gql_handler.fetchChatShortVideo_a(watch, msg_id)
encoded_base64_string_to_file(
self._hass, video.get("fetchChatShortVideo"), msg_id, "mp4", "video"
)
thumb = await self._coordinator.controller._gql_handler.fetchChatShortVideoCover_a(
watch, msg_id
)
encoded_base64_string_to_file(
self._hass, thumb.get("fetchChatShortVideoCover"), msg_id, "jpeg", "video/thumb"
)
elif chat_type == "IMAGE":
image = await self._coordinator.controller._gql_handler.fetchChatImage_a(watch, msg_id)
encoded_base64_string_to_file(self._hass, image.get("fetchChatImage"), msg_id, "jpeg", "image")
w.update({SENSOR_MESSAGE: (res_chats)})
old_state.update({watch: w})
self._coordinator.async_set_updated_data(old_state)
w: dict[str, Any] = old_state.get(watch, {})
res_chats = await self._coordinator.message_data(watch, limit, show_remove_msg)
if res_chats:
for chat in res_chats.get("list"):
chat_type = chat.get("type")
msg_id = chat.get("msgId")
if chat_type == "VOICE":
voice = await self._coordinator.controller._gql_handler.fetchChatVoice_a(watch, msg_id)
encoded_base64_string_to_mp3_file(self._hass, voice.get("fetchChatVoice"), msg_id)
elif chat_type == "SHORT_VIDEO":
video = await self._coordinator.controller._gql_handler.fetchChatShortVideo_a(watch, msg_id)
encoded_base64_string_to_file(self._hass, video.get("fetchChatShortVideo"), msg_id, "mp4", "video")
thumb = await self._coordinator.controller._gql_handler.fetchChatShortVideoCover_a(watch, msg_id)
encoded_base64_string_to_file(
self._hass, thumb.get("fetchChatShortVideoCover"), msg_id, "jpeg", "video/thumb"
)
elif chat_type == "IMAGE":
image = await self._coordinator.controller._gql_handler.fetchChatImage_a(watch, msg_id)
encoded_base64_string_to_file(self._hass, image.get("fetchChatImage"), msg_id, "jpeg", "image")
w.update({watch: {SENSOR_MESSAGE: res_chats}})
old_state.update(w)
await self._coordinator.async_set_updated_data(old_state)
else:
_LOGGER.warning("No watch id or type %s not allowed!" % type(targets))

Expand Down

0 comments on commit 8ee09df

Please sign in to comment.