Update ordering (#21377)

pull/20926/head
Fabian Affolter 2019-02-24 10:22:17 +01:00 committed by GitHub
parent 2ada0ecfd9
commit 3732d75633
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 119 additions and 152 deletions

View File

@ -4,27 +4,34 @@ import threading
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
CONF_HOST, CONF_METHOD, CONF_NAME, CONF_PORT, CONF_TYPE, CONF_TIMEOUT,
ATTR_STATE)
DOMAIN = 'modbus'
ATTR_STATE, CONF_HOST, CONF_METHOD, CONF_NAME, CONF_PORT, CONF_TIMEOUT,
CONF_TYPE, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['pymodbus==1.5.2']
CONF_HUB = 'hub'
# Type of network
_LOGGER = logging.getLogger(__name__)
ATTR_ADDRESS = 'address'
ATTR_HUB = 'hub'
ATTR_UNIT = 'unit'
ATTR_VALUE = 'value'
CONF_BAUDRATE = 'baudrate'
CONF_BYTESIZE = 'bytesize'
CONF_STOPBITS = 'stopbits'
CONF_HUB = 'hub'
CONF_PARITY = 'parity'
CONF_STOPBITS = 'stopbits'
DEFAULT_HUB = 'default'
DOMAIN = 'modbus'
SERVICE_WRITE_COIL = 'write_coil'
SERVICE_WRITE_REGISTER = 'write_register'
BASE_SCHEMA = vol.Schema({
vol.Optional(CONF_NAME, default=DEFAULT_HUB): cv.string
vol.Optional(CONF_NAME, default=DEFAULT_HUB): cv.string,
})
SERIAL_SCHEMA = BASE_SCHEMA.extend({
@ -49,16 +56,6 @@ CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.All(cv.ensure_list, [vol.Any(SERIAL_SCHEMA, ETHERNET_SCHEMA)])
}, extra=vol.ALLOW_EXTRA,)
_LOGGER = logging.getLogger(__name__)
SERVICE_WRITE_REGISTER = 'write_register'
SERVICE_WRITE_COIL = 'write_coil'
ATTR_ADDRESS = 'address'
ATTR_HUB = 'hub'
ATTR_UNIT = 'unit'
ATTR_VALUE = 'value'
SERVICE_WRITE_REGISTER_SCHEMA = vol.Schema({
vol.Optional(ATTR_HUB, default=DEFAULT_HUB): cv.string,
vol.Required(ATTR_UNIT): cv.positive_int,
@ -109,14 +106,13 @@ def setup_client(client_config):
def setup(hass, config):
"""Set up Modbus component."""
# Modbus connection type
hass.data[DOMAIN] = hub_collect = {}
for client_config in config[DOMAIN]:
client = setup_client(client_config)
name = client_config[CONF_NAME]
hub_collect[name] = ModbusHub(client, name)
_LOGGER.debug('Setting up hub: %s', client_config)
_LOGGER.debug("Setting up hub: %s", client_config)
def stop_modbus(event):
"""Stop Modbus service."""
@ -139,24 +135,20 @@ def setup(hass, config):
schema=SERVICE_WRITE_COIL_SCHEMA)
def write_register(service):
"""Write modbus registers."""
"""Write Modbus registers."""
unit = int(float(service.data.get(ATTR_UNIT)))
address = int(float(service.data.get(ATTR_ADDRESS)))
value = service.data.get(ATTR_VALUE)
client_name = service.data.get(ATTR_HUB)
if isinstance(value, list):
hub_collect[client_name].write_registers(
unit,
address,
[int(float(i)) for i in value])
unit, address, [int(float(i)) for i in value])
else:
hub_collect[client_name].write_register(
unit,
address,
int(float(value)))
unit, address, int(float(value)))
def write_coil(service):
"""Write modbus coil."""
"""Write Modbus coil."""
unit = service.data.get(ATTR_UNIT)
address = service.data.get(ATTR_ADDRESS)
state = service.data.get(ATTR_STATE)
@ -172,7 +164,7 @@ class ModbusHub:
"""Thread safe wrapper class for pymodbus."""
def __init__(self, modbus_client, name):
"""Initialize the modbus hub."""
"""Initialize the Modbus hub."""
self._client = modbus_client
self._lock = threading.Lock()
self._name = name
@ -196,52 +188,36 @@ class ModbusHub:
"""Read coils."""
with self._lock:
kwargs = {'unit': unit} if unit else {}
return self._client.read_coils(
address,
count,
**kwargs)
return self._client.read_coils(address, count, **kwargs)
def read_input_registers(self, unit, address, count):
"""Read input registers."""
with self._lock:
kwargs = {'unit': unit} if unit else {}
return self._client.read_input_registers(
address,
count,
**kwargs)
address, count, **kwargs)
def read_holding_registers(self, unit, address, count):
"""Read holding registers."""
with self._lock:
kwargs = {'unit': unit} if unit else {}
return self._client.read_holding_registers(
address,
count,
**kwargs)
address, count, **kwargs)
def write_coil(self, unit, address, value):
"""Write coil."""
with self._lock:
kwargs = {'unit': unit} if unit else {}
self._client.write_coil(
address,
value,
**kwargs)
self._client.write_coil(address, value, **kwargs)
def write_register(self, unit, address, value):
"""Write register."""
with self._lock:
kwargs = {'unit': unit} if unit else {}
self._client.write_register(
address,
value,
**kwargs)
self._client.write_register(address, value, **kwargs)
def write_registers(self, unit, address, values):
"""Write registers."""
with self._lock:
kwargs = {'unit': unit} if unit else {}
self._client.write_registers(
address,
values,
**kwargs)
self._client.write_registers(address, values, **kwargs)

