Add and use UNIT_CONDUCTIVITY constant (#34107)

pull/34113/head
springstan 2020-04-12 21:26:20 +02:00 committed by GitHub
parent 44afffcfbf
commit 538bb60022
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 8 deletions

View File

@ -18,6 +18,7 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_START,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UNIT_CONDUCTIVITY,
UNIT_PERCENTAGE,
)
from homeassistant.core import callback
@ -54,7 +55,7 @@ SENSOR_TYPES = {
"temperature": ["Temperature", TEMP_CELSIUS, "mdi:thermometer"],
"light": ["Light intensity", "lx", "mdi:white-balance-sunny"],
"moisture": ["Moisture", UNIT_PERCENTAGE, "mdi:water-percent"],
"conductivity": ["Conductivity", "µS/cm", "mdi:flash-circle"],
"conductivity": ["Conductivity", UNIT_CONDUCTIVITY, "mdi:flash-circle"],
"battery": ["Battery", UNIT_PERCENTAGE, "mdi:battery-charging"],
}

View File

@ -7,6 +7,7 @@ from homeassistant.const import (
POWER_WATT,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UNIT_CONDUCTIVITY,
UNIT_DEGREE,
UNIT_PERCENTAGE,
UNIT_VOLT,
@ -41,7 +42,7 @@ SENSORS = {
"V_CURRENT": ["A", "mdi:flash-auto"],
"V_PH": ["pH", None],
"V_ORP": ["mV", None],
"V_EC": ["μS/cm", None],
"V_EC": [UNIT_CONDUCTIVITY, None],
"V_VAR": ["var", None],
"V_VA": ["VA", None],
}

View File

@ -16,6 +16,7 @@ from homeassistant.const import (
STATE_UNAVAILABLE,
STATE_UNKNOWN,
TEMP_CELSIUS,
UNIT_CONDUCTIVITY,
UNIT_PERCENTAGE,
)
from homeassistant.core import callback
@ -147,7 +148,7 @@ class Plant(Entity):
"max": CONF_MAX_MOISTURE,
},
READING_CONDUCTIVITY: {
ATTR_UNIT_OF_MEASUREMENT: "µS/cm",
ATTR_UNIT_OF_MEASUREMENT: UNIT_CONDUCTIVITY,
"min": CONF_MIN_CONDUCTIVITY,
"max": CONF_MAX_CONDUCTIVITY,
},

View File

@ -409,6 +409,9 @@ MASS_MICROGRAMS = "µg"
MASS_OUNCES: str = "oz"
MASS_POUNDS: str = "lb"
# Conductivity units
UNIT_CONDUCTIVITY: str = f"µS/{LENGTH_CENTIMETERS}"
# UV Index units
UNIT_UV_INDEX: str = "UV index"

View File

@ -13,6 +13,7 @@ from homeassistant.const import (
STATE_PROBLEM,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
UNIT_CONDUCTIVITY,
)
from homeassistant.setup import setup_component
@ -93,7 +94,9 @@ class TestPlant(unittest.TestCase):
def test_initial_states(self):
"""Test plant initialises attributes if sensor already exists."""
self.hass.states.set(MOISTURE_ENTITY, 5, {ATTR_UNIT_OF_MEASUREMENT: "us/cm"})
self.hass.states.set(
MOISTURE_ENTITY, 5, {ATTR_UNIT_OF_MEASUREMENT: UNIT_CONDUCTIVITY}
)
plant_name = "some_plant"
assert setup_component(
self.hass, plant.DOMAIN, {plant.DOMAIN: {plant_name: GOOD_CONFIG}}
@ -111,7 +114,9 @@ class TestPlant(unittest.TestCase):
assert setup_component(
self.hass, plant.DOMAIN, {plant.DOMAIN: {plant_name: GOOD_CONFIG}}
)
self.hass.states.set(MOISTURE_ENTITY, 5, {ATTR_UNIT_OF_MEASUREMENT: "us/cm"})
self.hass.states.set(
MOISTURE_ENTITY, 5, {ATTR_UNIT_OF_MEASUREMENT: UNIT_CONDUCTIVITY}
)
self.hass.block_till_done()
state = self.hass.states.get(f"plant.{plant_name}")
assert STATE_PROBLEM == state.state
@ -127,7 +132,9 @@ class TestPlant(unittest.TestCase):
self.hass, plant.DOMAIN, {plant.DOMAIN: {plant_name: GOOD_CONFIG}}
)
self.hass.states.set(
MOISTURE_ENTITY, STATE_UNAVAILABLE, {ATTR_UNIT_OF_MEASUREMENT: "us/cm"}
MOISTURE_ENTITY,
STATE_UNAVAILABLE,
{ATTR_UNIT_OF_MEASUREMENT: UNIT_CONDUCTIVITY},
)
self.hass.block_till_done()
state = self.hass.states.get(f"plant.{plant_name}")
@ -143,13 +150,17 @@ class TestPlant(unittest.TestCase):
assert setup_component(
self.hass, plant.DOMAIN, {plant.DOMAIN: {plant_name: GOOD_CONFIG}}
)
self.hass.states.set(MOISTURE_ENTITY, 42, {ATTR_UNIT_OF_MEASUREMENT: "us/cm"})
self.hass.states.set(
MOISTURE_ENTITY, 42, {ATTR_UNIT_OF_MEASUREMENT: UNIT_CONDUCTIVITY}
)
self.hass.block_till_done()
state = self.hass.states.get(f"plant.{plant_name}")
assert state.state == STATE_OK
assert state.attributes[plant.READING_MOISTURE] == 42
self.hass.states.set(
MOISTURE_ENTITY, STATE_UNAVAILABLE, {ATTR_UNIT_OF_MEASUREMENT: "us/cm"}
MOISTURE_ENTITY,
STATE_UNAVAILABLE,
{ATTR_UNIT_OF_MEASUREMENT: UNIT_CONDUCTIVITY},
)
self.hass.block_till_done()
state = self.hass.states.get(f"plant.{plant_name}")