diff --git a/custom_components/duofern/binary_sensor.py b/custom_components/duofern/binary_sensor.py index eb9a2ce..1a1991f 100644 --- a/custom_components/duofern/binary_sensor.py +++ b/custom_components/duofern/binary_sensor.py @@ -47,6 +47,10 @@ async def async_setup_entry( continue async_add_entities([DuofernSmokeDetector(device['id'], device['name'], stick, hass)]) # Add new binary sensor for smoke detectors + if device['id'].startswith('a5'): # Check if this device is a sun sensor + if device['id'] in hass.data[DOMAIN]['devices'].keys(): # Check if Home Assistant already has this device + continue + async_add_entities([DuofernSunSensor(device['id'], device['name'], stick, hass)]) # Add new binary sensor for sun sensors class DuofernSmokeDetector(BinarySensorEntity): @@ -118,3 +122,68 @@ def update(self) -> None: self._battery_level = self._stick.duofern_parser.get_state(self._code, 'batteryLevel', channel=self._channel) except KeyError: self._battery_level = None + +class DuofernSunSensor(BinarySensorEntity): + """Duofern sun sensor entity""" + + def __init__(self, code, desc, stick, hass, channel=None): + """Initialize the sun sensor""" + + self._code = code + self._id = code + self._name = desc + + if channel: + chanNo = "{:02x}".format(channel) + self._id += chanNo + self._name += chanNo + + self._state: str | None = None # Holds the state (off = clear, on = smoke detected) + self._stick = stick # Hold an instance of the Duofern stick + self._channel = channel + hass.data[DOMAIN]['devices'][self._id] = self # Add device to our domain + + @property + def name(self): + """Returns the name of the sun sensor""" + return self._name + + @property + def is_on(self): + """Returns the current state of the sun sensor""" + return self._state == "on" + + @property + def device_state_attributes(self): + """Return the attributes of the sun sensor""" + attributes = { + } + + return attributes + @property + def icon(self): + """Return the icon of the sun sensor""" + return "mdi:sun-wireless-outline" + + @property + def device_class(self): + """Return the device class sun sensor""" + return BinarySensorDeviceClass.LIGHT + + @property + def should_poll(self): + """Whether this entity should be polled or uses subscriptions""" + return True # TODO: Add config option for subscriptions over polling + + @property + def unique_id(self): + """Return the unique id of the Duofern device""" + return self._id + + def update(self): + """Called right before is_on() to update the current state from the stick""" + try: + self._state = self._stick.duofern_parser.get_state(self._code, 'state', channel=self._channel) + except KeyError: + self._state = None + diff --git a/custom_components/duofern/manifest.json b/custom_components/duofern/manifest.json index f43d3df..fec7cab 100644 --- a/custom_components/duofern/manifest.json +++ b/custom_components/duofern/manifest.json @@ -7,5 +7,5 @@ "issue_tracker": "https://github.com/gluap/pyduofern-hacs/issues" , "codeowners": ["@gluap"], "requirements": ["pyduofern==0.35.1"], - "version": "0.5.0" + "version": "0.5.1" } diff --git a/hacs.json b/hacs.json index b63448b..c3ef2d2 100644 --- a/hacs.json +++ b/hacs.json @@ -1,5 +1,5 @@ { "name": "duofern", "domains": ["cover", "light", "binary_sensor"], - "homeassistant": "2023.3.2\" + "homeassistant": "2023.3.2" } \ No newline at end of file