Skip to content

Commit

Permalink
Merge pull request #3305 from ehowe/develop
Browse files Browse the repository at this point in the history
Make HomeAssistant description field optional
  • Loading branch information
vabene1111 committed Sep 23, 2024
2 parents 72c638c + eb0a95f commit 26ab7f5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
6 changes: 4 additions & 2 deletions cookbook/connectors/homeassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ async def on_shopping_list_entry_created(self, space: Space, shopping_list_entry
data = {
"entity_id": self._config.todo_entity,
"item": item,
"description": description,
}

if self._config.supports_description_field:
data["description"] = description

try:
await self.homeassistant_api_call("POST", "services/todo/add_item", data)
except ClientError as err:
self._logger.warning(f"received an exception from the api: {err=}, {type(err)=}")
self._logger.warning(f"received an exception from the api: {err=}, {type(err)=} {data=}")

async def on_shopping_list_entry_updated(self, space: Space, shopping_list_entry: ShoppingListEntry) -> None:
if not self._config.on_shopping_list_entry_updated_enabled:
Expand Down
8 changes: 7 additions & 1 deletion cookbook/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ class ConnectorConfigForm(forms.ModelForm):
required=False,
)

supports_description_field = forms.BooleanField(
help_text="Does the connector todo entity support the description field",
initial=True,
required=False,
)

update_token = forms.CharField(
widget=forms.TextInput(attrs={'autocomplete': 'update-token', 'type': 'password'}),
required=False,
Expand All @@ -198,7 +204,7 @@ class Meta:

fields = (
'name', 'type', 'enabled', 'on_shopping_list_entry_created_enabled', 'on_shopping_list_entry_updated_enabled',
'on_shopping_list_entry_deleted_enabled', 'url', 'todo_entity',
'on_shopping_list_entry_deleted_enabled', 'supports_description_field', 'url', 'todo_entity',
)

help_texts = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.15 on 2024-09-15 10:30

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('cookbook', '0218_alter_mealplan_from_date_alter_mealplan_to_date'),
]

operations = [
migrations.AddField(
model_name='connectorconfig',
name='supports_description_field',
field=models.BooleanField(default=True, help_text='Does the todo entity support the description field'),
),
]
1 change: 1 addition & 0 deletions cookbook/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ class ConnectorConfig(models.Model, PermissionModelMixin):
on_shopping_list_entry_created_enabled = models.BooleanField(default=False)
on_shopping_list_entry_updated_enabled = models.BooleanField(default=False)
on_shopping_list_entry_deleted_enabled = models.BooleanField(default=False)
supports_description_field = models.BooleanField(default=True, help_text="Does the todo entity support the description field")

url = models.URLField(blank=True, null=True)
token = models.CharField(max_length=512, blank=True, null=True)
Expand Down
2 changes: 1 addition & 1 deletion cookbook/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ class Meta:
fields = (
'id', 'name', 'url', 'token', 'todo_entity', 'enabled',
'on_shopping_list_entry_created_enabled', 'on_shopping_list_entry_updated_enabled',
'on_shopping_list_entry_deleted_enabled', 'created_by'
'on_shopping_list_entry_deleted_enabled', 'supports_description_field', 'created_by'
)

read_only_fields = ('created_by',)
Expand Down
12 changes: 12 additions & 0 deletions cookbook/tests/api/test_api_connector_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,23 @@ def test_add(arg, request, a1_s2, obj_1):
assert r.status_code == arg[1]
if r.status_code == 201:
assert response['name'] == 'test'
assert response['supports_description_field'] == True
r = c.get(reverse(DETAIL_URL, args={response['id']}))
assert r.status_code == 200
r = a1_s2.get(reverse(DETAIL_URL, args={response['id']}))
assert r.status_code == 404

def test_add_with_supports_description_field_false(a1_s2):
r = a1_s2.post(
reverse(LIST_URL),
{'name': 'test', 'url': 'http://localhost:8123/api', 'token': '1234', 'enabled': 'true', 'supports_description_field': 'false'},
content_type='application/json'
)
response = json.loads(r.content)
print(r.content)
assert r.status_code == 201
assert response['name'] == 'test'
assert response['supports_description_field'] == False

def test_delete(a1_s1, a1_s2, obj_1):
r = a1_s2.delete(
Expand Down

0 comments on commit 26ab7f5

Please sign in to comment.