mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
components.knx - KNXMultiAddressDevice corrections (#8275)
1. The has_attributes was comparing names to addresses 2. Some errors outside of an except block were using _LOGGER.except. This will cause an exception itself because there is no trance context available to the logger 3. Added alias names for the address and state addresses so that they can be accessed with the same 4. Added return values for the set_int_value and set_percentage methods to allow error checking similar to the set_value method 5. Added the name of the configured object to the log messages to make them more meaningful (otherwise multiple similar log messages are received without any hint as to the target device)
This commit is contained in:
parent
e6e0e5263a
commit
c13fdd23c1
@ -51,7 +51,7 @@ def setup(hass, config):
|
|||||||
res = KNXTUNNEL.connect()
|
res = KNXTUNNEL.connect()
|
||||||
_LOGGER.debug("Res = %s", res)
|
_LOGGER.debug("Res = %s", res)
|
||||||
if not res:
|
if not res:
|
||||||
_LOGGER.exception("Could not connect to KNX/IP interface %s", host)
|
_LOGGER.error("Could not connect to KNX/IP interface %s", host)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
except KNXException as ex:
|
except KNXException as ex:
|
||||||
@ -127,7 +127,10 @@ class KNXGroupAddress(Entity):
|
|||||||
self._config = config
|
self._config = config
|
||||||
self._state = False
|
self._state = False
|
||||||
self._data = None
|
self._data = None
|
||||||
_LOGGER.debug("Initalizing KNX group address %s", self.address)
|
_LOGGER.debug(
|
||||||
|
"Initalizing KNX group address for %s (%s)",
|
||||||
|
self.name, self.address
|
||||||
|
)
|
||||||
|
|
||||||
def handle_knx_message(addr, data):
|
def handle_knx_message(addr, data):
|
||||||
"""Handle an incoming KNX frame.
|
"""Handle an incoming KNX frame.
|
||||||
@ -198,11 +201,15 @@ class KNXGroupAddress(Entity):
|
|||||||
self._data = res
|
self._data = res
|
||||||
else:
|
else:
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"Unable to read from KNX address: %s (None)", self.address)
|
"%s: unable to read from KNX address: %s (None)",
|
||||||
|
self.name, self.address
|
||||||
|
)
|
||||||
|
|
||||||
except KNXException:
|
except KNXException:
|
||||||
_LOGGER.exception(
|
_LOGGER.exception(
|
||||||
"Unable to read from KNX address: %s", self.address)
|
"%s: unable to read from KNX address: %s",
|
||||||
|
self.name, self.address
|
||||||
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@ -229,20 +236,44 @@ class KNXMultiAddressDevice(Entity):
|
|||||||
self._config = config
|
self._config = config
|
||||||
self._state = False
|
self._state = False
|
||||||
self._data = None
|
self._data = None
|
||||||
_LOGGER.debug("Initalizing KNX multi address device")
|
_LOGGER.debug(
|
||||||
|
"%s: initalizing KNX multi address device",
|
||||||
|
self.name
|
||||||
|
)
|
||||||
|
|
||||||
settings = self._config.config
|
settings = self._config.config
|
||||||
|
if config.address:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"%s: base address: address=%s",
|
||||||
|
self.name, settings.get('address')
|
||||||
|
)
|
||||||
|
self.names[config.address] = 'base'
|
||||||
|
if config.state_address:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"%s, state address: state_address=%s",
|
||||||
|
self.name, settings.get('state_address')
|
||||||
|
)
|
||||||
|
self.names[config.state_address] = 'state'
|
||||||
|
|
||||||
# parse required addresses
|
# parse required addresses
|
||||||
for name in required:
|
for name in required:
|
||||||
_LOGGER.info(name)
|
|
||||||
paramname = '{}{}'.format(name, '_address')
|
paramname = '{}{}'.format(name, '_address')
|
||||||
addr = settings.get(paramname)
|
addr = settings.get(paramname)
|
||||||
if addr is None:
|
if addr is None:
|
||||||
_LOGGER.exception(
|
_LOGGER.error(
|
||||||
"Required KNX group address %s missing", paramname)
|
"%s: Required KNX group address %s missing",
|
||||||
|
self.name, paramname
|
||||||
|
)
|
||||||
raise KNXException(
|
raise KNXException(
|
||||||
"Group address for %s missing in configuration", paramname)
|
"%s: Group address for {} missing in "
|
||||||
_LOGGER.debug("%s: %s=%s", settings.get('name'), paramname, addr)
|
"configuration for {}".format(
|
||||||
|
self.name, paramname
|
||||||
|
)
|
||||||
|
)
|
||||||
|
_LOGGER.debug(
|
||||||
|
"%s: (required parameter) %s=%s",
|
||||||
|
self.name, paramname, addr
|
||||||
|
)
|
||||||
addr = parse_group_address(addr)
|
addr = parse_group_address(addr)
|
||||||
self.names[addr] = name
|
self.names[addr] = name
|
||||||
|
|
||||||
@ -250,12 +281,18 @@ class KNXMultiAddressDevice(Entity):
|
|||||||
for name in optional:
|
for name in optional:
|
||||||
paramname = '{}{}'.format(name, '_address')
|
paramname = '{}{}'.format(name, '_address')
|
||||||
addr = settings.get(paramname)
|
addr = settings.get(paramname)
|
||||||
_LOGGER.debug("%s: %s=%s", settings.get('name'), paramname, addr)
|
_LOGGER.debug(
|
||||||
|
"%s: (optional parameter) %s=%s",
|
||||||
|
self.name, paramname, addr
|
||||||
|
)
|
||||||
if addr:
|
if addr:
|
||||||
try:
|
try:
|
||||||
addr = parse_group_address(addr)
|
addr = parse_group_address(addr)
|
||||||
except KNXException:
|
except KNXException:
|
||||||
_LOGGER.exception("Cannot parse group address %s", addr)
|
_LOGGER.exception(
|
||||||
|
"%s: cannot parse group address %s",
|
||||||
|
self.name, addr
|
||||||
|
)
|
||||||
self.names[addr] = name
|
self.names[addr] = name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -283,7 +320,7 @@ class KNXMultiAddressDevice(Entity):
|
|||||||
|
|
||||||
This is mostly important for optional addresses.
|
This is mostly important for optional addresses.
|
||||||
"""
|
"""
|
||||||
for attributename, dummy_attribute in self.names.items():
|
for attributename in self.names.values():
|
||||||
if attributename == name:
|
if attributename == name:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
@ -296,7 +333,7 @@ class KNXMultiAddressDevice(Entity):
|
|||||||
percentage = abs(percentage) # only accept positive values
|
percentage = abs(percentage) # only accept positive values
|
||||||
scaled_value = percentage * 255 / 100
|
scaled_value = percentage * 255 / 100
|
||||||
value = min(255, scaled_value)
|
value = min(255, scaled_value)
|
||||||
self.set_int_value(name, value)
|
return self.set_int_value(name, value)
|
||||||
|
|
||||||
def get_percentage(self, name):
|
def get_percentage(self, name):
|
||||||
"""Get a percentage from knx for a given attribute.
|
"""Get a percentage from knx for a given attribute.
|
||||||
@ -312,7 +349,7 @@ class KNXMultiAddressDevice(Entity):
|
|||||||
# KNX packets are big endian
|
# KNX packets are big endian
|
||||||
value = round(value) # only accept integers
|
value = round(value) # only accept integers
|
||||||
b_value = value.to_bytes(num_bytes, byteorder='big')
|
b_value = value.to_bytes(num_bytes, byteorder='big')
|
||||||
self.set_value(name, list(b_value))
|
return self.set_value(name, list(b_value))
|
||||||
|
|
||||||
def get_int_value(self, name):
|
def get_int_value(self, name):
|
||||||
"""Get an integer value for a given attribute."""
|
"""Get an integer value for a given attribute."""
|
||||||
@ -340,13 +377,21 @@ class KNXMultiAddressDevice(Entity):
|
|||||||
addr = attributeaddress
|
addr = attributeaddress
|
||||||
|
|
||||||
if addr is None:
|
if addr is None:
|
||||||
_LOGGER.exception("Attribute %s undefined", name)
|
_LOGGER.error("%s: attribute '%s' undefined",
|
||||||
|
self.name, name)
|
||||||
|
_LOGGER.debug(
|
||||||
|
"%s: defined attributes: %s",
|
||||||
|
self.name, str(self.names)
|
||||||
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = KNXTUNNEL.group_read(addr, use_cache=self.cache)
|
res = KNXTUNNEL.group_read(addr, use_cache=self.cache)
|
||||||
except KNXException:
|
except KNXException:
|
||||||
_LOGGER.exception("Unable to read from KNX address: %s", addr)
|
_LOGGER.exception(
|
||||||
|
"%s: unable to read from KNX address: %s",
|
||||||
|
self.name, addr
|
||||||
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return res
|
return res
|
||||||
@ -361,13 +406,21 @@ class KNXMultiAddressDevice(Entity):
|
|||||||
addr = attributeaddress
|
addr = attributeaddress
|
||||||
|
|
||||||
if addr is None:
|
if addr is None:
|
||||||
_LOGGER.exception("Attribute %s undefined", name)
|
_LOGGER.error("%s: attribute '%s' undefined",
|
||||||
|
self.name, name)
|
||||||
|
_LOGGER.debug(
|
||||||
|
"%s: defined attributes: %s",
|
||||||
|
self.name, str(self.names)
|
||||||
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
KNXTUNNEL.group_write(addr, value)
|
KNXTUNNEL.group_write(addr, value)
|
||||||
except KNXException:
|
except KNXException:
|
||||||
_LOGGER.exception("Unable to write to KNX address: %s", addr)
|
_LOGGER.exception(
|
||||||
|
"%s: unable to write to KNX address: %s",
|
||||||
|
self.name, addr
|
||||||
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user