Skip to content

Commit

Permalink
Add new electricity counter #27
Browse files Browse the repository at this point in the history
  • Loading branch information
h4de5 committed Oct 7, 2021
1 parent 6da5ac7 commit 132bffb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
36 changes: 31 additions & 5 deletions custom_components/vimar/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
except ImportError:
from homeassistant.const import VOLT as ELECTRIC_POTENTIAL_VOLT

try:
from homeassistant.components.sensor import STATE_CLASS_TOTAL_INCREASING
except ImportError:
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT as STATE_CLASS_TOTAL_INCREASING

from homeassistant.helpers.entity import Entity

from .const import DOMAIN
Expand Down Expand Up @@ -82,27 +87,30 @@ def unit_of_measurement(self):
"""Return the unit of measurement."""
class_and_unit = self.class_and_units()
# _LOGGER.warning("DEBUG units for %s %s %s", self._device["object_type"], self._measurement_name, class_and_unit[0]);

return class_and_unit[0]

@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
class_and_unit = self.class_and_units()
# _LOGGER.warning("DEBUG class for %s %s %s", self._device["object_type"], self._measurement_name, class_and_unit[1]);

return class_and_unit[1]

@property
def state_class(self) -> str:
"""Return the state class of this entity."""
class_and_unit = self.class_and_units()
if class_and_unit[1] == DEVICE_CLASS_ENERGY:
return STATE_CLASS_TOTAL_INCREASING

def class_and_units(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
if self._device["object_type"] in ["CH_Misuratore", "CH_Carichi_Custom", "CH_Carichi", "CH_Carichi_3F", "CH_KNX_GENERIC_POWER_KW"]:
if any(x in self._measurement_name for x in ["energia"]):
if any(x in self._measurement_name for x in ["energia", "potenza_attiva"]):
return [ENERGY_KILO_WATT_HOUR, DEVICE_CLASS_ENERGY]
elif any(x in self._measurement_name for x in ["fase"]):
return [ELECTRIC_POTENTIAL_VOLT, DEVICE_CLASS_CURRENT]
# elif any(x in self._measurement_name for x in ["fase"]):
# return [ELECTRIC_POTENTIAL_VOLT, DEVICE_CLASS_VOLTAGE]

elif any(x in self._measurement_name for x in ["_date", "_time", "_datetime"]):
return ["", DEVICE_CLASS_TIMESTAMP]
else:
Expand Down Expand Up @@ -134,6 +142,24 @@ def class_and_units(self):
# ‘wind_speed_alarm’: {‘status_id’: ‘3417’, ‘status_value’: ‘0’, ‘status_range’: ‘’},
# ‘brightness’: {‘status_id’: ‘3437’, ‘status_value’: ‘0.00’, ‘status_range’: ‘’},

# 'potenza_attiva','-1','0.01'

# 'contatore_assoluto': {'status_id': '102467', 'status_value': '104','status_range': 'min=0|max=4294967295'},
# 'contatore_parziale':{'status_id': '102469', 'status_value': '15', 'status_range': 'min=0|max=4294967295'},
# 'reset_to_value': {'status_id': '102472', 'status_value': '0', 'status_range': 'min=0|max=4294967295'},
# 'reset_history': {'status_id': '102474', 'status_value': '0', 'status_range': 'min=0|max=1'},
# 'frequenza_impulsi': {'status_id': '102476', 'status_value':'0', 'status_range':'min=-2147483648|max=2147483648'},
# 'divisore': {'status_id': '103644', 'status_value': '1', 'status_range': ''},
# 'moltiplicatore': {'status_id': '103646', 'status_value': '100', 'status_range': ''}}

# contatore assoluto = absolute counter. total pulses received
# contatore parziale = partial counter. pulses since the last reset
# reset_to_value = initial value to count from
# reset_history = reset history
# frequenza_impulsi = pulse frequency
# divisore = divisor: value by which to divide the partial counter
# moltiplicatore = multiplier: value by which to multiply the partial counter

@property
def unique_id(self):
"""Return the ID of this device and its state."""
Expand Down
25 changes: 17 additions & 8 deletions custom_components/vimar/vimarlink/vimarlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

import logging
import sys

# for communicating with vimar webserver
import xml.etree.cElementTree as xmlTree
from xml.etree import ElementTree

import requests
from requests.exceptions import HTTPError

from .const import (DEVICE_TYPE_CLIMATES, # DEVICE_TYPE_FANS,
DEVICE_TYPE_COVERS, DEVICE_TYPE_LIGHTS,
DEVICE_TYPE_MEDIA_PLAYERS, DEVICE_TYPE_OTHERS,
DEVICE_TYPE_SCENES, DEVICE_TYPE_SENSORS,
DEVICE_TYPE_SWITCHES)
from .const import (
DEVICE_TYPE_CLIMATES, # DEVICE_TYPE_FANS,
DEVICE_TYPE_COVERS,
DEVICE_TYPE_LIGHTS,
DEVICE_TYPE_MEDIA_PLAYERS,
DEVICE_TYPE_OTHERS,
DEVICE_TYPE_SCENES,
DEVICE_TYPE_SENSORS,
DEVICE_TYPE_SWITCHES,
)

_LOGGER = logging.getLogger(__name__)
MAX_ROWS_PER_REQUEST = 300
Expand Down Expand Up @@ -606,8 +612,6 @@ def _request(self, url, post=None, headers=None, check_ssl=False):
else:
return response.text

return None


class VimarProject:
"""Container that holds all vimar devices and its states."""
Expand Down Expand Up @@ -759,7 +763,8 @@ def parse_device_type(self, device):
icon = ["mdi:window-shutter", "mdi:window-shutter-open"]

elif device["object_type"] in ["CH_Clima", "CH_HVAC_NoZonaNeutra", "CH_HVAC_RiscaldamentoNoZonaNeutra", "CH_Fancoil", "CH_HVAC"]:
device_type = DEVICE_TYPE_CLIMATES
# device_type = DEVICE_TYPE_CLIMATES
device_type = DEVICE_TYPE_SENSORS
icon = "mdi:thermometer-lines"

_LOGGER.debug("Climate object returned from web server: " + device["object_type"] + " / " + device["object_name"])
Expand Down Expand Up @@ -787,6 +792,10 @@ def parse_device_type(self, device):
# _LOGGER.debug(
# "Sensor object has states: "
# + str(device["status"]))
elif any(x in device["object_type"].upper() for x in ["CH_Contatore_"]):
device_type = DEVICE_TYPE_SENSORS
# icon = "mdi:battery-charging-high"
icon = "mdi:pulse"

elif device["object_type"] in ["CH_KNX_GENERIC_TEMPERATURE_C"]:
# see: https://github.com/h4de5/home-assistant-vimar/issues/20
Expand Down

0 comments on commit 132bffb

Please sign in to comment.