PlexPy Sensor

The official PlexPy API doesn’t offer much, but I found this snippet which will query Plex directly for who is watching what. It would be a cool addition to Home Assistant as a sensor. I’ll add when I find some time, but thought I’d post here if anyone is looking for a project!

http://pastebin.com/C4WeNX3z

from

2 Likes

Took a quick stab at this. Still needs some cleanup but it works.

https://github.com/home-assistant/home-assistant/pull/2210

tried setting this up. copied plex.py into custom_components/sensors, added info to configuration.yaml but got following errors…

16-05-19 21:03:48 requests.packages.urllib3.connection: Certificate did not match expected hostname: my.plexapp.com. Certificate: {'version': 3, 'serialNumber': '0363EBAD976C7A79CEACA4BE648FE5BF27AC', 'OCSP': ('http://ocsp.int-x3.letsencrypt.org/',), 'issuer': ((('countryName', 'US'),), (('organizationName', "Let's Encrypt"),), (('commonName', "Let's Encrypt Authority X3"),)), 'subject': ((('commonName', 'REDACTED'),),), 'subjectAltName': (('DNS', 'REDACTED'),), 'notBefore': 'Apr 25 16:14:00 2016 GMT', 'caIssuers': ('http://cert.int-x3.letsencrypt.org/',), 'notAfter': 'Jul 24 16:14:00 2016 GMT'}
16-05-19 21:03:48 homeassistant.components.sensor: Error while setting up platform plex
Traceback (most recent call last):
  File "/srv/hass/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 578, in urlopen
    chunked=chunked)
  File "/srv/hass/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 351, in _make_request
    self._validate_conn(conn)
  File "/srv/hass/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 814, in _validate_conn
    conn.connect()
  File "/srv/hass/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 305, in connect
    _match_hostname(cert, self.assert_hostname or hostname)
  File "/srv/hass/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 313, in _match_hostname
    match_hostname(cert, asserted_hostname)
  File "/usr/lib/python3.4/ssl.py", line 286, in match_hostname
    % (hostname, dnsnames[0]))
ssl.CertificateError: hostname 'my.plexapp.com' doesn't match 'REDACTED'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/hass/lib/python3.4/site-packages/requests/adapters.py", line 403, in send
    timeout=timeout
  File "/srv/hass/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 604, in urlopen
    raise SSLError(e)
requests.packages.urllib3.exceptions.SSLError: hostname 'my.plexapp.com' doesn't match 'REDACTED'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/hass/lib/python3.4/site-packages/homeassistant/helpers/entity_component.py", line 98, in _setup_platform
    discovery_info)
  File "/home/hass/.homeassistant/custom_components/sensor/plex.py", line 30, in setup_platform
    add_devices([PlexSensor('Plex', plex_user, plex_password, plex_url)])
  File "/home/hass/.homeassistant/custom_components/sensor/plex.py", line 40, in __init__
    self._auth_token = self.getAuthToken(plex_user, plex_password)
  File "/home/hass/.homeassistant/custom_components/sensor/plex.py", line 66, in getAuthToken
    response = requests.post(auth_url, data=auth_params, headers=headers)
  File "/srv/hass/lib/python3.4/site-packages/requests/api.py", line 111, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/srv/hass/lib/python3.4/site-packages/requests/api.py", line 57, in request
    return session.request(method=method, url=url, **kwargs)
  File "/srv/hass/lib/python3.4/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/srv/hass/lib/python3.4/site-packages/requests/sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "/srv/hass/lib/python3.4/site-packages/requests/adapters.py", line 477, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: hostname 'my.plexapp.com' doesn't match REDACTED'
16-05-19 21:03:53 homeassistant.components.media_player.plex: Saving config file failed: [Errno 13] Permission denied: '/home/hass/.homeassistant/plex.conf'
16-05-19 21:03:53 homeassistant.components.media_player.plex: failed to save config file
16-05-19 21:04:16 homeassistant.components.media_player.plex: Saving config file failed: [Errno 13] Permission denied: '/home/hass/.homeassistant/plex.conf'
16-05-19 21:04:16 homeassistant.components.media_player.plex: failed to save config file

Did you put in your local plex server ip for host? I would edit or delete your last comment, it contains your public ddns hostname.

I did put in my own host name.

  - platform: plex
    username: REDACTED
    password: REDACTED
    host: 192.168.0.90

For those still interested, check out: