Skip to content

Commit

Permalink
Fixed dependency misconfiguration and paper input model not being fet…
Browse files Browse the repository at this point in the history
…ched
  • Loading branch information
alryaz committed Jan 31, 2020
1 parent 5b53491 commit 7eadac3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
26 changes: 23 additions & 3 deletions custom_components/snmp_device/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async def async_step_discovered_select(self, user_input=None):
configured_devices = self.hass.data.get(DATA_DEVICE_CONFIGS)
discovered_choices = dict()
for (host, port), description in all_devices.items():
if configured_devices and (host, port) in configured_devices:
if self._check_entity_exists(host, port):
configured_num += 1
continue

Expand Down Expand Up @@ -147,6 +147,9 @@ async def async_step_device(self, user_input=None):
data_schema=vol.Schema(schema)
)

if self._check_entity_exists(user_input[CONF_HOST], i_c[CONF_PORT]):
return self.async_abort(reason='already_configured')

from pysnmp.hlapi import SnmpEngine, UdpTransportTarget, ContextData, CommunityData
from importlib import import_module
if TYPE_CHECKING:
Expand Down Expand Up @@ -185,15 +188,32 @@ async def async_step_device(self, user_input=None):
_LOGGER.exception('Error while connecting to device')
return self.async_abort(reason='connection_failed')

return self.async_create_entry(
return self._async_final_create_entry(
title=i_c[CONF_NAME],
data=i_c,
)

def _check_entity_exists(self, host, port):
for entry in self.hass.config_entries.async_entries(DOMAIN):
if entry.data[CONF_HOST] == host and entry.data[CONF_PORT] == port:
return True
return False

def _async_final_create_entry(self, title, data):
"""Return a set of the configured hosts."""
_LOGGER.debug('afce %s %s', title, data)
if self._check_entity_exists(data[CONF_HOST], data[CONF_PORT]):
return self.async_abort(reason='already_configured')

return self.async_create_entry(
title=title,
data=data
)

async def async_step_import(self, config: ConfigType):
"""Import a config entry from configuration.yaml."""
_LOGGER.debug('Import entry: %s' % config)
return self.async_create_entry(
return self._async_final_create_entry(
title=(config.get(CONF_NAME) or config[CONF_HOST]) + ' (yaml)',
data={
CONF_HOST: config[CONF_HOST],
Expand Down
2 changes: 1 addition & 1 deletion custom_components/snmp_device/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "SNMP Device",
"documentation": "https://github.com/alryaz/hass-component-snmp-device",
"requirements": [
"pysnmp==4.4.4"
"pysnmp==4.4.12"
],
"config_flow": true,
"dependencies": [],
Expand Down
12 changes: 6 additions & 6 deletions custom_components/snmp_device/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ async def update_entities(*_):

except Exception as e:
_LOGGER.warning('Device unavailable, retrying later')
_LOGGER.debug('retry reason: %s' % str(e))
_LOGGER.exception('retry reason: %s' % str(e))

raise PlatformNotReady

async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry, async_add_devices):
_LOGGER.debug('Setting up entry %s for component %s' % (config_entry.entry_id, SENSOR_DOMAIN))
host = config_entry.data[CONF_HOST]
port = config_entry.data[CONF_PORT]
_LOGGER.debug('Setting up %s entry %s for %s:%d' % (SENSOR_DOMAIN, config_entry.entry_id, host, port))
config = hass.data[DATA_DEVICE_CONFIGS][(host, port)]

return await async_setup_platform(
Expand Down Expand Up @@ -421,7 +421,7 @@ def unique_id(self) -> Optional[str]:
DOMAIN,
self._host,
self._port,
SENSOR_DOMAIN,
self.__class__.__name__,
self._sensor_type,
]
if self._entity_index is not None:
Expand Down Expand Up @@ -502,13 +502,12 @@ class SNMPPrinterSensor(_SNMPSensor):
'tonality': ('1.3.6.1.2.1.43.12.1.1.5.1', int),
},
('paper_inputs', True): {
'model': ('1.3.6.1.2.1.43.8.2.1.18.1', str),
'type': ('1.3.6.1.2.1.43.8.2.1.2.1', PaperInputType),
'unit': ('1.3.6.1.2.1.43.8.2.1.8.1', CapacityUnitType),
'capacity': ('1.3.6.1.2.1.43.8.2.1.9.1', CAPACITY_LEVEL_TYPE),
'level': ('1.3.6.1.2.1.43.8.2.1.10.1', CAPACITY_LEVEL_TYPE),
'media': ('1.3.6.1.2.1.43.8.2.1.12.1', lambda x: bytes(x).decode('utf-8')),
'serial': ('1.3.6.1.2.1.43.8.2.1.17.1', str),
'model': ('1.3.6.1.2.1.43.8.2.1.18.1', str),
#'media': ('1.3.6.1.2.1.43.8.2.1.12.1', lambda x: bytes(x).decode('utf-8')),
},
}

Expand Down Expand Up @@ -540,6 +539,7 @@ def update_sensor_attributes(self, new_data):
new_attributes = self._attributes
new_unit = self._unit_of_measurement
new_name = self._name

if self._sensor_type == SENSOR_TYPE_STATUS:
new_name = 'Status'
sensor_data = new_data['info']
Expand Down

0 comments on commit 7eadac3

Please sign in to comment.