Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python API logging #9933

Closed
dirtycajunrice opened this issue Oct 17, 2017 · 8 comments
Closed

python API logging #9933

dirtycajunrice opened this issue Oct 17, 2017 · 8 comments

Comments

@dirtycajunrice
Copy link

Home Assistant release (hass --version):
55.1

Python release (python3 --version):
3.5.2

Component/platform:
API

Description of problem:
When making a python API call, the logbook does not reflect the results

Expected:
Example: switch x was turned off

@suprithcs
Copy link

Hi, I'd like to contribute. I assume this is a simple change and suits someone new to the code base. Could you please explain bit more about which files to look at?

@dirtycajunrice
Copy link
Author

dirtycajunrice commented Oct 22, 2017

@suprithIUB I am also very comfortable with python but there are so many sections of HA, I do not know where the issue lies or I would fix it myself.

@sdague
Copy link
Contributor

sdague commented Oct 31, 2017

Do you have some example code about what you are calling to understand what part of the API you are hitting here?

@dirtycajunrice
Copy link
Author

dirtycajunrice commented Oct 31, 2017

@sdague

import requests
import time
import homeassistant.remote as remote
from urllib.request import urlopen, URLError


class Cachet(object):

    def __init__(self, api, token=None):
        self.api = api
        self.api_token = token
        self.headers = {'X-Cachet-Token': self.api_token}
        self.status = {'Operational': 1, 'Performance Issues': 2, 'Partial Outage': 3, 'Major Outage': 4}

    def get(self, endpoint):
        g = requests.get(self.api + endpoint)
        return_data = {d['name']: d for d in g.json()['data']}

        return return_data

    def post(self, endpoint, data):
        p = requests.post(self.api + endpoint, headers=self.headers, data=data)
        print(p.text)
        return_data = p.json()

        return return_data


cachet = Cachet('https://status.cajun.pro/api/v1/', 'xxxxxxxxxxxxxxxxx')
components = cachet.get('components')
plex = components['plex.cajun.pro']
plex['status'] = int(plex['status'])
old_status = plex['status']

hass_api = remote.API('assistant.cajun.pro', 'xxxxxxxxxxxxx')
domain = 'switch'
switch_name = 'switch.plex_switch'

try:
    urlopen('https://speed.cajun.pro', timeout=5)
    if plex['status'] != cachet.status['Operational']:
        plex['status'] = cachet.status['Operational']
except URLError:
    if plex['status'] == cachet.status['Operational']:
        plex['status'] = cachet.status['Major Outage']

down_template = {"name": "Plex is Down", "message": "This alert was auto-generated by CajunBot. "
                                                    "The Administrator has been notified. Rebooting "
                                                    "the server automatically",
           "status": 1, "visible": 1, "component_id": plex['id'],"component_status": cachet.status['Major Outage'],
                 "notify": "true"}

up_template = {"name": "Plex is Back Up", "message": "This alert was auto-generated by CajunBot. "
                                                     "The Administrator has been notified.",
           "status": 4, "visible": 1, "component_id": plex['id'],"component_status": cachet.status['Operational'],
               "notify": "true"}

if old_status != plex['status']:
    if plex['status'] == 1:
        cachet.post('incidents', up_template)
    elif plex['status'] == 4:
        cachet.post('incidents', down_template)
        remote.call_service(hass_api, domain, 'turn_off', {'entity_id': '{}'.format(switch_name)})
        time.sleep(5)
        remote.call_service(hass_api, domain, 'turn_on', {'entity_id': '{}'.format(switch_name)})

@sdague
Copy link
Contributor

sdague commented Oct 31, 2017

Ok, where would this be plugged into Home Assistant? Sorry I don't seem to have a complete picture of what HA call you are making that you are expecting something inside the logbook.

@dirtycajunrice
Copy link
Author

dirtycajunrice commented Oct 31, 2017

@sdague ah. sorry i didnt post all of the code. all you see is the cachet part. See above. I updated the code. remote.call_service()

@balloobbot
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.

Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍

@balloobbot
Copy link

This issue will be auto-closed because there hasn't been any activity for a few months. Feel free to open a new one if you still experience this problem 👍

@home-assistant home-assistant locked and limited conversation to collaborators Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants