Eliminate redundant code in greeneye_monitor (#63661)
parent
4d3f70ad05
commit
15f1a850ba
|
@ -9,7 +9,6 @@ import voluptuous as vol
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_PORT,
|
CONF_PORT,
|
||||||
CONF_SENSOR_TYPE,
|
|
||||||
CONF_SENSORS,
|
CONF_SENSORS,
|
||||||
CONF_TEMPERATURE_UNIT,
|
CONF_TEMPERATURE_UNIT,
|
||||||
EVENT_HOMEASSISTANT_STOP,
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
|
@ -26,7 +25,6 @@ from .const import (
|
||||||
CONF_CHANNELS,
|
CONF_CHANNELS,
|
||||||
CONF_COUNTED_QUANTITY,
|
CONF_COUNTED_QUANTITY,
|
||||||
CONF_COUNTED_QUANTITY_PER_PULSE,
|
CONF_COUNTED_QUANTITY_PER_PULSE,
|
||||||
CONF_MONITOR_SERIAL_NUMBER,
|
|
||||||
CONF_MONITORS,
|
CONF_MONITORS,
|
||||||
CONF_NET_METERING,
|
CONF_NET_METERING,
|
||||||
CONF_NUMBER,
|
CONF_NUMBER,
|
||||||
|
@ -37,10 +35,6 @@ from .const import (
|
||||||
CONF_VOLTAGE_SENSORS,
|
CONF_VOLTAGE_SENSORS,
|
||||||
DATA_GREENEYE_MONITOR,
|
DATA_GREENEYE_MONITOR,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SENSOR_TYPE_CURRENT,
|
|
||||||
SENSOR_TYPE_PULSE_COUNTER,
|
|
||||||
SENSOR_TYPE_TEMPERATURE,
|
|
||||||
SENSOR_TYPE_VOLTAGE,
|
|
||||||
TEMPERATURE_UNIT_CELSIUS,
|
TEMPERATURE_UNIT_CELSIUS,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -134,57 +128,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
|
|
||||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, close_monitors)
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, close_monitors)
|
||||||
|
|
||||||
all_sensors = []
|
num_sensors = 0
|
||||||
for monitor_config in server_config[CONF_MONITORS]:
|
for monitor_config in config[DOMAIN][CONF_MONITORS]:
|
||||||
monitor_serial_number = {
|
num_sensors += len(monitor_config[CONF_CHANNELS])
|
||||||
CONF_MONITOR_SERIAL_NUMBER: monitor_config[CONF_SERIAL_NUMBER]
|
num_sensors += len(monitor_config[CONF_PULSE_COUNTERS])
|
||||||
}
|
num_sensors += len(monitor_config[CONF_TEMPERATURE_SENSORS][CONF_SENSORS])
|
||||||
|
num_sensors += len(monitor_config[CONF_VOLTAGE_SENSORS])
|
||||||
|
|
||||||
channel_configs = monitor_config[CONF_CHANNELS]
|
if num_sensors == 0:
|
||||||
for channel_config in channel_configs:
|
|
||||||
all_sensors.append(
|
|
||||||
{
|
|
||||||
CONF_SENSOR_TYPE: SENSOR_TYPE_CURRENT,
|
|
||||||
**monitor_serial_number,
|
|
||||||
**channel_config,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
voltage_configs = monitor_config[CONF_VOLTAGE_SENSORS]
|
|
||||||
for voltage_config in voltage_configs:
|
|
||||||
all_sensors.append(
|
|
||||||
{
|
|
||||||
CONF_SENSOR_TYPE: SENSOR_TYPE_VOLTAGE,
|
|
||||||
**monitor_serial_number,
|
|
||||||
**voltage_config,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
if sensor_configs := monitor_config[CONF_TEMPERATURE_SENSORS]:
|
|
||||||
temperature_unit = {
|
|
||||||
CONF_TEMPERATURE_UNIT: sensor_configs[CONF_TEMPERATURE_UNIT]
|
|
||||||
}
|
|
||||||
for sensor_config in sensor_configs[CONF_SENSORS]:
|
|
||||||
all_sensors.append(
|
|
||||||
{
|
|
||||||
CONF_SENSOR_TYPE: SENSOR_TYPE_TEMPERATURE,
|
|
||||||
**monitor_serial_number,
|
|
||||||
**temperature_unit,
|
|
||||||
**sensor_config,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
counter_configs = monitor_config[CONF_PULSE_COUNTERS]
|
|
||||||
for counter_config in counter_configs:
|
|
||||||
all_sensors.append(
|
|
||||||
{
|
|
||||||
CONF_SENSOR_TYPE: SENSOR_TYPE_PULSE_COUNTER,
|
|
||||||
**monitor_serial_number,
|
|
||||||
**counter_config,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
if not all_sensors:
|
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Configuration must specify at least one "
|
"Configuration must specify at least one "
|
||||||
"channel, voltage, pulse counter or temperature sensor"
|
"channel, voltage, pulse counter or temperature sensor"
|
||||||
|
@ -192,7 +143,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
async_load_platform(hass, "sensor", DOMAIN, {CONF_SENSORS: all_sensors}, config)
|
async_load_platform(hass, "sensor", DOMAIN, config[DOMAIN], config)
|
||||||
)
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -8,7 +8,6 @@ import greeneye
|
||||||
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_SENSOR_TYPE,
|
|
||||||
CONF_SENSORS,
|
CONF_SENSORS,
|
||||||
CONF_TEMPERATURE_UNIT,
|
CONF_TEMPERATURE_UNIT,
|
||||||
ELECTRIC_POTENTIAL_VOLT,
|
ELECTRIC_POTENTIAL_VOLT,
|
||||||
|
@ -22,17 +21,18 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import DiscoveryInfoType
|
from homeassistant.helpers.typing import DiscoveryInfoType
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
|
CONF_CHANNELS,
|
||||||
CONF_COUNTED_QUANTITY,
|
CONF_COUNTED_QUANTITY,
|
||||||
CONF_COUNTED_QUANTITY_PER_PULSE,
|
CONF_COUNTED_QUANTITY_PER_PULSE,
|
||||||
CONF_MONITOR_SERIAL_NUMBER,
|
CONF_MONITORS,
|
||||||
CONF_NET_METERING,
|
CONF_NET_METERING,
|
||||||
CONF_NUMBER,
|
CONF_NUMBER,
|
||||||
|
CONF_PULSE_COUNTERS,
|
||||||
|
CONF_SERIAL_NUMBER,
|
||||||
|
CONF_TEMPERATURE_SENSORS,
|
||||||
CONF_TIME_UNIT,
|
CONF_TIME_UNIT,
|
||||||
|
CONF_VOLTAGE_SENSORS,
|
||||||
DATA_GREENEYE_MONITOR,
|
DATA_GREENEYE_MONITOR,
|
||||||
SENSOR_TYPE_CURRENT,
|
|
||||||
SENSOR_TYPE_PULSE_COUNTER,
|
|
||||||
SENSOR_TYPE_TEMPERATURE,
|
|
||||||
SENSOR_TYPE_VOLTAGE,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
DATA_PULSES = "pulses"
|
DATA_PULSES = "pulses"
|
||||||
|
@ -51,21 +51,25 @@ async def async_setup_platform(
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a single GEM temperature sensor."""
|
"""Set up a single GEM temperature sensor."""
|
||||||
entities: list[GEMSensor] = []
|
entities: list[GEMSensor] = []
|
||||||
for sensor in discovery_info[CONF_SENSORS]:
|
for monitor_config in discovery_info[CONF_MONITORS]:
|
||||||
sensor_type = sensor[CONF_SENSOR_TYPE]
|
monitor_serial_number = monitor_config[CONF_SERIAL_NUMBER]
|
||||||
if sensor_type == SENSOR_TYPE_CURRENT:
|
|
||||||
|
channel_configs = monitor_config[CONF_CHANNELS]
|
||||||
|
for sensor in channel_configs:
|
||||||
entities.append(
|
entities.append(
|
||||||
CurrentSensor(
|
CurrentSensor(
|
||||||
sensor[CONF_MONITOR_SERIAL_NUMBER],
|
monitor_serial_number,
|
||||||
sensor[CONF_NUMBER],
|
sensor[CONF_NUMBER],
|
||||||
sensor[CONF_NAME],
|
sensor[CONF_NAME],
|
||||||
sensor[CONF_NET_METERING],
|
sensor[CONF_NET_METERING],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif sensor_type == SENSOR_TYPE_PULSE_COUNTER:
|
|
||||||
|
pulse_counter_configs = monitor_config[CONF_PULSE_COUNTERS]
|
||||||
|
for sensor in pulse_counter_configs:
|
||||||
entities.append(
|
entities.append(
|
||||||
PulseCounter(
|
PulseCounter(
|
||||||
sensor[CONF_MONITOR_SERIAL_NUMBER],
|
monitor_serial_number,
|
||||||
sensor[CONF_NUMBER],
|
sensor[CONF_NUMBER],
|
||||||
sensor[CONF_NAME],
|
sensor[CONF_NAME],
|
||||||
sensor[CONF_COUNTED_QUANTITY],
|
sensor[CONF_COUNTED_QUANTITY],
|
||||||
|
@ -73,19 +77,23 @@ async def async_setup_platform(
|
||||||
sensor[CONF_COUNTED_QUANTITY_PER_PULSE],
|
sensor[CONF_COUNTED_QUANTITY_PER_PULSE],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif sensor_type == SENSOR_TYPE_TEMPERATURE:
|
|
||||||
|
temperature_sensor_configs = monitor_config[CONF_TEMPERATURE_SENSORS]
|
||||||
|
for sensor in temperature_sensor_configs[CONF_SENSORS]:
|
||||||
entities.append(
|
entities.append(
|
||||||
TemperatureSensor(
|
TemperatureSensor(
|
||||||
sensor[CONF_MONITOR_SERIAL_NUMBER],
|
monitor_serial_number,
|
||||||
sensor[CONF_NUMBER],
|
sensor[CONF_NUMBER],
|
||||||
sensor[CONF_NAME],
|
sensor[CONF_NAME],
|
||||||
sensor[CONF_TEMPERATURE_UNIT],
|
temperature_sensor_configs[CONF_TEMPERATURE_UNIT],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif sensor_type == SENSOR_TYPE_VOLTAGE:
|
|
||||||
|
voltage_sensor_configs = monitor_config[CONF_VOLTAGE_SENSORS]
|
||||||
|
for sensor in voltage_sensor_configs:
|
||||||
entities.append(
|
entities.append(
|
||||||
VoltageSensor(
|
VoltageSensor(
|
||||||
sensor[CONF_MONITOR_SERIAL_NUMBER],
|
monitor_serial_number,
|
||||||
sensor[CONF_NUMBER],
|
sensor[CONF_NUMBER],
|
||||||
sensor[CONF_NAME],
|
sensor[CONF_NAME],
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue