Skip to content

Commit

Permalink
Merge pull request #11 from golles/10-tab-missing-in-sensorpy
Browse files Browse the repository at this point in the history
Improve formatting
  • Loading branch information
golles authored Dec 6, 2022
2 parents 128cec8 + 82e3616 commit 487ac4c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 56 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: CI

on:
push:
branches:
- main
pull_request: ~

jobs:
validate:
runs-on: "ubuntu-latest"
Expand Down Expand Up @@ -32,7 +34,7 @@ jobs:

- run: python3 -m pip install black

- run: black .
- run: black . --check

# tests:
# runs-on: "ubuntu-latest"
Expand Down
118 changes: 63 additions & 55 deletions custom_components/mc66c/sensor.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
"""Constants for component."""
import logging
import voluptuous as vol
import serial
from time import sleep

from homeassistant.components.sensor import PLATFORM_SCHEMA
import homeassistant.helpers.config_validation as cv
from homeassistant.const import (
CONF_PORT,
CONF_NAME,
CONF_SCAN_INTERVAL,
CONF_RESOURCES
)
from homeassistant.util import Throttle
import serial
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, CONF_PORT, CONF_RESOURCES, CONF_SCAN_INTERVAL
from homeassistant.helpers.entity import Entity
from .const import (
DOMAIN,
DEFAULT_NAME,
DEFAULT_SCAN_INTERVAL,
SENSOR_TYPES,
)
from homeassistant.util import Throttle

from .const import DEFAULT_NAME, DEFAULT_SCAN_INTERVAL, DOMAIN, SENSOR_TYPES

_LOGGER = logging.getLogger(__name__)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Required(CONF_PORT): cv.string,
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL): cv.time_period,
vol.Required(CONF_RESOURCES, default=[]):
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Required(CONF_PORT): cv.string,
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL): cv.time_period,
vol.Required(CONF_RESOURCES, default=[]): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)]
),
}
)


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Setup the sensors."""
Expand All @@ -37,7 +33,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):

try:
reader = MC66CReader(port)
except RunTimeError:
except Exception:
_LOGGER.error("Unable to connect to %s", port)
return False

Expand All @@ -47,8 +43,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
sensor_type = resource.lower()

if sensor_type not in SENSOR_TYPES:
SENSOR_TYPES[sensor_type] = [
sensor_type.title(), "", "mdi:eye"]
SENSOR_TYPES[sensor_type] = [sensor_type.title(), "", "mdi:eye"]

entities.append(MC66CSensor(name, reader, sensor_type))

Expand All @@ -67,11 +62,13 @@ def __init__(self, port):
def read(self):
"""Read data from the serial port."""
# See page 33-36 in 5511- 634 GB Rev C1.qxd.pdf - 4. Data communication.
mc66c = serial.Serial(port=self._port,
bytesize=serial.SEVENBITS,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_TWO,
timeout=2)
mc66c = serial.Serial(
port=self._port,
bytesize=serial.SEVENBITS,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_TWO,
timeout=2,
)
mc66c.baudrate = 300
mc66c.write("/#1".encode("utf-8"))
mc66c.flush()
Expand All @@ -81,9 +78,9 @@ def read(self):
reading = mc66c.read(87).split()
mc66c.close()

self.validateAndSetData(reading)
self.validate_and_set_data(reading)

def validateAndSetData(self, reading):
def validate_and_set_data(self, reading):
"""New data needs validation before we continue."""
data = []

Expand All @@ -94,25 +91,32 @@ def validateAndSetData(self, reading):
try:
for c in reading:
if (len(c)) != 7:
raise Exception('Received invalid datafield length')

data.append(int((reading[0]).decode("utf-8")) / 1000) # Energy.
data.append(int((reading[1]).decode("utf-8")) / 1000) # Volume.
data.append(int((reading[2]).decode("utf-8"))) # Op_hrs.
data.append(int((reading[3]).decode("utf-8")) / 100) # Temperature_in.
data.append(int((reading[4]).decode("utf-8")) / 100) # Temperature_out.
data.append(int((reading[5]).decode("utf-8")) / 100) # Temperature_diff.
data.append(int((reading[6]).decode("utf-8")) / 10) # Power.
data.append(int((reading[7]).decode("utf-8")) / 10) # Flow.
data.append(int((reading[8]).decode("utf-8")) / 10) # Peak_power.
data.append(int((reading[9]).decode("utf-8"))) # Info_code

# Only setting the data at the end, if parsing fails for one or more sensors, the data was corrupt and this reading should be skipped.
self.data = data
raise Exception("Received invalid data field length")

data.append(int((reading[0]).decode("utf-8")) / 1000) # Energy.
data.append(int((reading[1]).decode("utf-8")) / 1000) # Volume.
data.append(int((reading[2]).decode("utf-8"))) # Op_hrs.
data.append(int((reading[3]).decode("utf-8")) / 100) # Temperature_in.
data.append(int((reading[4]).decode("utf-8")) / 100) # Temperature_out.
data.append(
int((reading[5]).decode("utf-8")) / 100
) # Temperature_diff.
data.append(int((reading[6]).decode("utf-8")) / 10) # Power.
data.append(int((reading[7]).decode("utf-8")) / 10) # Flow.
data.append(int((reading[8]).decode("utf-8")) / 10) # Peak_power.
data.append(int((reading[9]).decode("utf-8"))) # Info_code

# Only setting the data at the end, if parsing fails for one or more sensors,
# the data was corrupt and this reading should be skipped.
self.data = data
except Exception as error:
_LOGGER.info("Error=%s parsing data=%s", error, reading)
else:
_LOGGER.info("Skipping, received %s fields, this should be 10. Data: %s", num_fields, reading)
_LOGGER.info(
"Skipping, received %s fields, this should be 10. Data: %s",
num_fields,
reading,
)


class MC66CSensor(Entity):
Expand Down Expand Up @@ -163,12 +167,16 @@ def update(self):

if self.reader.data is not None:
new_state = self.reader.data[self._data_position]

if (
self._state is not None and
self._data_position is 0 and
abs(new_state - self._state) / self._state > 1
self._state is not None
and self._data_position is 0
and abs(new_state - self._state) / self._state > 1
):
_LOGGER.info("Skipping energy update; new value: %s is much different than previous: %s", new_state, self._state)
_LOGGER.info(
"Skipping energy update; new value: %s is much different than previous: %s",
new_state,
self._state,
)
else:
self._state = new_state
self._state = new_state

0 comments on commit 487ac4c

Please sign in to comment.