View File

@ -1,25 +1,27 @@
"""Support for Modbus Coil sensors."""
import logging
import voluptuous as vol
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components.modbus import (
CONF_HUB, DEFAULT_HUB, DOMAIN as MODBUS_DOMAIN)
from homeassistant.const import CONF_NAME, CONF_SLAVE
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.helpers import config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, CONF_SLAVE
from homeassistant.helpers import config_validation as cv
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['modbus']
CONF_COIL = 'coil'
CONF_COILS = 'coils'
DEPENDENCIES = ['modbus']
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_COILS): [{
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
vol.Required(CONF_COIL): cv.positive_int,
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
vol.Optional(CONF_SLAVE): cv.positive_int,
}]
})
@ -33,6 +35,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
sensors.append(ModbusCoilSensor(
hub, coil.get(CONF_NAME), coil.get(CONF_SLAVE),
coil.get(CONF_COIL)))
add_entities(sensors)
@ -40,7 +43,7 @@ class ModbusCoilSensor(BinarySensorDevice):
"""Modbus coil sensor."""
def __init__(self, hub, name, slave, coil):
"""Initialize the modbus coil sensor."""
"""Initialize the Modbus coil sensor."""
self._hub = hub
self._name = name
self._slave = int(slave) if slave else None

View File

@ -4,20 +4,15 @@ import struct
import voluptuous as vol
from homeassistant.const import (
CONF_NAME, CONF_SLAVE, ATTR_TEMPERATURE)
from homeassistant.components.climate import ClimateDevice, PLATFORM_SCHEMA
from homeassistant.components.climate.const import (
SUPPORT_TARGET_TEMPERATURE)
from homeassistant.components.climate import PLATFORM_SCHEMA, ClimateDevice
from homeassistant.components.climate.const import SUPPORT_TARGET_TEMPERATURE
from homeassistant.components.modbus import (
CONF_HUB, DEFAULT_HUB, DOMAIN as MODBUS_DOMAIN)
from homeassistant.const import ATTR_TEMPERATURE, CONF_NAME, CONF_SLAVE
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['modbus']
# Parameters not defined by homeassistant.const
CONF_TARGET_TEMP = 'target_temp_register'
CONF_CURRENT_TEMP = 'current_temp_register'
CONF_DATA_TYPE = 'data_type'
@ -27,21 +22,22 @@ CONF_PRECISION = 'precision'
DATA_TYPE_INT = 'int'
DATA_TYPE_UINT = 'uint'
DATA_TYPE_FLOAT = 'float'
DEPENDENCIES = ['modbus']
SUPPORT_FLAGS = SUPPORT_TARGET_TEMPERATURE
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
vol.Required(CONF_CURRENT_TEMP): cv.positive_int,
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_SLAVE): cv.positive_int,
vol.Required(CONF_TARGET_TEMP): cv.positive_int,
vol.Required(CONF_CURRENT_TEMP): cv.positive_int,
vol.Optional(CONF_COUNT, default=2): cv.positive_int,
vol.Optional(CONF_DATA_TYPE, default=DATA_TYPE_FLOAT):
vol.In([DATA_TYPE_INT, DATA_TYPE_UINT, DATA_TYPE_FLOAT]),
vol.Optional(CONF_COUNT, default=2): cv.positive_int,
vol.Optional(CONF_PRECISION, default=1): cv.positive_int
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
vol.Optional(CONF_PRECISION, default=1): cv.positive_int,
})
SUPPORT_FLAGS = SUPPORT_TARGET_TEMPERATURE
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Modbus Thermostat Platform."""
@ -78,12 +74,14 @@ class ModbusThermostat(ClimateDevice):
self._precision = precision
self._structure = '>f'
data_types = {DATA_TYPE_INT: {1: 'h', 2: 'i', 4: 'q'},
DATA_TYPE_UINT: {1: 'H', 2: 'I', 4: 'Q'},
DATA_TYPE_FLOAT: {1: 'e', 2: 'f', 4: 'd'}}
data_types = {
DATA_TYPE_INT: {1: 'h', 2: 'i', 4: 'q'},
DATA_TYPE_UINT: {1: 'H', 2: 'I', 4: 'Q'},
DATA_TYPE_FLOAT: {1: 'e', 2: 'f', 4: 'd'},
}
self._structure = '>{}'.format(data_types[self._data_type]
[self._count])
self._structure = '>{}'.format(
data_types[self._data_type][self._count])
@property
def supported_features(self):
@ -109,7 +107,7 @@ class ModbusThermostat(ClimateDevice):
@property
def target_temperature(self):
"""Return the temperature we try to reach."""
"""Return the target temperature."""
return self._target_temperature
def set_temperature(self, **kwargs):
@ -121,16 +119,16 @@ class ModbusThermostat(ClimateDevice):
register_value = struct.unpack('>h', byte_string[0:2])[0]
try:
self.write_register(self._target_temperature_register,
register_value)
self.write_register(
self._target_temperature_register, register_value)
except AttributeError as ex:
_LOGGER.error(ex)
def read_register(self, register):
"""Read holding register using the modbus hub slave."""
"""Read holding register using the Modbus hub slave."""
try:
result = self._hub.read_holding_registers(self._slave, register,
self._count)
result = self._hub.read_holding_registers(
self._slave, register, self._count)
except AttributeError as ex:
_LOGGER.error(ex)
byte_string = b''.join(
@ -140,5 +138,5 @@ class ModbusThermostat(ClimateDevice):
return register_value
def write_register(self, register, value):
"""Write register using the modbus hub slave."""
"""Write register using the Modbus hub slave."""
self._hub.write_registers(self._slave, register, [value, 0])

View File

@ -6,50 +6,50 @@ import voluptuous as vol
from homeassistant.components.modbus import (
CONF_HUB, DEFAULT_HUB, DOMAIN as MODBUS_DOMAIN)
from homeassistant.const import (
CONF_NAME, CONF_OFFSET, CONF_UNIT_OF_MEASUREMENT, CONF_SLAVE,
CONF_STRUCTURE)
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers import config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_NAME, CONF_OFFSET, CONF_SLAVE, CONF_STRUCTURE,
CONF_UNIT_OF_MEASUREMENT)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['modbus']
CONF_COUNT = 'count'
CONF_REVERSE_ORDER = 'reverse_order'
CONF_DATA_TYPE = 'data_type'
CONF_PRECISION = 'precision'
CONF_REGISTER = 'register'
CONF_REGISTERS = 'registers'
CONF_SCALE = 'scale'
CONF_DATA_TYPE = 'data_type'
CONF_REGISTER_TYPE = 'register_type'
CONF_REGISTERS = 'registers'
CONF_REVERSE_ORDER = 'reverse_order'
CONF_SCALE = 'scale'
DATA_TYPE_CUSTOM = 'custom'
DATA_TYPE_FLOAT = 'float'
DATA_TYPE_INT = 'int'
DATA_TYPE_UINT = 'uint'
DEPENDENCIES = ['modbus']
REGISTER_TYPE_HOLDING = 'holding'
REGISTER_TYPE_INPUT = 'input'
DATA_TYPE_INT = 'int'
DATA_TYPE_UINT = 'uint'
DATA_TYPE_FLOAT = 'float'
DATA_TYPE_CUSTOM = 'custom'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_REGISTERS): [{
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_REGISTER): cv.positive_int,
vol.Optional(CONF_REGISTER_TYPE, default=REGISTER_TYPE_HOLDING):
vol.In([REGISTER_TYPE_HOLDING, REGISTER_TYPE_INPUT]),
vol.Optional(CONF_COUNT, default=1): cv.positive_int,
vol.Optional(CONF_REVERSE_ORDER, default=False): cv.boolean,
vol.Optional(CONF_OFFSET, default=0): vol.Coerce(float),
vol.Optional(CONF_PRECISION, default=0): cv.positive_int,
vol.Optional(CONF_SCALE, default=1): vol.Coerce(float),
vol.Optional(CONF_SLAVE): cv.positive_int,
vol.Optional(CONF_DATA_TYPE, default=DATA_TYPE_INT):
vol.In([DATA_TYPE_INT, DATA_TYPE_UINT, DATA_TYPE_FLOAT,
DATA_TYPE_CUSTOM]),
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
vol.Optional(CONF_OFFSET, default=0): vol.Coerce(float),
vol.Optional(CONF_PRECISION, default=0): cv.positive_int,
vol.Optional(CONF_REGISTER_TYPE, default=REGISTER_TYPE_HOLDING):
vol.In([REGISTER_TYPE_HOLDING, REGISTER_TYPE_INPUT]),
vol.Optional(CONF_REVERSE_ORDER, default=False): cv.boolean,
vol.Optional(CONF_SCALE, default=1): vol.Coerce(float),
vol.Optional(CONF_SLAVE): cv.positive_int,
vol.Optional(CONF_STRUCTURE): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
}]
@ -93,17 +93,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
hub_name = register.get(CONF_HUB)
hub = hass.data[MODBUS_DOMAIN][hub_name]
sensors.append(ModbusRegisterSensor(
hub,
register.get(CONF_NAME),
register.get(CONF_SLAVE),
register.get(CONF_REGISTER),
register.get(CONF_REGISTER_TYPE),
register.get(CONF_UNIT_OF_MEASUREMENT),
register.get(CONF_COUNT),
register.get(CONF_REVERSE_ORDER),
register.get(CONF_SCALE),
register.get(CONF_OFFSET),
structure,
hub, register.get(CONF_NAME), register.get(CONF_SLAVE),
register.get(CONF_REGISTER), register.get(CONF_REGISTER_TYPE),
register.get(CONF_UNIT_OF_MEASUREMENT), register.get(CONF_COUNT),
register.get(CONF_REVERSE_ORDER), register.get(CONF_SCALE),
register.get(CONF_OFFSET), structure,
register.get(CONF_PRECISION)))
if not sensors:
@ -158,14 +152,10 @@ class ModbusRegisterSensor(RestoreEntity):
"""Update the state of the sensor."""
if self._register_type == REGISTER_TYPE_INPUT:
result = self._hub.read_input_registers(
self._slave,
self._register,
self._count)
self._slave, self._register, self._count)
else:
result = self._hub.read_holding_registers(
self._slave,
self._register,
self._count)
self._slave, self._register, self._count)
val = 0
try:

View File

@ -1,54 +1,54 @@
"""Support for Modbus switches."""
import logging
import voluptuous as vol
from homeassistant.components.modbus import (
CONF_HUB, DEFAULT_HUB, DOMAIN as MODBUS_DOMAIN)
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_NAME, CONF_SLAVE, CONF_COMMAND_ON, CONF_COMMAND_OFF, STATE_ON)
CONF_COMMAND_OFF, CONF_COMMAND_ON, CONF_NAME, CONF_SLAVE, STATE_ON)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers import config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['modbus']
CONF_COIL = 'coil'
CONF_COILS = 'coils'
CONF_REGISTER = 'register'
CONF_REGISTER_TYPE = 'register_type'
CONF_REGISTERS = 'registers'
CONF_STATE_OFF = 'state_off'
CONF_STATE_ON = 'state_on'
CONF_VERIFY_REGISTER = 'verify_register'
CONF_VERIFY_STATE = 'verify_state'
CONF_COIL = "coil"
CONF_COILS = "coils"
CONF_REGISTER = "register"
CONF_REGISTERS = "registers"
CONF_VERIFY_STATE = "verify_state"
CONF_VERIFY_REGISTER = "verify_register"
CONF_REGISTER_TYPE = "register_type"
CONF_STATE_ON = "state_on"
CONF_STATE_OFF = "state_off"
DEPENDENCIES = ['modbus']
REGISTER_TYPE_HOLDING = 'holding'
REGISTER_TYPE_INPUT = 'input'
REGISTERS_SCHEMA = vol.Schema({
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_SLAVE): cv.positive_int,
vol.Required(CONF_REGISTER): cv.positive_int,
vol.Required(CONF_COMMAND_ON): cv.positive_int,
vol.Required(CONF_COMMAND_OFF): cv.positive_int,
vol.Optional(CONF_VERIFY_STATE, default=True): cv.boolean,
vol.Optional(CONF_VERIFY_REGISTER):
cv.positive_int,
vol.Required(CONF_COMMAND_ON): cv.positive_int,
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_REGISTER): cv.positive_int,
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
vol.Optional(CONF_REGISTER_TYPE, default=REGISTER_TYPE_HOLDING):
vol.In([REGISTER_TYPE_HOLDING, REGISTER_TYPE_INPUT]),
vol.Optional(CONF_STATE_ON): cv.positive_int,
vol.Optional(CONF_SLAVE): cv.positive_int,
vol.Optional(CONF_STATE_OFF): cv.positive_int,
vol.Optional(CONF_STATE_ON): cv.positive_int,
vol.Optional(CONF_VERIFY_REGISTER): cv.positive_int,
vol.Optional(CONF_VERIFY_STATE, default=True): cv.boolean,
})
COILS_SCHEMA = vol.Schema({
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
vol.Required(CONF_COIL): cv.positive_int,
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_SLAVE): cv.positive_int,
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
})
PLATFORM_SCHEMA = vol.All(
@ -141,9 +141,9 @@ class ModbusRegisterSwitch(ModbusCoilSwitch):
"""Representation of a Modbus register switch."""
# pylint: disable=super-init-not-called
def __init__(self, hub, name, slave, register, command_on,
command_off, verify_state, verify_register,
register_type, state_on, state_off):
def __init__(self, hub, name, slave, register, command_on, command_off,
verify_state, verify_register, register_type, state_on,
state_off):
"""Initialize the register switch."""
self._hub = hub
self._name = name