Home Information - Your home, your data, organized.
Python
Fix: Update entity icon when EntityType changes (#250)
Thiago Trautwein Santosd3e48714d6b2
diff --git a/src/hi/apps/entity/tests/test_views.py b/src/hi/apps/entity/tests/test_views.pyindex 0624c9eb..4e873d83 100644--- a/src/hi/apps/entity/tests/test_views.py+++ b/src/hi/apps/entity/tests/test_views.py@@ -119,6 +119,30 @@ class TestEntityEditView(DualModeViewTestCase):self.assertEqual(self.entity.name, 'Updated Entity Name')self.assertEqual(self.entity.entity_type_str, str(EntityType.WALL_SWITCH))+ def test_post_entity_type_change_returns_refresh_response(self):+ """Test EntityType changes return refresh response so location icons update."""+ url = reverse('entity_edit', kwargs={'entity_id': self.entity.id})++ form_data = EntityAttributeSyntheticData.create_form_data_for_entity_edit(+ entity=self.entity,+ entity_type_str=str(EntityType.CAMERA),+ )++ attributes = list(self.entity.attributes.all())+ formset_data = EntityAttributeSyntheticData.create_formset_data_for_attributes(+ attributes, self.entity+ )+ form_data.update(formset_data)++ response = self.client.post(url, form_data)++ self.assertSuccessResponse(response)+ self.assertJsonResponse(response)++ data = response.json()+ self.assertIn('refresh', data)+ self.assertTrue(data['refresh'])+def test_post_attribute_value_updates(self):"""Test updating attribute values through formset."""url = reverse('entity_edit', kwargs={'entity_id': self.entity.id})diff --git a/src/hi/apps/entity/views.py b/src/hi/apps/entity/views.pyindex 733835dc..36b83e48 100644--- a/src/hi/apps/entity/views.py+++ b/src/hi/apps/entity/views.py@@ -11,6 +11,7 @@ from hi.apps.sense.sensor_history_manager import SensorHistoryMixinfrom hi.views import page_not_found_responsefrom hi.hi_async_view import HiModalView+from hi.apps.entity.edit.entity_type_transition_handler import EntityTypeTransitionHandlerfrom .models import Entity, EntityAttributefrom .transient_models import EntityStateHistoryData@@ -86,12 +87,30 @@ class EntityEditView( HiModalView, EntityViewMixin, AttributeEditViewMixin ):def post( self, request,*args, **kwargs ):entity = self.get_entity(request, *args, **kwargs)+ original_entity_type = entity.entity_typeattr_item_context = EntityAttributeItemEditContext( entity = entity )- return self.post_attribute_form(+ response = self.post_attribute_form(request = request,attr_item_context = attr_item_context,)+ if response.status_code != 200:+ return response++ entity.refresh_from_db()+ entity_type_changed = bool( original_entity_type != entity.entity_type )+ if not entity_type_changed:+ return response++ transition_response = EntityTypeTransitionHandler().handle_entity_type_change(+ request = request,+ entity = entity,+ )+ if transition_response is None:+ return response++ return transition_response+class EntityAttributeUploadView( View, EntityViewMixin, AttributeEditViewMixin ):diff --git a/src/hi/static/js/attr.js b/src/hi/static/js/attr.jsindex 06a789a4..a36de7d7 100644--- a/src/hi/static/js/attr.js+++ b/src/hi/static/js/attr.js@@ -296,6 +296,19 @@handleResponse: function(response, $form, options = {}, isError = false) {// Parse response into normalized formatconst data = this.parseResponse(response);++ // Handle antinode-style redirects+ if (data.location) {+ window.location.href = data.location;+ return;+ }++ // Handle antinode-style refresh responses+ if (data.refresh) {+ window.location.reload();+ window.scrollTo(0, 0);+ return;+ }// Handle redirects first - if redirect is present, do it immediatelyif (data.redirect) {