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

View File

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

View File

@ -25,3 +25,12 @@ DEVICE_ID = "device_id"
UNDO_UPDATE_LISTENER = "undo_update_listener" UNDO_UPDATE_LISTENER = "undo_update_listener"
DEFAULT_SCAN_INTERVAL = 5 DEFAULT_SCAN_INTERVAL = 5
MIN_UPDATE_INTERVAL = timedelta(minutes=1) 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,
DEVICE_ID, DEVICE_ID,
DEVICE_NAME, DEVICE_NAME,
DEVICE_STATE_ATTRIBUTES,
DEVICE_TYPE, DEVICE_TYPE,
DOMAIN, DOMAIN,
SENSOR_DATA, SENSOR_DATA,
@ -69,8 +70,13 @@ class PlaatoEntity(entity.Entity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the monitored installation.""" """Return the state attributes of the monitored installation."""
if self._attributes is not None: if self._attributes:
return 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 @property
def available(self): def available(self):

View File

@ -59,11 +59,9 @@ async def async_setup_entry(hass, entry, async_add_entities):
coordinator = entry_data[COORDINATOR] coordinator = entry_data[COORDINATOR]
async_add_entities( async_add_entities(
PlaatoSensor(entry_data, sensor_type, coordinator) 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): class PlaatoSensor(PlaatoEntity):
"""Representation of a Plaato Sensor.""" """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["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "api_method" assert result["step_id"] == "api_method"
async def return_async_value(val):
return val
hass.config.components.add("cloud") hass.config.components.add("cloud")
with patch( with patch(
"homeassistant.components.cloud.async_active_subscription", return_value=True "homeassistant.components.cloud.async_active_subscription", return_value=True
), patch( ), patch(
"homeassistant.components.cloud.async_create_cloudhook", "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 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@ -243,21 +240,34 @@ async def test_options(hass):
data={}, data={},
options={CONF_SCAN_INTERVAL: 5}, options={CONF_SCAN_INTERVAL: 5},
) )
config_entry.add_to_hass(hass) 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) result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user" assert result["step_id"] == "user"
result = await hass.config_entries.options.async_configure( result = await hass.config_entries.options.async_configure(
result["flow_id"], result["flow_id"],
user_input={CONF_SCAN_INTERVAL: 10}, user_input={CONF_SCAN_INTERVAL: 10},
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY await hass.async_block_till_done()
assert result["data"][CONF_SCAN_INTERVAL] == 10
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): async def test_options_webhook(hass, webhook_id):
@ -268,19 +278,32 @@ async def test_options_webhook(hass, webhook_id):
data={CONF_USE_WEBHOOK: True, CONF_WEBHOOK_ID: None}, data={CONF_USE_WEBHOOK: True, CONF_WEBHOOK_ID: None},
options={CONF_SCAN_INTERVAL: 5}, options={CONF_SCAN_INTERVAL: 5},
) )
config_entry.add_to_hass(hass) 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) result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "webhook" assert result["step_id"] == "webhook"
assert result["description_placeholders"] == {"webhook_url": ""} assert result["description_placeholders"] == {"webhook_url": ""}
result = await hass.config_entries.options.async_configure( result = await hass.config_entries.options.async_configure(
result["flow_id"], result["flow_id"],
user_input={CONF_WEBHOOK_ID: WEBHOOK_ID}, user_input={CONF_WEBHOOK_ID: WEBHOOK_ID},
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY await hass.async_block_till_done()
assert result["data"][CONF_WEBHOOK_ID] == CONF_WEBHOOK_ID
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