Skip to content

Commit

Permalink
Fix the lightning sensor to be imperial/metric.
Browse files Browse the repository at this point in the history
Finally fix the problem where flaky sensors that are often out of range
don't get setup properly because they aren't in the first connection packet.
Update the docs to remove the warning, because we suck less now.
  • Loading branch information
garbled1 committed Nov 13, 2020
1 parent e6ac54f commit 702634e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 27 additions & 2 deletions custom_components/ecowitt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
CONF_UNIT_WIND = "windunit"
CONF_UNIT_RAIN = "rainunit"
CONF_UNIT_WINDCHILL = "windchillunit"
CONF_UNIT_LIGHTNING = "lightningunit"

TYPE_BAROMABSHPA = "baromabshpa"
TYPE_BAROMRELHPA = "baromrelhpa"
Expand Down Expand Up @@ -547,6 +548,8 @@
default=CONF_UNIT_SYSTEM_IMPERIAL): cv.string,
vol.Optional(CONF_UNIT_RAIN,
default=CONF_UNIT_SYSTEM_IMPERIAL): cv.string,
vol.Optional(CONF_UNIT_LIGHTNING,
default=CONF_UNIT_SYSTEM_IMPERIAL): cv.string,
vol.Optional(CONF_UNIT_WINDCHILL,
default=W_TYPE_HYBRID): cv.string,
}
Expand Down Expand Up @@ -625,24 +628,31 @@ def check_imp_metric_sensor(sensor):
if (conf[CONF_UNIT_WIND] == CONF_UNIT_SYSTEM_METRIC
and metric == S_IMPERIAL):
return False
if (sensor == 'lightning'
and conf[CONF_UNIT_LIGHTNING] == CONF_UNIT_SYSTEM_IMPERIAL):
return False
if (sensor == 'lightning_mi'
and conf[CONF_UNIT_LIGHTNING] == CONF_UNIT_SYSTEM_METRIC):
return False
return True

def check_and_append_sensor(sensor):
"""Check the sensor for validity, and append to appropriate list."""
if sensor not in SENSOR_TYPES:
if sensor not in IGNORED_SENSORS:
_LOGGER.warning("Unhandled sensor type %s", sensor)
return
return None

# Is this a metric or imperial sensor, lookup and skip
if not check_imp_metric_sensor(sensor):
return
return None

name, uom, kind, device_class, icon, metric = SENSOR_TYPES[sensor]
if kind == TYPE_SENSOR:
sensor_sensors.append(sensor)
if kind == TYPE_BINARY_SENSOR:
binary_sensors.append(sensor)
return(kind)

async def _first_data_rec(weather_data):
_LOGGER.info("First ecowitt data recd, setting up sensors.")
Expand Down Expand Up @@ -700,6 +710,21 @@ async def _async_ecowitt_update_cb(weather_data):
and check_imp_metric_sensor(sensor)):
_LOGGER.warning("Unregistered sensor type %s value %s received.",
sensor, weather_data[sensor])
# try to register the sensor
new_sensor = []
new_sensor.append(sensor)
kind = check_and_append_sensor(sensor)
if kind == TYPE_SENSOR:
hass.async_create_task(
async_load_platform(hass, "sensor", DOMAIN,
new_sensor, config)
)
if kind == TYPE_BINARY_SENSOR:
hass.async_create_task(
async_load_platform(hass, "binary_sensor", DOMAIN,
new_sensor, config)
)

async_dispatcher_send(hass, DOMAIN)

ws.register_listener(_async_ecowitt_update_cb)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ecowitt/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, hass, key, name, dc, uom, icon):
def state(self):
"""Return the state of the sensor."""
if self._key in self._ws.last_values:
# Im concerned this is nonsense due to TZ...
# The lightning time is reported in UTC, hooray.
if self._dc == DEVICE_CLASS_TIMESTAMP:
return dt_util.as_local(
dt_util.utc_from_timestamp(self._ws.last_values[self._key])
Expand Down

0 comments on commit 702634e

Please sign in to comment.