diff --git a/homeassistant/components/blue_current/__init__.py b/homeassistant/components/blue_current/__init__.py index 55362a7392d36a673afda1995c0da39e28815dc4..e852dfc8c6e684613527d8a3f1dd43fd8e5fc7f8 100644 --- a/homeassistant/components/blue_current/__init__.py +++ b/homeassistant/components/blue_current/__init__.py @@ -111,9 +111,9 @@ class Connector: entry[EVSE_ID], entry[MODEL_TYPE], entry[ATTR_NAME] ) for entry in charge_points_data - ) + ), + self.client.get_grid_status(charge_points_data[0][EVSE_ID]), ) - await self.client.get_grid_status(charge_points_data[0][EVSE_ID]) async def handle_charge_point(self, evse_id: str, model: str, name: str) -> None: """Add the chargepoint and request their data.""" @@ -127,22 +127,26 @@ class Connector: def update_charge_point(self, evse_id: str, data: dict) -> None: """Update the charge point data.""" self.charge_points[evse_id].update(data) - self.dispatch_value_update_signal(evse_id) + self.dispatch_charge_point_update_signal(evse_id) - def dispatch_value_update_signal(self, evse_id: str) -> None: - """Dispatch a value signal.""" - async_dispatcher_send(self.hass, f"{DOMAIN}_value_update_{evse_id}") + def dispatch_charge_point_update_signal(self, evse_id: str) -> None: + """Dispatch a charge point update signal.""" + async_dispatcher_send(self.hass, f"{DOMAIN}_charge_point_update_{evse_id}") def dispatch_grid_update_signal(self) -> None: - """Dispatch a grid signal.""" + """Dispatch a grid update signal.""" async_dispatcher_send(self.hass, f"{DOMAIN}_grid_update") + async def on_open(self) -> None: + """Fetch data when connection is established.""" + await self.client.get_charge_points() + async def run_task(self) -> None: """Start the receive loop.""" try: while True: try: - await self.client.connect(self.on_data) + await self.client.connect(self.on_data, self.on_open) except RequestLimitReached: LOGGER.warning( "Request limit reached. reconnecting at 00:00 (Europe/Amsterdam)" @@ -160,7 +164,7 @@ class Connector: def _on_disconnect(self) -> None: """Dispatch signals to update entity states.""" for evse_id in self.charge_points: - self.dispatch_value_update_signal(evse_id) + self.dispatch_charge_point_update_signal(evse_id) self.dispatch_grid_update_signal() async def _disconnect(self) -> None: diff --git a/homeassistant/components/blue_current/entity.py b/homeassistant/components/blue_current/entity.py index ecbbd8f0851bd0b7ef33ab99b2ef12d29e600042..cae7d420c996995ce8e0440d3848d36ce6f5c873 100644 --- a/homeassistant/components/blue_current/entity.py +++ b/homeassistant/components/blue_current/entity.py @@ -53,7 +53,7 @@ class ChargepointEntity(BlueCurrentEntity): def __init__(self, connector: Connector, evse_id: str) -> None: """Initialize the entity.""" - super().__init__(connector, f"{DOMAIN}_value_update_{evse_id}") + super().__init__(connector, f"{DOMAIN}_charge_point_update_{evse_id}") chargepoint_name = connector.charge_points[evse_id][ATTR_NAME] diff --git a/homeassistant/components/blue_current/manifest.json b/homeassistant/components/blue_current/manifest.json index fddd48554e25f0d198b935200dd521153d6bd962..4f277e836563ec7c229cb3d6810fde9549e95b00 100644 --- a/homeassistant/components/blue_current/manifest.json +++ b/homeassistant/components/blue_current/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/blue_current", "iot_class": "cloud_push", "loggers": ["bluecurrent_api"], - "requirements": ["bluecurrent-api==1.2.2"] + "requirements": ["bluecurrent-api==1.2.3"] } diff --git a/requirements_all.txt b/requirements_all.txt index a33da54e754a64d58d3d608a155bf3b398aaf382..109a1ede4db252d0d990ede24f71f40d2b5029d8 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -571,7 +571,7 @@ blinkpy==0.22.6 blockchain==1.4.4 # homeassistant.components.blue_current -bluecurrent-api==1.2.2 +bluecurrent-api==1.2.3 # homeassistant.components.bluemaestro bluemaestro-ble==0.2.3 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 1fe16e9b6e6385d70fb0b21e278769df38540123..d24d56ce1f7c99881d3c27fe08fa1d962f7c9b47 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -490,7 +490,7 @@ blebox-uniapi==2.2.2 blinkpy==0.22.6 # homeassistant.components.blue_current -bluecurrent-api==1.2.2 +bluecurrent-api==1.2.3 # homeassistant.components.bluemaestro bluemaestro-ble==0.2.3 diff --git a/tests/components/blue_current/__init__.py b/tests/components/blue_current/__init__.py index b5c15064449768f91422c74d24a54ac58220e075..97acff39a62771a1eaec0e85ff675ca65b3f8901 100644 --- a/tests/components/blue_current/__init__.py +++ b/tests/components/blue_current/__init__.py @@ -42,10 +42,10 @@ def create_client_mock( """Wait until chargepoints are received.""" await received_charge_points.wait() - async def connect(receiver): + async def connect(receiver, on_open): """Set the receiver and await future.""" client_mock.receiver = receiver - await client_mock.get_charge_points() + await on_open() started_loop.set() started_loop.clear() @@ -112,8 +112,9 @@ async def init_integration( hass, future_container, started_loop, charge_point, status, grid ) - with patch("homeassistant.components.blue_current.PLATFORMS", [platform]), patch( - "homeassistant.components.blue_current.Client", return_value=client_mock + with ( + patch("homeassistant.components.blue_current.PLATFORMS", [platform]), + patch("homeassistant.components.blue_current.Client", return_value=client_mock), ): config_entry.add_to_hass(hass) diff --git a/tests/components/blue_current/test_config_flow.py b/tests/components/blue_current/test_config_flow.py index 2e278af49826312e661011a7f45d5a2359391eaf..b5dad155618c8013fbc973c85e1ff690b94c8e58 100644 --- a/tests/components/blue_current/test_config_flow.py +++ b/tests/components/blue_current/test_config_flow.py @@ -36,15 +36,19 @@ async def test_user(hass: HomeAssistant) -> None: assert result["errors"] == {} assert result["type"] == FlowResultType.FORM - with patch( - "homeassistant.components.blue_current.config_flow.Client.validate_api_token", - return_value="1234", - ), patch( - "homeassistant.components.blue_current.config_flow.Client.get_email", - return_value="test@email.com", - ), patch( - "homeassistant.components.blue_current.async_setup_entry", - return_value=True, + with ( + patch( + "homeassistant.components.blue_current.config_flow.Client.validate_api_token", + return_value="1234", + ), + patch( + "homeassistant.components.blue_current.config_flow.Client.get_email", + return_value="test@email.com", + ), + patch( + "homeassistant.components.blue_current.async_setup_entry", + return_value=True, + ), ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -83,15 +87,19 @@ async def test_flow_fails(hass: HomeAssistant, error: Exception, message: str) - assert result["errors"]["base"] == message assert result["type"] == FlowResultType.FORM - with patch( - "homeassistant.components.blue_current.config_flow.Client.validate_api_token", - return_value="1234", - ), patch( - "homeassistant.components.blue_current.config_flow.Client.get_email", - return_value="test@email.com", - ), patch( - "homeassistant.components.blue_current.async_setup_entry", - return_value=True, + with ( + patch( + "homeassistant.components.blue_current.config_flow.Client.validate_api_token", + return_value="1234", + ), + patch( + "homeassistant.components.blue_current.config_flow.Client.get_email", + return_value="test@email.com", + ), + patch( + "homeassistant.components.blue_current.async_setup_entry", + return_value=True, + ), ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -121,17 +129,22 @@ async def test_reauth( expected_api_token: str, ) -> None: """Test reauth flow.""" - with patch( - "homeassistant.components.blue_current.config_flow.Client.validate_api_token", - return_value=customer_id, - ), patch( - "homeassistant.components.blue_current.config_flow.Client.get_email", - return_value="test@email.com", - ), patch( - "homeassistant.components.blue_current.config_flow.Client.wait_for_charge_points", - ), patch( - "homeassistant.components.blue_current.Client.connect", - lambda self, on_data: hass.loop.create_future(), + with ( + patch( + "homeassistant.components.blue_current.config_flow.Client.validate_api_token", + return_value=customer_id, + ), + patch( + "homeassistant.components.blue_current.config_flow.Client.get_email", + return_value="test@email.com", + ), + patch( + "homeassistant.components.blue_current.config_flow.Client.wait_for_charge_points", + ), + patch( + "homeassistant.components.blue_current.Client.connect", + lambda self, on_data, on_open: hass.loop.create_future(), + ), ): config_entry.add_to_hass(hass) result = await hass.config_entries.flow.async_init( diff --git a/tests/components/blue_current/test_init.py b/tests/components/blue_current/test_init.py index 4f570156c825f51fbabf9af6718882bb04c99485..06cc6b27c26b2c7e3fcc5ebc10b244e7a8c994da 100644 --- a/tests/components/blue_current/test_init.py +++ b/tests/components/blue_current/test_init.py @@ -29,13 +29,14 @@ async def test_load_unload_entry( hass: HomeAssistant, config_entry: MockConfigEntry ) -> None: """Test load and unload entry.""" - with patch( - "homeassistant.components.blue_current.Client.validate_api_token" - ), patch( - "homeassistant.components.blue_current.Client.wait_for_charge_points" - ), patch("homeassistant.components.blue_current.Client.disconnect"), patch( - "homeassistant.components.blue_current.Client.connect", - lambda self, on_data: hass.loop.create_future(), + with ( + patch("homeassistant.components.blue_current.Client.validate_api_token"), + patch("homeassistant.components.blue_current.Client.wait_for_charge_points"), + patch("homeassistant.components.blue_current.Client.disconnect"), + patch( + "homeassistant.components.blue_current.Client.connect", + lambda self, on_data, on_open: hass.loop.create_future(), + ), ): config_entry.add_to_hass(hass) await hass.config_entries.async_setup(config_entry.entry_id) @@ -61,10 +62,13 @@ async def test_config_exceptions( config_error: IntegrationError, ) -> None: """Test if the correct config error is raised when connecting to the api fails.""" - with patch( - "homeassistant.components.blue_current.Client.validate_api_token", - side_effect=api_error, - ), pytest.raises(config_error): + with ( + patch( + "homeassistant.components.blue_current.Client.validate_api_token", + side_effect=api_error, + ), + pytest.raises(config_error), + ): config_entry.add_to_hass(hass) await async_setup_entry(hass, config_entry)