Skip to content

Commit

Permalink
Check bridge model version
Browse files Browse the repository at this point in the history
  • Loading branch information
Snapcase committed Jul 18, 2019
1 parent 9ed57f2 commit fc21e4a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 37 deletions.
9 changes: 7 additions & 2 deletions script.service.hue/addon.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<addon id="script.service.hue" name="Hue Service"
provider-name="zim514" version="0.6.9">
provider-name="zim514" version="0.6.10">

<requires>
<import addon="xbmc.python" version="2.26.0" />
Expand All @@ -24,7 +24,12 @@
<summary lang="en_GB">Automate Hue lights with Kodi playback</summary>
<description lang="en_GB">Automate your Philips Hue lights with Kodi. Create multi-room scenes that run when you Play, Pause or Stop media playback. Create and delete multi-room scenes from add-on settings then select which to apply for different Playback events. Can use Hue's sunrise and sunset times to automatically disable the service during daytime and turn on the lights at sunset during playback. Includes experimental Ambilight support. Check forum for more information.</description>
<disclaimer lang="en_GB">Automate Hue lights with Kodi playback</disclaimer>
<news>v0.6.7:
<news>
v0.6.10:
- Warn about V1 bridge incompatibility
- Color distance minimum setting

v0.6.7:
- Change some ambilight default settings to better values
- Change Hue NuPNP Discovery URL until SSL chain is fixed by Philips
- Fix Hue username not saving properly
Expand Down
25 changes: 10 additions & 15 deletions script.service.hue/resources/lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ def menu():

if args == "discover":
logger.debug("Started with Discovery")
bridge = kodiHue.bridgeDiscover(monitor)
if bridge is not None:
logger.debug("Found bridge, starting service.")
service() #restart service

bridgeDiscovered = kodiHue.bridgeDiscover(monitor)
if bridgeDiscovered:
bridge = kodiHue.connectBridge(monitor, silent=True)
if bridge:
logger.debug("Found bridge. Running model check & starting service.")
kodiHue.checkBridgeModel(bridge)
globals.ADDON.openSettings()
service()

elif args == "createHueScene":
logger.debug("Started with {}".format(args))
Expand Down Expand Up @@ -58,28 +61,20 @@ def menu():
bridge = kodiHue.connectBridge(monitor, silent=True) # don't rediscover, proceed silently
if bridge is not None:
kodiHue.configureScene(bridge, kgroup, action)

#TODO: save selection
else:
logger.debug("No bridge found. sceneSelect cancelled.")
xbmcgui.Dialog().notification(_("Hue Service"), _("Check Hue Bridge configuration"))

elif args == "ambiLightSelect": # ambiLightSelect=kgroup
elif args == "ambiLightSelect": # ambiLightSelect=kgroupID
kgroup = sys.argv[2]

logger.debug("Started with {}, kgroup: {}".format(args, kgroup))
logger.debug("Started with {}, kgroupID: {}".format(args, kgroup))

bridge = kodiHue.connectBridge(monitor, silent=True) # don't rediscover, proceed silently
if bridge is not None:
kodiHue.configureAmbiLights(bridge, kgroup)

#TODO: save selection
else:
logger.debug("No bridge found. scene ambi lights cancelled.")
xbmcgui.Dialog().notification(_("Hue Service"), _("Check Hue Bridge configuration"))



else:
globals.ADDON.openSettings()
return
Expand Down
7 changes: 3 additions & 4 deletions script.service.hue/resources/lib/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
logger = getLogger(ADDONID)


#Init values for code completion, all get overwritten by kodiHue.loadSettings()
settingsChanged = False
connected = False
daylight = False
Expand All @@ -50,7 +49,7 @@ def wrapper_timer(*args, **kwargs):
value = func(*args, **kwargs)
endTime = time.time() # 2
runTime = endTime - startTime # 3
if performanceLogging == True:
logger.debug("[script.service.hue][{}] Completed in {:02f}ms".format(func.__name__,runTime*1000))
if performanceLogging:
logger.debug("[{}] Completed in {:02.0f}ms".format(func.__name__,runTime*1000))
return value
return wrapper_timer
return wrapper_timer
42 changes: 28 additions & 14 deletions script.service.hue/resources/lib/kodiHue.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from . import qhue

from .language import get_string as _
from resources.lib.qhue.qhue import QhueException



Expand Down Expand Up @@ -95,14 +96,12 @@ def deleteHueScene(bridge):
xbmcgui.Dialog().notification(_("Hue Service"), _("ERROR: Scene not created"))



def _discoverNupnp():

logger.debug("In kodiHue discover_nupnp()")
try:
#ssl chain on new URL seems to be broken
req = requests.get('https://www.meethue.com/api/nupnp')
#req = requests.get('https://discovery.meethue.com/')
#ssl chain on new URL seems to be fixed
#req = requests.get('https://www.meethue.com/api/nupnp')
req = requests.get('https://discovery.meethue.com/')
except requests.exceptions.ConnectionError as e:
logger.info("Nupnp failed: {}".format(e))
return None
Expand All @@ -111,12 +110,10 @@ def _discoverNupnp():
bridge_ip = None
if res:
bridge_ip = res[0]["internalipaddress"]

return bridge_ip


def _discoverSsdp():

from . import ssdp
from urlparse import urlsplit

