Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plant limits attributes #42420

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions homeassistant/components/plant/__init__.py
Expand Up @@ -39,6 +39,7 @@

ATTR_PROBLEM = "problem"
ATTR_SENSORS = "sensors"
ATTR_LIMITS = "limits"
PROBLEM_NONE = "none"
ATTR_MAX_BRIGHTNESS_HISTORY = "max_brightness"

Expand Down Expand Up @@ -165,6 +166,7 @@ def __init__(self, name, config):
self._config = config
self._sensormap = {}
self._readingmap = {}
self._limitsmap = {}
self._unit_of_measurement = {}
for reading, entity_id in config["sensors"].items():
self._sensormap[entity_id] = reading
Expand All @@ -183,6 +185,11 @@ def __init__(self, name, config):
self._conf_check_days = self._config[CONF_CHECK_DAYS]
self._brightness_history = DailyHistory(self._conf_check_days)

for reading in self.READINGS.values():
for item in ["min", "max"]:
if item in reading and reading[item] in self._config:
self._limitsmap[reading[item]] = self._config[reading[item]]

@callback
def _state_changed_event(self, event):
"""Sensor state change event."""
Expand Down Expand Up @@ -357,6 +364,7 @@ def state_attributes(self):
attrib = {
ATTR_PROBLEM: self._problems,
ATTR_SENSORS: self._readingmap,
ATTR_LIMITS: self._limitsmap,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't allow storing config in the state attributes.

ATTR_DICT_OF_UNITS_OF_MEASUREMENT: self._unit_of_measurement,
}

Expand Down
20 changes: 12 additions & 8 deletions tests/components/plant/test_init.py
Expand Up @@ -30,6 +30,15 @@
BRIGHTNESS_ENTITY = "sensor.mqtt_plant_brightness"
MOISTURE_ENTITY = "sensor.mqtt_plant_moisture"

GOOD_LIMITS = {
"min_moisture": 20,
"max_moisture": 60,
"min_battery": 17,
"min_conductivity": 500,
"min_temperature": 15,
"min_brightness": 500,
}

GOOD_CONFIG = {
"sensors": {
"moisture": MOISTURE_ENTITY,
Expand All @@ -38,12 +47,7 @@
"conductivity": "sensor.mqtt_plant_conductivity",
"brightness": BRIGHTNESS_ENTITY,
},
"min_moisture": 20,
"max_moisture": 60,
"min_battery": 17,
"min_conductivity": 500,
"min_temperature": 15,
"min_brightness": 500,
**GOOD_LIMITS,
}


Expand All @@ -59,9 +63,9 @@ async def test_valid_data(hass):
)
assert sensor.state == "ok"
attrib = sensor.state_attributes
assert attrib["problem"] == "none"
assert attrib["limits"] == GOOD_LIMITS
for reading, value in GOOD_DATA.items():
# battery level has a different name in
# the JSON format than in hass
assert attrib[reading] == value


Expand Down