diff --git a/homeassistant/components/tasmota/light.py b/homeassistant/components/tasmota/light.py index c761d3436df..1a41e7373fd 100644 --- a/homeassistant/components/tasmota/light.py +++ b/homeassistant/components/tasmota/light.py @@ -80,10 +80,11 @@ class TasmotaLight( self._setup_from_entity() - async def discovery_update(self, update): + async def discovery_update(self, update, write_state=True): """Handle updated discovery message.""" + await super().discovery_update(update, write_state=False) self._setup_from_entity() - await super().discovery_update(update) + self.async_write_ha_state() def _setup_from_entity(self): """(Re)Setup the entity.""" diff --git a/homeassistant/components/tasmota/mixins.py b/homeassistant/components/tasmota/mixins.py index 3b52fafd12b..5a1c7a9f3de 100644 --- a/homeassistant/components/tasmota/mixins.py +++ b/homeassistant/components/tasmota/mixins.py @@ -38,11 +38,12 @@ class TasmotaEntity(Entity): await self._tasmota_entity.unsubscribe_topics() await super().async_will_remove_from_hass() - async def discovery_update(self, update): + async def discovery_update(self, update, write_state=True): """Handle updated discovery message.""" self._tasmota_entity.config_update(update) await self._subscribe_topics() - self.async_write_ha_state() + if write_state: + self.async_write_ha_state() async def _subscribe_topics(self): """(Re)Subscribe to topics.""" diff --git a/tests/components/tasmota/test_common.py b/tests/components/tasmota/test_common.py index ccd87b2e434..5c18f2f401c 100644 --- a/tests/components/tasmota/test_common.py +++ b/tests/components/tasmota/test_common.py @@ -385,7 +385,7 @@ async def help_test_discovery_update_unchanged( entity_id="test", name="Test", ): - """Test update of discovered component without changes. + """Test update of discovered component with and without changes. This is a test helper for the MqttDiscoveryUpdate mixin. """ diff --git a/tests/components/tasmota/test_light.py b/tests/components/tasmota/test_light.py index afb583a5a2a..871d55a4d73 100644 --- a/tests/components/tasmota/test_light.py +++ b/tests/components/tasmota/test_light.py @@ -2,6 +2,8 @@ import copy import json +from hatasmota.const import CONF_MAC + from homeassistant.components import light from homeassistant.components.light import ( SUPPORT_BRIGHTNESS, @@ -771,6 +773,38 @@ async def test_unlinked_light2(hass, mqtt_mock, setup_tasmota): await _test_unlinked_light(hass, mqtt_mock, config, 2) +async def test_discovery_update_reconfigure_light( + hass, mqtt_mock, caplog, setup_tasmota +): + """Test reconfigure of discovered light.""" + config = copy.deepcopy(DEFAULT_CONFIG) + config["rl"][0] = 2 + config["lt_st"] = 1 # 1 channel light (Dimmer) + config2 = copy.deepcopy(DEFAULT_CONFIG) + config2["rl"][0] = 2 + config2["lt_st"] = 3 # 3 channel light (RGB) + data1 = json.dumps(config) + data2 = json.dumps(config2) + + # Simple dimmer + async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data1) + await hass.async_block_till_done() + state = hass.states.get("light.test") + assert ( + state.attributes.get("supported_features") + == SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION + ) + + # Reconfigure as RGB light + async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data2) + await hass.async_block_till_done() + state = hass.states.get("light.test") + assert ( + state.attributes.get("supported_features") + == SUPPORT_BRIGHTNESS | SUPPORT_COLOR | SUPPORT_EFFECT | SUPPORT_TRANSITION + ) + + async def test_availability_when_connection_lost( hass, mqtt_client_mock, mqtt_mock, setup_tasmota ):