Skip to content

Commit

Permalink
Reworked how to obtain unique_id.
Browse files Browse the repository at this point in the history
Implemented fallback if mac address cannot be obtained.
  • Loading branch information
tschamm committed Feb 13, 2021
1 parent 7dee73e commit 3ee577b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
67 changes: 37 additions & 30 deletions boschshcpy/information.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from zeroconf import Error as ZeroconfError
from zeroconf import IPVersion, ServiceBrowser, ServiceInfo, ServiceStateChange, current_time_millis

from boschshcpy.exceptions import SHCConnectionError, SHCmDNSError
from boschshcpy.exceptions import SHCConnectionError, SHCmDNSError, SHCAuthenticationError

logger = logging.getLogger("boschshcpy")

Expand Down Expand Up @@ -55,43 +55,42 @@ class UpdateState(Enum):
UPDATE_IN_PROGRESS = "UPDATE_IN_PROGRESS"
UPDATE_AVAILABLE = "UPDATE_AVAILABLE"

def __init__(self, api, raw_information, zeroconf=None):
def __init__(self, api, zeroconf=None):
self._api = api
self._raw_information = raw_information
self._mac_address = None
self._unique_id = None
self._name = None
self._pub_info = self._api.get_publicinformation()
if self._pub_info == None:
raise SHCAuthenticationError

if zeroconf is not None:
self._listener = SHCListener(zeroconf, self.filter)
else:
self.get_mac(self._api.controller_ip)
self.get_unique_id(zeroconf)

@property
def version(self):
return self._raw_information["version"]
return self._pub_info["softwareUpdateState"]["swInstalledVersion"]

@property
def updateState(self) -> UpdateState:
return self.UpdateState(self._raw_information["updateState"])
return self.UpdateState(self._pub_info["softwareUpdateState"]["swUpdateState"])

@property
def connectivityVersion(self):
return self._raw_information["connectivityVersion"]
def shcIpAddress(self):
return self._pub_info["shcIpAddress"]

@property
def name(self):
return self._name

@property
def mac_address(self):
return self._mac_address
def unique_id(self):
return self._unique_id

def filter(self, service_info: ServiceInfo):
mac_address = None
name = None

try:
host_ip = socket.gethostbyname(self._api.controller_ip)
host_ip = socket.gethostbyname(self.shcIpAddress)
except Exception as e:
raise SHCConnectionError

Expand All @@ -104,29 +103,37 @@ def filter(self, service_info: ServiceInfo):
name = info.server[:server_pos]
if mac_address is None or name is None:
raise SHCmDNSError
self._mac_address = format_mac(mac_address)
self._unique_id = format_mac(mac_address)
self._name = name

def get_mac(self, host):
"""Get the mac address of the given host."""
try:
mac_address = get_mac_address(ip=host)
if not mac_address:
mac_address = get_mac_address(hostname=host)
except Exception as err: # pylint: disable=broad-except
logger.exception("Unable to get mac address: %s", err)
mac_address = None
if mac_address is None:
raise SHCmDNSError
self._mac_address = format_mac(mac_address)
self._name = host

def get_unique_id(self, zeroconf):
if zeroconf is not None:
self._listener = SHCListener(zeroconf, self.filter)
else:
host = self.shcIpAddress
try:
mac_address = get_mac_address(ip=host)
if not mac_address:
mac_address = get_mac_address(hostname=host)
except Exception as err: # pylint: disable=broad-except
logger.exception("Unable to get mac address: %s", err)
mac_address = None
if mac_address is not None:
self._unique_id = format_mac(mac_address)
else:
self._unique_id = host
logger.warning("Cannot obtain unique id, using IP address '%s' instead. Please make sure the IP stays the same!", host)
self._name = host

def summary(self):
print(f"Information:")
print(f" shcIpAddress : {self.shcIpAddress}")
print(f" SW-Version : {self.version}")
print(f" updateState : {self.updateState}")
print(f" connectivityVersion: {self.connectivityVersion}")
print(f" macAddress : {self.mac_address}")
print(f" updateState : {self.updateState.name}")
print(f" uniqueId : {self.unique_id}")
print(f" name : {self.name}")


Expand Down
12 changes: 4 additions & 8 deletions boschshcpy/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,22 +228,18 @@ def scenario(self, scenario_id) -> SHCScenario:
return self._scenarios_by_id[scenario_id]

def authenticate(self):
raw_information = self._api.get_shcinformation()
if raw_information is None:
raise SHCAuthenticationError

try:
self._shc_information = SHCInformation(
api=self._api, raw_information=raw_information, zeroconf=self._zeroconf
api=self._api, zeroconf=self._zeroconf
)
except SHCmDNSError:
self._shc_information = SHCInformation(api=self._api, raw_information=raw_information)
self._shc_information = SHCInformation(api=self._api)

def mdns_info(self) -> SHCInformation:
try:
return SHCInformation(api=self._api, raw_information=None, zeroconf=self._zeroconf)
return SHCInformation(api=self._api, zeroconf=self._zeroconf)
except SHCmDNSError:
return SHCInformation(api=self._api, raw_information=None)
return SHCInformation(api=self._api)

@property
def information(self) -> SHCInformation:
Expand Down
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="boschshcpy",
version="0.2.4",
version="0.2.5",
url="https://github.com/tschamm/boschshcpy",
author="Clemens-Alexander Brust, Thomas Schamm",
author_email="cabrust@pm.me, thomas@tschamm.de",
Expand Down

0 comments on commit 3ee577b

Please sign in to comment.