Skip to content

Commit

Permalink
feat: Add pumping sensor and service
Browse files Browse the repository at this point in the history
  • Loading branch information
jcgoette committed Jun 5, 2022
1 parent 84f98b6 commit 5d18d1d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ This service adds a feeding entry for your child. Feeding start/end/child fields
| amount | yes | Specify amount of feeding as an integer
| notes | yes | Add notes text to entry

### SERVICE ADD_PUMPING

This service adds a pumping entry for your child.

| Service data attribute | Optional | Description |
|------|:----:|-------------|
| entity_id | no | entity_id for the child sensor
| amount | no | Specify amount of pumping as an integer
| time | yes | Specify pumping recording time (must be in the past, else now() will be used)
| notes | yes | Add notes text to entry

### SERVICE ADD_SLEEP

This service adds a sleep entry for your child. Sleep start/end/child fields can be linked to an active timer.
Expand Down
7 changes: 7 additions & 0 deletions custom_components/babybuddy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
ATTR_NOTE: Final = "note"
ATTR_NOTES: Final = "notes"
ATTR_PICTURE: Final = "picture"
ATTR_PUMPING: Final = "pumping"
ATTR_RESULTS: Final = "results"
ATTR_SLEEP: Final = "sleep"
ATTR_SLUG: Final = "slug"
Expand Down Expand Up @@ -126,6 +127,12 @@ class BabyBuddyEntityDescription(SensorEntityDescription, SwitchEntityDescriptio
key=ATTR_NOTES,
state_key=ATTR_TIME,
),
BabyBuddyEntityDescription(
icon="mdi:mother-nurse",
key=ATTR_PUMPING,
state_class=STATE_CLASS_MEASUREMENT,
state_key=ATTR_AMOUNT,
),
BabyBuddyEntityDescription(
icon="mdi:sleep",
key=ATTR_SLEEP,
Expand Down
38 changes: 38 additions & 0 deletions custom_components/babybuddy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
ATTR_NOTE,
ATTR_NOTES,
ATTR_PICTURE,
ATTR_PUMPING,
ATTR_SLUG,
ATTR_SOLID,
ATTR_TYPE,
Expand Down Expand Up @@ -130,6 +131,15 @@ def update_entities() -> None:
},
"async_add_note",
)
platform.async_register_entity_service(
"add_pumping",
{
vol.Required(ATTR_AMOUNT): cv.positive_int,
vol.Optional(ATTR_TIME): vol.Any(cv.datetime, cv.time),
vol.Optional(ATTR_NOTES): cv.string,
},
"async_add_pumping",
)
platform.async_register_entity_service(
"add_temperature",
{
Expand Down Expand Up @@ -316,6 +326,34 @@ async def async_add_note(
await self.coordinator.client.async_post(ATTR_NOTES, data, date_time_now)
await self.coordinator.async_request_refresh()

async def async_add_pumping(
self,
amount: float,
time: datetime | time | None = None,
notes: str | None = None,
) -> None:
"""Add a pumping entry."""
if not isinstance(self, BabyBuddyChildSensor):
_LOGGER.debug("Babybuddy child sensor should be selected. Ignoring.")
return
data = {
ATTR_CHILD: self.child[ATTR_ID],
ATTR_AMOUNT: amount,
}
if time:
try:
date_time = get_datetime_from_time(time)
data[ATTR_TIME] = date_time
except ValidationError as err:
_LOGGER.error(err)
return
if notes:
data[ATTR_NOTES] = notes

date_time_now = get_datetime_from_time(dt_util.now())
await self.coordinator.client.async_post(ATTR_PUMPING, data, date_time_now)
await self.coordinator.async_request_refresh()

async def async_add_temperature(
self,
temperature: float,
Expand Down
29 changes: 29 additions & 0 deletions custom_components/babybuddy/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,35 @@ add_note:
selector:
time:

add_pumping:
name: Add pumping
description: Adds a pumping entry
target:
entity:
integration: babybuddy
domain: sensor
device_class: babybuddy_child
fields:
amount:
name: Pumping amount
description: Pumping amount
selector:
number:
min: 1
max: 10
mode: box
time:
name: Pumping time
description: Pumping time
selector:
time:
notes:
name: Pumping notes
description: Pumping notes
selector:
text:
multiline: true

add_sleep:
name: Add sleep
description: Adds sleep entry
Expand Down

0 comments on commit 5d18d1d

Please sign in to comment.