Skip to content

Commit f2efe18

Browse files
authored
Check for watched status to update
1 parent b4c9e4c commit f2efe18

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

custom_components/sensor/plex_recently_added.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SSL
1919
from homeassistant.helpers.entity import Entity
2020

21-
__version__ = '0.1.5'
21+
__version__ = '0.1.6'
2222

2323
_LOGGER = logging.getLogger(__name__)
2424

@@ -71,9 +71,8 @@ def __init__(self, hass, conf):
7171
self.change_detected = False
7272
self._state = None
7373
self.card_json = []
74-
self.media_ids = [1]
7574
self.api_json = []
76-
self.data = []
75+
self.data = [{1}]
7776

7877
@property
7978
def name(self):
@@ -209,7 +208,15 @@ def update(self):
209208
self.api_json += sub_sec.json()['MediaContainer']['Metadata']
210209
self.api_json = sorted(self.api_json, key=lambda i: i['addedAt'],
211210
reverse=True)[:self.max_items]
211+
overview = get_info(self.api_json[0].get('title', ''))
212212

213+
"""Update attributes if view count changes"""
214+
if view_count(self.api_json) != view_count(self.data):
215+
self.change_detected = True
216+
self.data = self.api_json
217+
218+
api_ids = media_ids(self.api_json, True)
219+
data_ids = media_ids(self.data, True)
213220
if self.dl_images:
214221
directory = self.conf_dir + 'www' + self._dir
215222
if not os.path.exists(directory):
@@ -223,14 +230,12 @@ def update(self):
223230
dir_ids.sort(key=int)
224231

225232
"""Update if media items have changed or images are missing"""
226-
if (dir_ids != self.media_ids or
227-
media_ids(self.api_json, True) != self.media_ids):
233+
if dir_ids != api_ids or data_ids != api_ids:
228234
self.change_detected = True # Tell attributes to update
229235
self.data = self.api_json
230-
self.media_ids = media_ids(self.data, True)
231236
"""Remove images not in list"""
232237
for file in dir_images:
233-
if not any(str(ids) in file for ids in self.media_ids):
238+
if not any(str(ids) in file for ids in data_ids):
234239
os.remove(directory + file)
235240
"""Retrieve image from Plex if it doesn't exist"""
236241
for media in self.data:
@@ -264,10 +269,9 @@ def update(self):
264269
continue
265270
else:
266271
"""Update if media items have changed"""
267-
if media_ids(self.api_json, True) != self.media_ids:
272+
if api_ids != data_ids:
268273
self.change_detected = True # Tell attributes to update
269274
self.data = self.api_json
270-
self.media_ids = media_ids(self.data, False)
271275
else:
272276
self._state = '%s cannot be reached' % self.server_ip
273277

@@ -337,3 +341,24 @@ def media_ids(data, remote):
337341
ids = ids * 2
338342
ids.sort(key=int)
339343
return ids
344+
345+
346+
def view_count(data):
347+
ids = []
348+
for media in data:
349+
if 'ratingKey' in media:
350+
if 'viewCount' in media:
351+
ids.append(str(media['viewCount']))
352+
else:
353+
ids.append('0')
354+
else:
355+
continue
356+
return ids
357+
358+
359+
def get_info(title):
360+
tmdb_url = requests.get('https://api.themoviedb.org/3/search/movie?'
361+
'api_key=1f7708bb9a218ab891a5d438b1b63992&query='
362+
+ title)
363+
tmdb_json = tmdb_url.json()
364+
return tmdb_json['results'][0]['overview']

0 commit comments

Comments
 (0)