Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using productID instead of name #15

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2d9fddd
Update __init__.py
jsb2092 Sep 30, 2024
717519c
Update hub.py
jsb2092 Sep 30, 2024
7592d03
Update hub.py
jsb2092 Sep 30, 2024
87a0524
Update hub.py
jsb2092 Sep 30, 2024
8f5abcc
Update __init__.py
jsb2092 Sep 30, 2024
fb5da0d
Update __init__.py
jsb2092 Sep 30, 2024
916ad01
Update hub.py
jsb2092 Sep 30, 2024
a6a00bc
Update hub.py
jsb2092 Sep 30, 2024
a586cc4
Update feeder.py
jsb2092 Sep 30, 2024
af99b20
Revert "Update feeder.py"
jsb2092 Sep 30, 2024
fac8aec
attempting to add fountain support
jsb2092 Sep 30, 2024
6261df3
adding sensors for fountain
jsb2092 Sep 30, 2024
7c16b28
fixed sensor class
jsb2092 Sep 30, 2024
4a91f7d
adding weight percent
jsb2092 Sep 30, 2024
414f2d9
added labels for water
jsb2092 Sep 30, 2024
41075f6
fixing labels
jsb2092 Sep 30, 2024
ee0fa59
trying to get the water today to show up
jsb2092 Sep 30, 2024
53b2b22
trying a device class
jsb2092 Sep 30, 2024
7d52682
fixing units for volume
jsb2092 Sep 30, 2024
9899d19
fixing stuff
jsb2092 Sep 30, 2024
8155ed3
fixed unit converstion
jsb2092 Sep 30, 2024
36223f9
changed device types
jsb2092 Sep 30, 2024
bd5ce38
adding %, I think
jsb2092 Sep 30, 2024
2a1e33e
Trying to get the % label to show
jsb2092 Sep 30, 2024
482268c
trying to refresh water data correctly
jsb2092 Sep 30, 2024
6b7d51f
attempt to get fountain to refresh
jsb2092 Sep 30, 2024
ca8bebb
still trying to get it to regresh
jsb2092 Sep 30, 2024
f8fc154
reducing timeout for refresh
jsb2092 Sep 30, 2024
9b70158
reducing timeout for refresh
jsb2092 Sep 30, 2024
2533bbc
Revert "reducing timeout for refresh"
jsb2092 Oct 4, 2024
92bd500
Merge branch 'master' of https://github.com/jetcom/ha_petlibro
jsb2092 Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions custom_components/petlibro/devices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
from .device import Device
from .feeders.granary_feeder import GranaryFeeder
from .feeders.granary_camera_feeder import GranaryCameraFeeder
from .fountains.dockstream_fountain import DockstreamFountain

product_name_map : Dict[str, Type[Device]] = {
"Granary Feeder": GranaryFeeder,
"Granary Camera Feeder": GranaryCameraFeeder
}

product_id_map : Dict[str, Type[Device]] = {
"PLAF103": GranaryFeeder,
"PLAF203": GranaryCameraFeeder,
"PLWF105": DockstreamFountain,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import cast

from .fountain import Fountain


class DockstreamFountain(Fountain):
async def refresh(self):
await super().refresh()


@property
def remaining_cleaning(self) -> str:
return cast(str, self._data.get("remainingCleaningDays"))

@property
def remaining_replacement_days(self) -> str:
return cast(str, self._data.get("remainingReplacementDays"))

@property
def today_totalMl(self) -> int:
quantity = self._data.get("todayTotalMl")
if not quantity:
return 0

return self.convert_unit(quantity)

@property
def weight_percent(self) -> int:
return cast(int, self._data.get("weightPercent"))
40 changes: 39 additions & 1 deletion custom_components/petlibro/devices/fountains/fountain.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
from typing import Optional, cast
from . import Device

UNITS = {
1: "ml",
2: "oz"
}

UNITS_RATIO = {
1: 1,
2: 0.034
}

class Fountain(Device):
pass
async def refresh(self):
await super().refresh()


@property
def unit_id(self) -> int | None:
"""The device unit type identifier"""
return self._data.get("unitType")

@property
def unit_type(self) -> str | None:
"""The device unit type"""
unit : Optional[str] = None

if unit_id := self.unit_id:
unit = UNITS.get(unit_id)

return unit

def convert_unit(self, value: int) -> int:
"""
Convert a value to the device unit

:param unit: Value to convert
:return: Converted value or unchanged if no unit
"""
if self.unit_id:
return value * UNITS_RATIO.get(self.unit_id, 1)
return value
11 changes: 6 additions & 5 deletions custom_components/petlibro/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

from .const import DOMAIN
from .api import PetLibroAPIError
from .devices import Device, product_name_map
from .devices import Device, product_name_map, product_id_map

_LOGGER = getLogger(__name__)
UPDATE_INTERVAL_SECONDS = 60 * 5
UPDATE_INTERVAL_SECONDS = 60 * 2


class PetLibroHub:
Expand All @@ -37,6 +37,7 @@ def __init__(self, hass: HomeAssistant, data: Mapping[str, Any]) -> None:
name=DOMAIN,
update_method=self.refresh_devices,
update_interval=timedelta(seconds=UPDATE_INTERVAL_SECONDS),

)

