Skip to content

Commit

Permalink
Merge pull request jterrace#3 from siam28/feature/change-channel
Browse files Browse the repository at this point in the history
Added change_channel method and HA change channel helper
  • Loading branch information
iandday authored Mar 30, 2017
2 parents 460a01f + 5201b24 commit e864b66
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pyharmony/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@ def ha_sync(token, ip, port):
client.disconnect(send_close=True)
return 0

def ha_change_channel(token, ip, port, channel):
client = ha_get_client(token, ip, port)
status = client.change_channel(channel)
client.disconnect(send_close=True)
if status:
return True
else:
logger.error('Unable to change the channel')
return False

# Functions for use on command line
def show_config(args):
Expand Down
29 changes: 29 additions & 0 deletions pyharmony/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,35 @@ def send_command(self, device, command):
result = iq_cmd.send(block=False)
return result

def change_channel(self, channel):
"""Changes a channel.
Args:
channel: Channel number
Returns:
An HTTP 200 response (hopefully)
"""
iq_cmd = self.Iq()
iq_cmd['type'] = 'get'
action_cmd = ET.Element('oa')
action_cmd.attrib['xmlns'] = 'connect.logitech.com'
action_cmd.attrib['mime'] = ('harmony.engine?changeChannel')
cmd = 'channel=' + str(channel) + ':timestamp=0'
action_cmd.text = cmd
iq_cmd.set_payload(action_cmd)
try:
result = iq_cmd.send(block=True)
except Exception:
logger.info('XMPP timeout, reattempting')
result = iq_cmd.send(block=True)
payload = result.get_payload()
assert len(payload) == 1
action_cmd = payload[0]
if action_cmd.text == None:
return True
else:
return False


def power_off(self):
"""Turns the system off if it's on, otherwise it does nothing.
Expand Down

0 comments on commit e864b66

Please sign in to comment.