hass-core/homeassistant/components/omnilogic/sensor.py
Oliver Acevedo 0c12af347e
Add Omnilogic integration (#40474)
* Scaffold

* Added the en translation

* Modified the name

* Basic functionality for config flow.

* Pulled in enough to validate config flow works.

* Update manifest.json

* initial data polling (water and air temp sensors)

* Adding sensors, debugging update function

* polling updates working

* support for new data format from library

* Updated entity_id, friendly name, conversion for ppm, attributes for hayward display units, MSPSystemID and component systemID

* Fixed errors for PR

* clean up

* Add login exc, check if configured, test login.

* Remove debug print.

* Black formatting, ran isort, update requirements.

* Updated w isort. fix flake8 failures.

* Fix flake8 errors

* Fixed self.attrs to remove invalid self._ values - small change

* Missed on small change - fixing attributes

* Updated naming, updated unit of measure, updated icon, bumped omnilog…

* Updated to fix flake8 issues in __init__.py and config_flow.py

* Updated test_config_flow.py to pass, updated config_flow.py to correct errors in test

* Remove comments in preparation for PR

* update .covezragerc

* Formatting fix

* Rewrote sensors to dynamically add all BOWs, pumps, clorinators. Still to do - add CSAD sensors.

* Rewrote sensors to dynamically add all BOWs, pumps, clorinators. Still to do - add CSAD sensors.

* Added CSAD sensors for pools that have them.

* Added CSAD sensors for pools that have them.

* Fixed CSAD to not create if blank or don't exist, removed broad except usage to pass linting.

* Updated entity naming convention. Fixed linting issues.

* Added device association to the back yard / omnilogic system

* Removed .0 from ppm values when returning imperial values for salt sensor

* Updated to return state = None for water temp when pump is off, handled Chlorinator operatingMode = 2, and added PlatformNotReady check

* Corrected exception from Omnilogic library

* Bumped omnilogic to 0.3.7. Added alarm sensor/data to sensors. Handle pump off condition for ph and orp sensors.

* Bumped omnilogic to 0.3.7. Added alarm sensor/data to sensors. Handle pump off condition for ph and orp sensors.

* Bumped omnilogic to 0.3.7. Added alarm sensor/data to sensors. Handle pump off condition for ph and orp sensors.

* Removed nested_lookup dependency, bumped omnilogic.py to 0.3.8.

* Fixed lint error

* Added logging for sensor creation.

* Fixed linting errors with logging.

* Fixed explicit chaining of raised error. Fixed issue with alarm sensor.

* Fixed manifest.json based on feedback.

* Fixed self.attrs, should_poll, CoordinatorEntity, SCAN_INTERVAL from comments in PR.

* Addressed unique_id, moved data update coordinator, addressed minor other issues from testing

* Created main OmniLogic entity for common items, reworked DataUpdateCoordinator to it's own class.

* Addressed config_schema not used in __init__.py

* Fixed linting issues.

* Addressed several comments, still todo - separate sensor classes.

* Split the Omnilogic Sensors into separate logical classes for simpler logic.

* Fixed snake case lint error for AddAlarms (to add_alarms)

* Addressed config_flow issues from comments.

* Changed addressed ConfigNotReady issue from comments.

* Updated strings.json and generated corrected en.json with translations.

* Updated en.json to standard generated file.

* Added config_flow tests and updated issue with config_flow on cannot_connect

* Added test case for incomplete information entered.

* Compressed logic in the sensor classes to reduce duplication.

* Updated strings.json for polling_interval, added generic exception handling on config flow.

* Removed omnilogic from the .coveragerc omit file.

* Updated test_config_flow to follow recommended pattern.

* Excluded sensor.py from test coverage tests.

* Corected minor issues in test_config_flow from comments

* Fixed linting issues on last commits

* Fixed linting issues.

* Corrected issue when temp state is not available from Omnilogic

* Added omnililogic_common.py from .coveragerc to bypass test coverage check.

* Return false on Login Exception, handle OmniLogicException in config_flow and in tests.

* Handle all exceptions and in config_flow and tests, clarified test naming.

* Broke out test cases per comments.

* Regenerated en.json file.

* Addressed changes from comments in PR.

* Added session and bumped API to 0.4.0, addressed other comments from PR.

* Addressed entitydata (missed earlier).

* Fixed pylint issue

* Added test case for options flow in test_config_flow.py

* Removed super() and used self when calling methods in current class.

* Addressed comments in PR.

* Addressed comments in PR.

* Updated translations file.

* Rewrote data coordinator to output dict for easy searching.

* Updated chlorinator unit when chlorinator is on/off only

* Scaffold

* Added the en translation

* Modified the name

* Basic functionality for config flow.

* Pulled in enough to validate config flow works.

* Update manifest.json

* initial data polling (water and air temp sensors)

* Adding sensors, debugging update function

* polling updates working

* support for new data format from library

* Updated entity_id, friendly name, conversion for ppm, attributes for hayward display units, MSPSystemID and component systemID

* Fixed errors for PR

* clean up

* Add login exc, check if configured, test login.

* Remove debug print.

* Black formatting, ran isort, update requirements.

* Updated w isort. fix flake8 failures.

* Fix flake8 errors

* Fixed self.attrs to remove invalid self._ values - small change

* Missed on small change - fixing attributes

* Updated naming, updated unit of measure, updated icon, bumped omnilog…

* Updated to fix flake8 issues in __init__.py and config_flow.py

* Updated test_config_flow.py to pass, updated config_flow.py to correct errors in test

* Remove comments in preparation for PR

* update .covezragerc

* Formatting fix

* Rewrote sensors to dynamically add all BOWs, pumps, clorinators. Still to do - add CSAD sensors.

* Rewrote sensors to dynamically add all BOWs, pumps, clorinators. Still to do - add CSAD sensors.

* Added CSAD sensors for pools that have them.

* Added CSAD sensors for pools that have them.

* Fixed CSAD to not create if blank or don't exist, removed broad except usage to pass linting.

* Updated entity naming convention. Fixed linting issues.

* Added device association to the back yard / omnilogic system

* Removed .0 from ppm values when returning imperial values for salt sensor

* Updated to return state = None for water temp when pump is off, handled Chlorinator operatingMode = 2, and added PlatformNotReady check

* Corrected exception from Omnilogic library

* Bumped omnilogic to 0.3.7. Added alarm sensor/data to sensors. Handle pump off condition for ph and orp sensors.

* Bumped omnilogic to 0.3.7. Added alarm sensor/data to sensors. Handle pump off condition for ph and orp sensors.

* Bumped omnilogic to 0.3.7. Added alarm sensor/data to sensors. Handle pump off condition for ph and orp sensors.

* Removed nested_lookup dependency, bumped omnilogic.py to 0.3.8.

* Fixed lint error

* Added logging for sensor creation.

* Fixed linting errors with logging.

* Fixed explicit chaining of raised error. Fixed issue with alarm sensor.

* Fixed manifest.json based on feedback.

* Fixed self.attrs, should_poll, CoordinatorEntity, SCAN_INTERVAL from comments in PR.

* Addressed unique_id, moved data update coordinator, addressed minor other issues from testing

* Created main OmniLogic entity for common items, reworked DataUpdateCoordinator to it's own class.

* Addressed config_schema not used in __init__.py

* Fixed linting issues.

* Addressed several comments, still todo - separate sensor classes.

* Split the Omnilogic Sensors into separate logical classes for simpler logic.

* Fixed snake case lint error for AddAlarms (to add_alarms)

* Addressed config_flow issues from comments.

* Changed addressed ConfigNotReady issue from comments.

* Updated strings.json and generated corrected en.json with translations.

* Updated en.json to standard generated file.

* Added config_flow tests and updated issue with config_flow on cannot_connect

* Added test case for incomplete information entered.

* Compressed logic in the sensor classes to reduce duplication.

* Updated strings.json for polling_interval, added generic exception handling on config flow.

* Removed omnilogic from the .coveragerc omit file.

* Updated test_config_flow to follow recommended pattern.

* Excluded sensor.py from test coverage tests.

* Corected minor issues in test_config_flow from comments

* Fixed linting issues on last commits

* Fixed linting issues.

* Corrected issue when temp state is not available from Omnilogic

* Added omnililogic_common.py from .coveragerc to bypass test coverage check.

* Return false on Login Exception, handle OmniLogicException in config_flow and in tests.

* Handle all exceptions and in config_flow and tests, clarified test naming.

* Broke out test cases per comments.

* Regenerated en.json file.

* Addressed changes from comments in PR.

* Added session and bumped API to 0.4.0, addressed other comments from PR.

* Addressed entitydata (missed earlier).

* Fixed pylint issue

* Added test case for options flow in test_config_flow.py

* Removed super() and used self when calling methods in current class.

* Addressed comments in PR.

* Addressed comments in PR.

* Updated translations file.

* Rewrote data coordinator to output dict for easy searching.

* Updated chlorinator unit when chlorinator is on/off only

* Fixed ORP method not being @property, fixed unique_id potential issue. Does not address comments from PR.

* Rewrote coordinator for updated dict structure, rewrote sensors to parse new data structure.

* Added alarms as attributes on all entities which support alarm reporting.

* Updated SENSOR_TYPES to sensor_types to adhere to snake case in pylint.

* Addressed PR comments.

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Removed binary sensor conditions (alarms, on/off sensor types) and added ability for multiple guard conditions

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Updated per comments in PR for Pump Type and removal of force_update().

* Update homeassistant/components/omnilogic/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/omnilogic/common.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Correctly asserting conditions for the login exception case.

* Update .coveragerc

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

Co-authored-by: Mike Hershberger <mike.hershberger@gmail.com>
Co-authored-by: Chad <54695185+chadlyy@users.noreply.github.com>
Co-authored-by: Tim Empringham <tim.empringham@live.ca>
Co-authored-by: djtimca <60706061+djtimca@users.noreply.github.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2020-09-25 17:55:10 +02:00

356 lines
9.8 KiB
Python

"""Definition and setup of the Omnilogic Sensors for Home Assistant."""
import logging
from homeassistant.components.sensor import DEVICE_CLASS_TEMPERATURE
from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION,
MASS_GRAMS,
PERCENTAGE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
VOLUME_LITERS,
)
from .common import OmniLogicEntity, OmniLogicUpdateCoordinator
from .const import COORDINATOR, DOMAIN, PUMP_TYPES
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id][COORDINATOR]
entities = []
for item_id, item in coordinator.data.items():
id_len = len(item_id)
item_kind = item_id[-2]
entity_settings = SENSOR_TYPES.get((id_len, item_kind))
if not entity_settings:
continue
for entity_setting in entity_settings:
for state_key, entity_class in entity_setting["entity_classes"].items():
if state_key not in item:
continue
guard = False
for guard_condition in entity_setting["guard_condition"]:
if guard_condition and all(
item.get(guard_key) == guard_value
for guard_key, guard_value in guard_condition.items()
):
guard = True
if guard:
continue
entity = entity_class(
coordinator=coordinator,
state_key=state_key,
name=entity_setting["name"],
kind=entity_setting["kind"],
item_id=item_id,
device_class=entity_setting["device_class"],
icon=entity_setting["icon"],
unit=entity_setting["unit"],
)
entities.append(entity)
async_add_entities(entities)
class OmnilogicSensor(OmniLogicEntity):
"""Defines an Omnilogic sensor entity."""
def __init__(
self,
coordinator: OmniLogicUpdateCoordinator,
kind: str,
name: str,
device_class: str,
icon: str,
unit: str,
item_id: tuple,
state_key: str,
):
"""Initialize Entities."""
super().__init__(
coordinator=coordinator,
kind=kind,
name=name,
item_id=item_id,
icon=icon,
)
backyard_id = item_id[:2]
unit_type = coordinator.data[backyard_id].get("Unit-of-Measurement")
self._unit_type = unit_type
self._device_class = device_class
self._unit = unit
self._state_key = state_key
@property
def device_class(self):
"""Return the device class of the entity."""
return self._device_class
@property
def unit_of_measurement(self):
"""Return the right unit of measure."""
return self._unit
class OmniLogicTemperatureSensor(OmnilogicSensor):
"""Define an OmniLogic Temperature (Air/Water) Sensor."""
@property
def state(self):
"""Return the state for the temperature sensor."""
sensor_data = self.coordinator.data[self._item_id][self._state_key]
hayward_state = sensor_data
hayward_unit_of_measure = TEMP_FAHRENHEIT
state = sensor_data
if self._unit_type == "Metric":
hayward_state = round((hayward_state - 32) * 5 / 9, 1)
hayward_unit_of_measure = TEMP_CELSIUS
if int(sensor_data) == -1:
hayward_state = None
state = None
self._attrs["hayward_temperature"] = hayward_state
self._attrs["hayward_unit_of_measure"] = hayward_unit_of_measure
self._unit = TEMP_FAHRENHEIT
return state
class OmniLogicPumpSpeedSensor(OmnilogicSensor):
"""Define an OmniLogic Pump Speed Sensor."""
@property
def state(self):
"""Return the state for the pump speed sensor."""
pump_type = PUMP_TYPES[self.coordinator.data[self._item_id]["Filter-Type"]]
pump_speed = self.coordinator.data[self._item_id][self._state_key]
if pump_type == "VARIABLE":
self._unit = PERCENTAGE
state = pump_speed
elif pump_type == "DUAL":
if pump_speed == 0:
state = "off"
elif pump_speed == self.coordinator.data[self._item_id].get(
"Min-Pump-Speed"
):
state = "low"
elif pump_speed == self.coordinator.data[self._item_id].get(
"Max-Pump-Speed"
):
state = "high"
self._attrs["pump_type"] = pump_type
return state
class OmniLogicSaltLevelSensor(OmnilogicSensor):
"""Define an OmniLogic Salt Level Sensor."""
@property
def state(self):
"""Return the state for the salt level sensor."""
salt_return = self.coordinator.data[self._item_id][self._state_key]
unit_of_measurement = self._unit
if self._unit_type == "Metric":
salt_return = round(salt_return / 1000, 2)
unit_of_measurement = f"{MASS_GRAMS}/{VOLUME_LITERS}"
self._unit = unit_of_measurement
return salt_return
class OmniLogicChlorinatorSensor(OmnilogicSensor):
"""Define an OmniLogic Chlorinator Sensor."""
@property
def state(self):
"""Return the state for the chlorinator sensor."""
state = self.coordinator.data[self._item_id][self._state_key]
return state
class OmniLogicPHSensor(OmnilogicSensor):
"""Define an OmniLogic pH Sensor."""
@property
def state(self):
"""Return the state for the pH sensor."""
ph_state = self.coordinator.data[self._item_id][self._state_key]
if ph_state == 0:
ph_state = None
return ph_state
class OmniLogicORPSensor(OmnilogicSensor):
"""Define an OmniLogic ORP Sensor."""
def __init__(
self,
coordinator: OmniLogicUpdateCoordinator,
state_key: str,
name: str,
kind: str,
item_id: tuple,
device_class: str,
icon: str,
unit: str,
):
"""Initialize the sensor."""
super().__init__(
coordinator=coordinator,
kind=kind,
name=name,
device_class=device_class,
icon=icon,
unit=unit,
item_id=item_id,
state_key=state_key,
)
@property
def state(self):
"""Return the state for the ORP sensor."""
orp_state = self.coordinator.data[self._item_id][self._state_key]
if orp_state == -1:
orp_state = None
return orp_state
SENSOR_TYPES = {
(2, "Backyard"): [
{
"entity_classes": {"airTemp": OmniLogicTemperatureSensor},
"name": "Air Temperature",
"kind": "air_temperature",
"device_class": DEVICE_CLASS_TEMPERATURE,
"icon": None,
"unit": TEMP_FAHRENHEIT,
"guard_condition": [{}],
},
],
(4, "BOWS"): [
{
"entity_classes": {"waterTemp": OmniLogicTemperatureSensor},
"name": "Water Temperature",
"kind": "water_temperature",
"device_class": DEVICE_CLASS_TEMPERATURE,
"icon": None,
"unit": TEMP_FAHRENHEIT,
"guard_condition": [{}],
},
],
(6, "Filter"): [
{
"entity_classes": {"filterSpeed": OmniLogicPumpSpeedSensor},
"name": "Speed",
"kind": "filter_pump_speed",
"device_class": None,
"icon": "mdi:speedometer",
"unit": PERCENTAGE,
"guard_condition": [
{"Type": "FMT_SINGLE_SPEED"},
],
},
],
(6, "Pumps"): [
{
"entity_classes": {"pumpSpeed": OmniLogicPumpSpeedSensor},
"name": "Pump Speed",
"kind": "pump_speed",
"device_class": None,
"icon": "mdi:speedometer",
"unit": PERCENTAGE,
"guard_condition": [
{"Type": "PMP_SINGLE_SPEED"},
],
},
],
(6, "Chlorinator"): [
{
"entity_classes": {"Timed-Percent": OmniLogicChlorinatorSensor},
"name": "Setting",
"kind": "chlorinator",
"device_class": None,
"icon": "mdi:gauge",
"unit": PERCENTAGE,
"guard_condition": [
{
"Shared-Type": "BOW_SHARED_EQUIPMENT",
"status": "0",
},
{
"operatingMode": "2",
},
],
},
{
"entity_classes": {"avgSaltLevel": OmniLogicSaltLevelSensor},
"name": "Salt Level",
"kind": "salt_level",
"device_class": None,
"icon": "mdi:gauge",
"unit": CONCENTRATION_PARTS_PER_MILLION,
"guard_condition": [
{
"Shared-Type": "BOW_SHARED_EQUIPMENT",
"status": "0",
},
],
},
],
(6, "CSAD"): [
{
"entity_classes": {"ph": OmniLogicPHSensor},
"name": "pH",
"kind": "csad_ph",
"device_class": None,
"icon": "mdi:gauge",
"unit": "pH",
"guard_condition": [
{"ph": ""},
],
},
{
"entity_classes": {"orp": OmniLogicORPSensor},
"name": "ORP",
"kind": "csad_orp",
"device_class": None,
"icon": "mdi:gauge",
"unit": "mV",
"guard_condition": [
{"orp": ""},
],
},
],
}