Skip to content

Commit

Permalink
Add exception if device can't be reached
Browse files Browse the repository at this point in the history
Add ping method to test if the device is reachable

Very ugly method ...
Remove requirements
  • Loading branch information
HydrelioxGitHub committed Jun 12, 2018
1 parent 70cd499 commit 1261836
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 18 additions & 1 deletion pyAlfawise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from voluptuous import Schema
import os

class AlfawiseError(Exception):
def __init__(self, arg):
Exception.__init__(self, "Alfawaise device can't be reached using this ip :" + arg)


class Alfawise:
Expand Down Expand Up @@ -42,6 +47,10 @@ def __init__(self, mac, ip='255.255.255.255'):
self.property = dict.fromkeys([self.OPTION_POWER, self.OPTION_COLOR,
self.OPTION_EFFECT, self.OPTION_TIMER,
self.OPTION_SPEED])
# Test if device is available by pinging it
if not self._is_device_reachable(ip):
raise AlfawiseError(ip)


def is_fan_on(self):
return self.property[self.OPTION_SPEED] != self.OFF
Expand Down Expand Up @@ -210,4 +219,12 @@ def _send_command(self, command_type, command_name, command_value):
command = bytes('{"command":"' + command_type + '", "' + command_name + '":"' + command_value + '","deviceid":"' + self.mac + '","modelid":"sj07","phoneid":"020000000000","userid":""}',
'UTF-8')
sock.sendto(command, (self.ip, 10002))
sock.close()
sock.close()

def _is_device_reachable(self, hostname):
response = os.system("ping -c 1 " + hostname)
# and then check the response...
if response == 0:
return True
else:
return False
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
name='pyAlfawise',
packages=['pyAlfawise'], # this must be the same as the name above
install_requires=['voluptuous'],
version='0.4-beta',
version='0.5-beta',
description='a simple python3 library for the Alfawise Humidifier',
author='Hydreliox',
author_email='[email protected]',
url='https://github.com/HydrelioxGitHub/pyAlfawise', # use the URL to the github repo
download_url='https://github.com/HydrelioxGitHub/pyAlfawise/tarball/0.1-beta',
download_url='https://github.com/HydrelioxGitHub/pyAlfawise/tarball/0.5-beta',
keywords=['alfawise', 'humidifier', 'mist', 'API'], # arbitrary keywords
classifiers=[],
)

0 comments on commit 1261836

Please sign in to comment.