Skip to content

Commit

Permalink
Adopt the device name for zone history entity if it is unnamed and th…
Browse files Browse the repository at this point in the history
…ere is only one zone
  • Loading branch information
sebr committed Apr 28, 2024
1 parent 7727c89 commit 304344a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
16 changes: 12 additions & 4 deletions custom_components/bhyve/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ async def async_setup_entry(
for device in devices:
if device.get("type") == DEVICE_SPRINKLER:
sensors.append(BHyveStateSensor(hass, bhyve, device))
for zone in device.get("zones"):
sensors.append(BHyveZoneHistorySensor(hass, bhyve, device, zone))
all_zones = device.get("zones")
for zone in all_zones:
# if the zone doesn't have a name, set it to the device's name if there is only one (eg a hose timer)
if zone_name is None:
zone_name = (
device.get("name") if len(all_zones) == 1 else "Unnamed Zone"
)
sensors.append(
BHyveZoneHistorySensor(hass, bhyve, device, zone, zone_name)
)

if device.get("battery", None) is not None:
sensors.append(BHyveBatterySensor(hass, bhyve, device))
Expand Down Expand Up @@ -187,13 +195,13 @@ def parse_battery_level(battery_data):
class BHyveZoneHistorySensor(BHyveDeviceEntity):
"""Define a BHyve sensor."""

def __init__(self, hass, bhyve, device, zone):
def __init__(self, hass, bhyve, device, zone, zone_name):
"""Initialize the sensor."""
self._history = None
self._zone = zone
self._zone_id = zone.get("station")

name = "{} zone history".format(zone.get("name", "Unknown"))
name = "{} zone history".format(zone_name)
_LOGGER.info("Creating history sensor: %s", name)

super().__init__(
Expand Down
7 changes: 6 additions & 1 deletion debug/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import os
import sys
import glob

if len(sys.argv) < 2:
print("Not enough arguments")
Expand All @@ -13,7 +14,11 @@

script_dir = os.path.dirname(os.path.realpath(__file__))

diagnostics_json = os.path.join(script_dir, FOLDER + "/diagnostics.json")
config_entry_files = glob.glob(os.path.join(script_dir, FOLDER, "config_entry*"))
if not config_entry_files:
print("No config_entry file found")
sys.exit()
diagnostics_json = config_entry_files[0]

with open(diagnostics_json, mode="r", encoding="utf8") as diagnosticsFile:
diagnostics = json.load(diagnosticsFile)
Expand Down

0 comments on commit 304344a

Please sign in to comment.