fix scanning

This commit is contained in:
Andrey
2021-01-25 17:18:42 +03:00
parent 57f355d479
commit ff6225a959
5 changed files with 32 additions and 8 deletions

View File

@@ -25,7 +25,7 @@ STEP_USER_DATA_SCHEMA = vol.Schema(
vol.Optional(CONF_PORT_TO_SCAN, default=0): int,
vol.Optional(CONF_MQTT_INPUTS, default=True): bool,
vol.Optional(CONF_NPORTS, default=37): int,
vol.Optional(CONF_UPDATE_ALL, default=True): bool,
# vol.Optional(CONF_UPDATE_ALL, default=True): bool,
},
)

View File

@@ -123,10 +123,12 @@ class MegaD:
async with self.lck:
self.entities.append(ent)
async def get_sensors(self):
async def get_sensors(self, only_list=False):
self.lg.debug(self.sensors)
ports = []
for x in self.sensors:
if only_list and x.http_cmd != 'list':
continue
if x.port in ports:
continue
await self.get_port(x.port, force_http=True, http_cmd=x.http_cmd)
@@ -161,7 +163,7 @@ class MegaD:
self.lg.debug('poll')
if self.mqtt is None:
await self.get_all_ports()
await self.get_sensors()
await self.get_sensors(only_list=True)
return
if len(self.sensors) > 0:
await self.get_sensors()
@@ -193,7 +195,9 @@ class MegaD:
self.lg.warning('%s returned %s (%s)', url, req.status, await req.text())
return None
else:
return await req.text()
ret = await req.text()
self.lg.debug('response %s', ret)
return ret
async def save(self):
await self.send_command(cmd='s')
@@ -223,6 +227,11 @@ class MegaD:
if self.mqtt is None or force_http:
ret = await self.request(pt=port, cmd=http_cmd)
ret = self.parse_response(ret)
self.lg.debug('parsed: %s', ret)
if http_cmd == 'list' and isinstance(ret, dict) and 'value' in ret:
await asyncio.sleep(1)
ret = await self.request(pt=port, http_cmd=http_cmd)
ret = self.parse_response(ret)
self.values[port] = ret
return ret
@@ -368,7 +377,7 @@ class MegaD:
try:
http_cmd = 'get'
values = await self.get_port(port, force_http=True)
if values is None:
if values is None or (isinstance(values, dict) and str(values.get('value')) in ('', 'None')):
values = await self.get_port(port, force_http=True, http_cmd='list')
http_cmd = 'list'
except asyncio.TimeoutError:

View File

@@ -117,6 +117,7 @@ class Mega1WSensor(MegaPushEntity):
self._device_class = device_class
self._unit_of_measurement = unit_of_measurement
self.mega.sensors.append(self)
self.http_cmd = http_cmd
@property
def unit_of_measurement(self):
@@ -143,12 +144,24 @@ class Mega1WSensor(MegaPushEntity):
@property
def state(self):
ret = None
if self.key:
ret = self.mega.values.get(self.port, {}).get('value', {}).get(self.key)
try:
ret = self.mega.values.get(self.port, {})
if isinstance(ret, dict):
ret = ret.get(self.key)
except:
self.lg.error(self.mega.values.get(self.port, {}).get('value', {}))
return
else:
ret = self.mega.values.get(self.port, {}).get('value')
if ret is None and self._state is not None:
ret = self._state.state
try:
ret = float(ret)
ret = str(ret)
except:
ret = None
return ret
@property

View File

@@ -37,7 +37,8 @@
"reload": "[%key:common::config_flow::data::reload%]",
"invert": "[%key:common::config_flow::data::invert%]",
"mqtt_inputs": "[%key:common::config_flow::data::mqtt_inputs%]",
"nports": "[%key:common::config_flow::data::nports%]"
"nports": "[%key:common::config_flow::data::nports%]",
"update_all": "[%key:common::config_flow::data::update_all%]"
}
}
}

View File

@@ -21,7 +21,8 @@
"scan_interval": "Scan interval (sec), 0 - don't update",
"port_to_scan": "Port to poll aliveness (needed only if no sensors used)",
"nports": "Number of ports",
"update_all": "Update all outs when input"
"update_all": "Update all outs when input",
"mqtt_inputs": "Use MQTT"
}
}
}