Expand Down Expand Up @@ -145,7 +142,6 @@ def bridgeDiscover(monitor):
complete = False
while not progressBar.iscanceled() and not complete:


progressBar.update(10, _("N-UPnP discovery..."))
bridgeIP =_discoverNupnp()
if not bridgeIP:
Expand All @@ -169,7 +165,7 @@ def bridgeDiscover(monitor):
progressBar.update(100, _("Complete!"))
monitor.waitForAbort(5)
progressBar.close()
globals.ADDON.openSettings()
logger.debug("Bridge discovery complete")
return True
else:
logger.debug("User not created, received: {}".format(bridgeUser))
Expand All @@ -181,11 +177,13 @@ def bridgeDiscover(monitor):

else:
progressBar.update(100, _("Bridge not found"),_("Check your bridge and network"))
logger.debug("Bridge not found, check your bridge and network")
monitor.waitForAbort(5)
complete = True
progressBar.close()

if progressBar.iscanceled():
logger.debug("Bridge discovery cancelled by user")
progressBar.update(100,_("Cancelled"))
complete = True
progressBar.close()
Expand Down Expand Up @@ -397,24 +395,22 @@ def sunset(bridge,kgroups):
g.sunset()
return


def connectBridge(monitor,silent=False):
bridgeIP = kodiutils.get_setting("bridgeIP")
bridgeUser = kodiutils.get_setting("bridgeUser")
logger.debug("in Connect() with settings: bridgeIP: {}, bridgeUser: {}".format(bridgeIP,bridgeUser))


if bridgeIP and bridgeUser:
if connectionTest(bridgeIP):
logger.debug("in Connect(): Bridge responding to connection test.")

else:
logger.debug("in Connect(): Bridge not responding to connection test, attempt finding a new bridge IP.")
bridgeIP = discoverBridgeIP(monitor)
if bridgeIP:
logger.debug("in Connect(): New IP found: {}. Saving".format(bridgeIP))
kodiutils.set_setting("bridgeIP",bridgeIP)


if bridgeIP:
logger.debug("in Connect(): Checking User")
if userTest(bridgeIP, bridgeUser):
Expand All @@ -436,8 +432,9 @@ def connectBridge(monitor,silent=False):
globals.connected = False
return None


def validateSchedule():
logger.debug("Checking if schedule is valid. Enabled: {}".format(globals.enableSchedule))
logger.debug("Validate schedule. Schedule Enabled: {}".format(globals.enableSchedule))
if globals.enableSchedule:
try:
kodiutils.convertTime(globals.startTime)
Expand All @@ -449,6 +446,7 @@ def validateSchedule():
kodiutils.set_setting("EnableSchedule", False)
globals.enableSchedule = False


def getLightGamut(bridge,L):
try:
gamut = bridge.lights()[L]['capabilities']['control']['colorgamuttype']
Expand All @@ -461,11 +459,27 @@ def getLightGamut(bridge,L):
return None


def checkBridgeModel(bridge):
try:
bridgeConfig = bridge.config()
model=bridgeConfig["modelid"]
except QhueException:
logger.exception("Exception: checkBridgeModel")
return None
if model == "BSB002":
logger.debug("Bridge model OK: {}".format(model))
return True
logger.error("Unsupported bridge model: {}".format(model))
xbmcgui.Dialog().ok(_("Unsupported Hue Bridge"),_("Hue Bridge V1 (Round) is unsupported. Hue Bridge V2 (Square) is required for certain features."))
return None


class HueMonitor(xbmc.Monitor):
def __init__(self):
super(xbmc.Monitor,self).__init__()

def onSettingsChanged(self):
logger.debug("Settings changed")
self.waitForAbort(1)
loadSettings()
globals.settingsChanged = True
4 changes: 2 additions & 2 deletions script.service.hue/resources/lib/kodiutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from . import globals



def notification(header, message, time=5000, icon=globals.ADDON.getAddonInfo('icon'), sound=True):
xbmcgui.Dialog().notification(header, message, icon, time, sound)

Expand All @@ -25,11 +24,13 @@ def get_setting(setting):
def set_setting(setting, value):
globals.ADDON.setSetting(setting, str(value))


def convertTime(time):
hour=int(time.split(":")[0])
minute=int(time.split(":")[1])
return datetime.time(hour,minute)


def get_setting_as_bool(setting):
return get_setting(setting).lower() == "true"

Expand All @@ -50,4 +51,3 @@ def get_setting_as_int(setting):

def get_string(string_id):
return globals.ADDON.getLocalizedString(string_id)

1 change: 1 addition & 0 deletions script.service.hue/resources/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<setting id="group3_Interval" type="slider" label="30065" default="100" range="0,100,1000" option="int" visible="eq(-14,true)" />
<setting id="group3_TransitionTime" type="slider" label="30066" default="100" range="0,100,500" option="int" visible="eq(-15,true)"/>
<setting id="group3_CaptureSize" type="slider" label="30067" default="150" range="50,50,500" option="int" visible="eq(-16,true)"/>
<setting id="group3_ColorDifference" type="slider" label="Minimum colour difference" default="150" range="0,0.0001,1" option="float" visible="eq(-17,true)"/>

</category>
<!-- /GROUP SECTION 0-->
Expand Down

0 comments on commit fc21e4a

Please sign in to comment.