async def get_device(self, serial: str) -> Optional[Device]:
Expand All @@ -52,12 +53,12 @@ async def load_devices(self):
if device := await self.get_device(device_data["deviceSn"]):
await device.refresh()
else:
if device_data["productName"] in product_name_map:
device = product_name_map[device_data["productName"]](device_data, self.api)
if device_data["productIdentifier"] in product_id_map:
device = product_id_map[device_data["productIdentifier"]](device_data, self.api)
await device.refresh() # Get all API data
self.devices.append(device)
else:
_LOGGER.error("Unsupported device found: %s", device_data["productName"])
_LOGGER.info("Unsupported device found: %s", device_data["productIdentifier"])

async def refresh_devices(self) -> bool:
"""Update all known devices states from the PETLIBRO API."""
Expand Down
38 changes: 38 additions & 0 deletions custom_components/petlibro/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from .devices import Device
from .devices.feeders.feeder import Feeder
from .devices.feeders.granary_feeder import GranaryFeeder
from .devices.fountains.fountain import Fountain
from .devices.fountains.dockstream_fountain import DockstreamFountain
from . import PetLibroHubConfigEntry
from .entity import PetLibroEntity, _DeviceT, PetLibroEntityDescription

Expand Down Expand Up @@ -46,6 +48,14 @@ def device_class_feeder(device: Feeder) -> SensorDeviceClass | None:
return SensorDeviceClass.WEIGHT
if device.unit_type in ["cup", UnitOfVolume.MILLILITERS]:
return SensorDeviceClass.VOLUME


def unit_of_measurement_fountain(device: Fountain) -> str | None:
return device.unit_type

def device_class_fountain(device: Fountain) -> SensorDeviceClass | None:
if device.unit_type in ["ml", UnitOfVolume.MILLILITERS]:
return SensorDeviceClass.VOLUME


@dataclass(frozen=True)
Expand Down Expand Up @@ -115,6 +125,34 @@ def device_class(self) -> SensorDeviceClass | None:
icon="mdi:history",
state_class=SensorStateClass.TOTAL_INCREASING
)
],
DockstreamFountain: [
PetLibroSensorEntityDescription[DockstreamFountain](
key="remaining_cleaning",
translation_key="remaining_cleaning",
icon="mdi:calendar-clock"
),
PetLibroSensorEntityDescription[DockstreamFountain](
key="remaining_replacement_days",
translation_key="remaining_replacement_days",
icon="mdi:calendar-clock"
),
PetLibroSensorEntityDescription[DockstreamFountain](
key="today_totalMl",
translation_key="today_totalMl",
icon="mdi:water",
native_unit_of_measurement_fn=unit_of_measurement_fountain,
device_class_fn=device_class_fountain,
device_class=SensorDeviceClass.VOLUME,
state_class=SensorStateClass.MEASUREMENT
),
PetLibroSensorEntityDescription[DockstreamFountain](
key="weight_percent",
translation_key="weight_percent",
icon="mdi:percent",
native_unit_of_measurement="%",
device_class=SensorDeviceClass.MOISTURE,
)
]
}

Expand Down
19 changes: 19 additions & 0 deletions custom_components/petlibro/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,26 @@
},
"today_feeding_times": {
"name": "Today's feeding times"
},
"today_water_quantity": {
"name": "Today's water quantity"
},
"today_water_times": {
"name": "Today's water times"
},
"remaining_cleaning": {
"name": "Cleaning remaining days"
},
"remaining_replacement_days": {
"name": "Filter remaining days"
},
"weight_percent": {
"name": "Water Remaining"
},
"today_totalMl": {
"name": "Today's total water"
}

},
"switch": {
"feeding_plan": {
Expand Down