Address Plaato post merge review (#46024)

This commit is contained in:
Johan Nenzén 2021-02-05 18:28:06 +01:00 committed by GitHub
parent ae2c7e4c74
commit 55f81a8a04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 38 deletions

View File

@ -20,7 +20,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Plaato from a config entry."""
if config_entry.data[CONF_USE_WEBHOOK]:
return False
return
coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
async_add_entities(
@ -29,11 +29,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
sensor_type,
coordinator,
)
for sensor_type in coordinator.data.binary_sensors.keys()
for sensor_type in coordinator.data.binary_sensors
)
return True
class PlaatoBinarySensor(PlaatoEntity, BinarySensorEntity):
"""Representation of a Binary Sensor."""

View File

@ -30,7 +30,7 @@ _LOGGER = logging.getLogger(__package__)
class PlaatoConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handles a Plaato config flow."""
VERSION = 2
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
def __init__(self):
@ -128,16 +128,16 @@ class PlaatoConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def _show_api_method_form(
self, device_type: PlaatoDeviceType, errors: dict = None
):
data_scheme = vol.Schema({vol.Optional(CONF_TOKEN, default=""): str})
data_schema = vol.Schema({vol.Optional(CONF_TOKEN, default=""): str})
if device_type == PlaatoDeviceType.Airlock:
data_scheme = data_scheme.extend(
data_schema = data_schema.extend(
{vol.Optional(CONF_USE_WEBHOOK, default=False): bool}
)
return self.async_show_form(
step_id="api_method",
data_schema=data_scheme,
data_schema=data_schema,
errors=errors,
description_placeholders={PLACEHOLDER_DEVICE_TYPE: device_type.name},
)

View File

@ -25,3 +25,12 @@ DEVICE_ID = "device_id"
UNDO_UPDATE_LISTENER = "undo_update_listener"
DEFAULT_SCAN_INTERVAL = 5
MIN_UPDATE_INTERVAL = timedelta(minutes=1)
DEVICE_STATE_ATTRIBUTES = {
"beer_name": "beer_name",
"keg_date": "keg_date",
"mode": "mode",
"original_gravity": "original_gravity",
"final_gravity": "final_gravity",
"alcohol_by_volume": "alcohol_by_volume",
}

View File

@ -7,6 +7,7 @@ from .const import (
DEVICE,
DEVICE_ID,
DEVICE_NAME,
DEVICE_STATE_ATTRIBUTES,
DEVICE_TYPE,
DOMAIN,
SENSOR_DATA,
@ -69,8 +70,13 @@ class PlaatoEntity(entity.Entity):
@property
def device_state_attributes(self):
"""Return the state attributes of the monitored installation."""
if self._attributes is not None:
return self._attributes
if self._attributes:
return {
attr_key: self._attributes[plaato_key]
for attr_key, plaato_key in DEVICE_STATE_ATTRIBUTES.items()
if plaato_key in self._attributes
and self._attributes[plaato_key] is not None
}
@property
def available(self):

View File

@ -59,11 +59,9 @@ async def async_setup_entry(hass, entry, async_add_entities):
coordinator = entry_data[COORDINATOR]
async_add_entities(
PlaatoSensor(entry_data, sensor_type, coordinator)
for sensor_type in coordinator.data.sensors.keys()
for sensor_type in coordinator.data.sensors
)
return True
class PlaatoSensor(PlaatoEntity):
"""Representation of a Plaato Sensor."""

View File

@ -95,15 +95,12 @@ async def test_show_config_form_validate_webhook(hass, webhook_id):
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "api_method"
async def return_async_value(val):
return val
hass.config.components.add("cloud")
with patch(
"homeassistant.components.cloud.async_active_subscription", return_value=True
), patch(
"homeassistant.components.cloud.async_create_cloudhook",
return_value=return_async_value("https://hooks.nabu.casa/ABCD"),
return_value="https://hooks.nabu.casa/ABCD",
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@ -243,9 +240,17 @@ async def test_options(hass):
data={},
options={CONF_SCAN_INTERVAL: 5},
)
config_entry.add_to_hass(hass)
with patch("homeassistant.components.plaato.async_setup_entry", return_value=True):
with patch(
"homeassistant.components.plaato.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.plaato.async_setup_entry", return_value=True
) as mock_setup_entry:
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
@ -256,9 +261,14 @@ async def test_options(hass):
user_input={CONF_SCAN_INTERVAL: 10},
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["data"][CONF_SCAN_INTERVAL] == 10
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
async def test_options_webhook(hass, webhook_id):
"""Test updating options."""
@ -268,9 +278,17 @@ async def test_options_webhook(hass, webhook_id):
data={CONF_USE_WEBHOOK: True, CONF_WEBHOOK_ID: None},
options={CONF_SCAN_INTERVAL: 5},
)
config_entry.add_to_hass(hass)
with patch("homeassistant.components.plaato.async_setup_entry", return_value=True):
with patch(
"homeassistant.components.plaato.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.plaato.async_setup_entry", return_value=True
) as mock_setup_entry:
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
@ -282,5 +300,10 @@ async def test_options_webhook(hass, webhook_id):
user_input={CONF_WEBHOOK_ID: WEBHOOK_ID},
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["data"][CONF_WEBHOOK_ID] == CONF_WEBHOOK_ID
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1