Skip to content

Commit

Permalink
feat: make sure MQTT integration is loaded before setup
Browse files Browse the repository at this point in the history
  • Loading branch information
argoyle committed Dec 6, 2022
1 parent fd93bc4 commit 704919c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Ferroamp MQTT support sends updates to these topics:
* extapi/data/esm (interval 60s)

## Prerequisites
- Home assistant `2021.12`
- Home assistant `2022.12.x`
- Enable Ferroamp MQTT by contacting [Ferroamp Support](https://ferroamp.com/sv/kontakt/) to get the username and password for your Energy MQTT broker.
- Enable MQTT in Home assistant and set the broker to your Ferroamp Energy IP and configure it with your username and password received from Ferroamp (or setup a bridge-connection if you already have an MQTT-server, see the `Configuring Bridges`-section in the [Mosquitto documentation](https://mosquitto.org/man/mosquitto-conf-5.html)).

Expand Down
9 changes: 9 additions & 0 deletions custom_components/ferroamp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ async def async_unload_entry(hass, entry):


async def async_setup(hass: core.HomeAssistant, config: dict) -> bool:
# Make sure MQTT is available and the entry is loaded
if not hass.config_entries.async_entries(
mqtt.DOMAIN
) or not await hass.config_entries.async_wait_component(
hass.config_entries.async_entries(mqtt.DOMAIN)[0]
):
_LOGGER.error("MQTT integration is not available")
return False

_LOGGER.debug("Setting up ferroamp battery service calls")
hass.data.setdefault(DOMAIN, {})
device_registry = dr.async_get(hass)
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "Ferroamp Sensors",
"homeassistant": "2022.8.0b0"
"homeassistant": "2022.12.0b0"
}
2 changes: 1 addition & 1 deletion requirements.test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Strictly for tests
pytest-homeassistant-custom-component==0.10.2
pytest-homeassistant-custom-component==0.12.27
janus==1.0.0
# From our manifest.json for our custom component
aiohttp_cors==0.7.0
Expand Down
8 changes: 7 additions & 1 deletion tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.helpers import device_registry
from pytest_homeassistant_custom_component.common import async_fire_mqtt_message, MockConfigEntry

from custom_components.ferroamp import ATTR_POWER, ATTR_TARGET
from custom_components.ferroamp import ATTR_POWER, ATTR_TARGET, async_setup
from custom_components.ferroamp.const import DOMAIN, CONF_INTERVAL, DATA_DEVICES, DATA_PREFIXES, DATA_LISTENERS


Expand All @@ -32,6 +32,12 @@ def create_config(name="Ferroamp", prefix="extapi", unique_id="ferroamp"):
)


async def test_no_mqtt(hass, caplog):
result = await async_setup(hass, {})
assert not result
assert "MQTT integration is not available" in caplog.text


async def test_unload(hass, mqtt_mock):
config_entry = create_config()
config_entry.add_to_hass(hass)
Expand Down

0 comments on commit 704919c

Please sign in to comment.