Skip to content

Commit

Permalink
Add notion of bridge ID (#21)
Browse files Browse the repository at this point in the history
* Add notion of bridge ID

* Version bump to 1.10.0
  • Loading branch information
balloob authored Dec 16, 2019
1 parent d3a03ab commit 8663445
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
11 changes: 10 additions & 1 deletion aiohue/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
class Bridge:
"""Control a Hue bridge."""

def __init__(self, host, websession, *, username=None):
def __init__(self, host, websession, *, username=None, bridge_id=None):
self.host = host
self.username = username
self.websession = websession
self._bridge_id = bridge_id

self.config = None
self.groups = None
Expand All @@ -28,6 +29,14 @@ def __init__(self, host, websession, *, username=None):
# self.rules = None
# self.schedules = None

@property
def id(self):
"""Return the ID of the bridge."""
if self.config is not None:
return self.config.bridgeid

return self._bridge_id

async def create_user(self, device_type):
"""Create a user.
Expand Down
5 changes: 4 additions & 1 deletion aiohue/discovery.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from .bridge import Bridge
from .util import normalize_bridge_id

URL_NUPNP = 'https://www.meethue.com/api/nupnp'


async def discover_nupnp(websession):
"""Discover bridges via NUPNP."""
async with websession.get(URL_NUPNP) as res:
return [Bridge(item['internalipaddress'], websession=websession)
return [Bridge(item['internalipaddress'],
bridge_id=normalize_bridge_id(item['id']),
websession=websession)
for item in (await res.json())]
23 changes: 23 additions & 0 deletions aiohue/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Utils for aiohue."""
import logging


def normalize_bridge_id(bridge_id: str):
"""Normalize a bridge identifier."""
# zeroconf: properties['id'], field contains semicolons after each 2 char
if len(bridge_id) == 17 and sum(True for c in "aa:bb:cc:dd:ee:ff"
if c == ":"):
return bridge_id.replace(':', '')

# nupnp: contains 4 extra characters in the middle: "fffe"
if len(bridge_id) == 16 and "fffe" in bridge_id:
return bridge_id[0:6] + bridge_id[-6:]

# SSDP/UPNP and Hue Bridge API contains right ID.
if len(bridge_id) == 12:
return bridge_id

logging.getLogger(__name__).warn("Received unexpected bridge id: %s",
bridge_id)

return bridge_id
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='aiohue',
version='1.9.2',
version='1.10.0',
license='Apache License 2.0',
url='https://github.com/balloob/aiohue',
author='Paulus Schoutsen',
Expand Down

0 comments on commit 8663445

Please sign in to comment.