Skip to content

Commit

Permalink
Merge pull request #3 from hostcc/bugfix/too-many-open-files
Browse files Browse the repository at this point in the history
Fix `Too many open files` error
  • Loading branch information
hostcc authored May 30, 2022
2 parents eb1b2c9 + 555b8b1 commit a98199d
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/energomera_hass_mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from iec62056_21.messages import CommandMessage
from iec62056_21.client import Iec6205621Client
from iec62056_21 import utils
from asyncio_mqtt import Client as mqtt_client
import asyncio_mqtt
from .config import EnergomeraConfig, EnergomeraConfigError # noqa:F401

_LOGGER = logging.getLogger(__name__)
Expand All @@ -43,12 +43,11 @@ class IecToHassSensor: # pylint: disable=too-many-instance-attributes
sends them over to HomeAssistant using MQTT.
:param dict mqtt_config: ``mqtt`` fragment of the configuration
:param asyncio_mqtt.Client mqtt_client: Instance of MQTT client
:param dict config_param: particular entry from ``parameters``
configuration section
:param iec62056_21.messages.AnswerDataMessage iec_item: Entry received from
the meter for the specified ``config_param``
:param ssl.SSLContext mqtt_ssl_context: TLS context needed with interacting
with SSL/TLS enabled MQTT broker
"""

# Class attribute to store HASS sensors having config payload sent, to
Expand All @@ -57,14 +56,11 @@ class IecToHassSensor: # pylint: disable=too-many-instance-attributes
# sensor has already been discovered
_hass_config_entities_published = {}

def __init__(self, mqtt_config, config_param, iec_item, mqtt_tls_context):
def __init__(self, mqtt_config, mqtt_client, config_param, iec_item):
self._config_param = config_param
self._iec_item = iec_item
self._mqtt_config = mqtt_config
self._mqtt_client = mqtt_client(
hostname=mqtt_config.host, username=mqtt_config.user,
password=mqtt_config.password, tls_context=mqtt_tls_context
)
self._mqtt_client = mqtt_client
self._hass_item_name = None
self._hass_device_id = None
self._hass_unique_id = None
Expand Down Expand Up @@ -240,6 +236,7 @@ async def process(self):
continue


# pylint: disable=too-many-instance-attributes
class EnergomeraHassMqtt:
"""
Communicates with Energomera energy meters using IEC 62056-21 (supersedes
Expand Down Expand Up @@ -285,6 +282,10 @@ def __init__(
self._client = Iec6205621Client.with_serial_transport(
port=config.of.meter.port, password=config.of.meter.password
)
self._mqtt_client = asyncio_mqtt.Client(
hostname=config.of.mqtt.host, username=config.of.mqtt.user,
password=config.of.mqtt.password, tls_context=mqtt_tls_context
)
self._hass_discovery_prefix = config.of.mqtt.hass_discovery_prefix
self._model = None
self._serial_number = None
Expand Down Expand Up @@ -343,7 +344,9 @@ async def iec_read_admin(self):
)

hass_item = IecToHassSensor(
self._config.of.mqtt, param, iec_item, self._mqtt_tls_context
mqtt_config=self._config.of.mqtt,
mqtt_client=self._mqtt_client, config_param=param,
iec_item=iec_item
)
hass_item.set_meter_ids(model, sw_version, serial_number)
await hass_item.process()
Expand Down

0 comments on commit a98199d

Please sign in to comment.