Skip to content

Commit

Permalink
Catch the exception if the TV is completely turned off
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioparraga committed Jul 7, 2016
1 parent 53c0b4e commit 9f64a6d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
31 changes: 19 additions & 12 deletions braviarc/braviarc.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _wakeonlan(self):
socket_instance.sendto(msg, ('<broadcast>', 9))
socket_instance.close()

def send_req_ircc(self, params):
def send_req_ircc(self, params, log_errors=True):
"""Send an IRCC command via HTTP to Sony Bravia."""
headers = {'SOAPACTION': 'urn:schemas-sony-com:service:IRCC:1#X_SendIRCC'}
data = ("<?xml version=\"1.0\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org" +
Expand All @@ -111,26 +111,30 @@ def send_req_ircc(self, params):
data=data,
timeout=TIMEOUT)
except requests.exceptions.HTTPError as exception_instance:
_LOGGER.error("[W] HTTPError: " + str(exception_instance))
if log_errors:
_LOGGER.error("HTTPError: " + str(exception_instance))

except Exception as exception_instance: # pylint: disable=broad-except
_LOGGER.error("[W] Exception: " + str(exception_instance))
if log_errors:
_LOGGER.error("Exception: " + str(exception_instance))
else:
content = response.content
return content

def bravia_req_json(self, url, params):
def bravia_req_json(self, url, params, log_errors=True):
""" Send request command via HTTP json to Sony Bravia."""
try:
response = requests.post('http://'+self._host+'/'+url,
data=params.encode("UTF-8"),
cookies=self._cookies,
timeout=TIMEOUT)
except requests.exceptions.HTTPError as exception_instance:
_LOGGER.error("[W] HTTPError: " + str(exception_instance))
if log_errors:
_LOGGER.error("HTTPError: " + str(exception_instance))

except Exception as exception_instance: # pylint: disable=broad-except
_LOGGER.error("[W] Exception: " + str(exception_instance))
if log_errors:
_LOGGER.error("Exception: " + str(exception_instance))

else:
html = json.loads(response.content.decode('utf-8'))
Expand Down Expand Up @@ -180,12 +184,15 @@ def get_playing_info(self):
return return_value

def get_power_status(self):
"""Get power status."""
return_value = {}
resp = self.bravia_req_json("sony/system", self._jdata_build("getPowerStatus", None))
if resp is not None and not resp.get('error'):
power_data = resp.get('result')[0]
return_value = power_data.get('status')
"""Get power status: off, active, standby"""
return_value = 'off' # by default the TV is turned off
try:
resp = self.bravia_req_json("sony/system", self._jdata_build("getPowerStatus", None), False)
if resp is not None and not resp.get('error'):
power_data = resp.get('result')[0]
return_value = power_data.get('status')
except: # pylint: disable=broad-except
pass
return return_value

def _refresh_commands(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# and be sure to test it firstly using "python setup.py register sdist upload -r pypitest"
setup(name='braviarc',
# version=open(os.path.join(CURRENT_DIR, 'xgboost/VERSION')).read().strip(),
version='0.3.2',
version='0.3.3',
description=open(os.path.join(CURRENT_DIR, 'README.md')).read(),
install_requires=['requests'],
maintainer='Antonio Parraga',
Expand Down
2 changes: 1 addition & 1 deletion setup_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# and be sure to test it firstly using "python setup.py register sdist upload -r pypitest"
setup(name='braviarc',
# version=open(os.path.join(CURRENT_DIR, 'xgboost/VERSION')).read().strip(),
version='0.3.2',
version='0.3.3',
description=open(os.path.join(CURRENT_DIR, 'README.md')).read(),
install_requires=['requests'],
maintainer='Antonio Parraga',
Expand Down

0 comments on commit 9f64a6d

Please sign in to comment.