Skip to content

Commit

Permalink
Merge pull request #204 from ufozone/hibernation
Browse files Browse the repository at this point in the history
Add connect_expiration sensor entity
  • Loading branch information
ufozone authored Oct 13, 2024
2 parents 9d9e108 + 47414f4 commit 00581d0
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 72 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,12 @@ _These entities are disabled by default. You have to activate it if you want to
| mapping_started | Mapping started |
| mapping_ended | Mapping ended |

* connect_expiration

Attributes:

```text
connect_expiration, infinity_state, infinity_expiration
infinity_state, infinity_expiration
```

### Switch
Expand Down
52 changes: 34 additions & 18 deletions custom_components/zcsmower/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""ZCS Lawn Mower Robot sensor platform."""
from __future__ import annotations

from datetime import (
date,
datetime,
)

from homeassistant.core import HomeAssistant
from homeassistant.const import (
ATTR_STATE,
Expand All @@ -16,6 +21,7 @@
Entity,
EntityCategory,
)
from homeassistant.helpers.typing import StateType
import homeassistant.util.dt as dt_util

from .const import (
Expand All @@ -33,6 +39,13 @@
translation_key="state",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key=ATTR_CONNECT_EXPIRATION,
icon="mdi:ev-station",
device_class=SensorDeviceClass.TIMESTAMP,
translation_key=ATTR_CONNECT_EXPIRATION,
entity_category=EntityCategory.DIAGNOSTIC,
),
)


Expand Down Expand Up @@ -82,30 +95,33 @@ def __init__(

def _update_extra_state_attributes(self) -> None:
"""Update extra attributes."""
# Data expiration date
if (_connect_expiration := self._get_attribute(ATTR_CONNECT_EXPIRATION)) is not None:
self._additional_extra_state_attributes.update({
ATTR_CONNECT_EXPIRATION: _connect_expiration,
})
# +Infinity state
self._additional_extra_state_attributes.update({
ATTR_INFINITY_STATE: (
self._get_attribute(ATTR_INFINITY_STATE) in ("active", "pending")
and self._get_attribute(ATTR_INFINITY_EXPIRATION) > dt_util.now()
),
})
# +Infinity expiration date
if (_infinity_expiration := self._get_attribute(ATTR_INFINITY_EXPIRATION)) is not None:
if self._entity_key == ATTR_CONNECT_EXPIRATION:
# +Infinity state
self._additional_extra_state_attributes.update({
ATTR_INFINITY_EXPIRATION: _infinity_expiration,
ATTR_INFINITY_STATE: (
self._get_attribute(ATTR_INFINITY_STATE) in ("active", "pending")
and self._get_attribute(ATTR_INFINITY_EXPIRATION) > dt_util.now()
),
})
# +Infinity expiration date
if (_infinity_expiration := self._get_attribute(ATTR_INFINITY_EXPIRATION)) is not None:
self._additional_extra_state_attributes.update({
ATTR_INFINITY_EXPIRATION: _infinity_expiration,
})

@property
def icon(self) -> str:
"""Return the icon of the entity."""
return self._get_attribute(ATTR_ICON)
if self._entity_key is None:
return self._get_attribute(ATTR_ICON)

# Fallback
return self.entity_description.icon

@property
def native_value(self) -> str:
def native_value(self) -> StateType | date | datetime:
"""Return the native value of the sensor."""
return self._get_attribute(ATTR_STATE)
if self._entity_key is None:
return self._get_attribute(ATTR_STATE)
elif self._entity_key == ATTR_CONNECT_EXPIRATION:
return self._get_attribute(ATTR_CONNECT_EXPIRATION)
31 changes: 18 additions & 13 deletions custom_components/zcsmower/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,24 @@
}
},
"sensor": {
"connect_expiration": {
"name": "Connect expiration date",
"state_attributes": {
"infinity_state": {
"name": "+Infinity State",
"state": {
"true": "[%key:common::state::enabled%]",
"false": "[%key:common::state::disabled%]"
}
},
"infinity_expiration": {
"name": "+Infinity expiration date"
},
"imei": {
"name": "IMEI"
}
}
},
"state": {
"name": "State",
"state": {
Expand All @@ -499,19 +517,6 @@
"mapping_ended": "Mapping ended"
},
"state_attributes": {
"connect_expiration": {
"name": "Connect expiration date"
},
"infinity_state": {
"name": "+Infinity State",
"state": {
"true": "[%key:common::state::enabled%]",
"false": "[%key:common::state::disabled%]"
}
},
"infinity_expiration": {
"name": "+Infinity expiration date"
},
"imei": {
"name": "IMEI"
}
Expand Down
31 changes: 18 additions & 13 deletions custom_components/zcsmower/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,24 @@
}
},
"sensor": {
"connect_expiration": {
"name": "Connect Ablaufdatum",
"state_attributes": {
"infinity_state": {
"name": "+Infinity Status",
"state": {
"true": "Aktiviert",
"false": "Deaktiviert"
}
},
"infinity_expiration": {
"name": "+Infinity Ablaufdatum"
},
"imei": {
"name": "IMEI"
}
}
},
"state": {
"name": "Status",
"state": {
Expand All @@ -496,19 +514,6 @@
"mapping_ended": "Kartierung abgeschlossen"
},
"state_attributes": {
"connect_expiration": {
"name": "Connect Ablaufdatum"
},
"infinity_state": {
"name": "+Infinity Status",
"state": {
"true": "Aktiviert",
"false": "Deaktiviert"
}
},
"infinity_expiration": {
"name": "+Infinity Ablaufdatum"
},
"imei": {
"name": "IMEI"
}
Expand Down
31 changes: 18 additions & 13 deletions custom_components/zcsmower/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,24 @@
}
},
"sensor": {
"connect_expiration": {
"name": "Connect expiration date",
"state_attributes": {
"infinity_state": {
"name": "+Infinity State",
"state": {
"true": "Enabled",
"false": "Disabled"
}
},
"infinity_expiration": {
"name": "+Infinity expiration date"
},
"imei": {
"name": "IMEI"
}
}
},
"state": {
"name": "State",
"state": {
Expand All @@ -499,19 +517,6 @@
"mapping_ended": "Mapping ended"
},
"state_attributes": {
"connect_expiration": {
"name": "Connect expiration date"
},
"infinity_state": {
"name": "+Infinity State",
"state": {
"true": "Enabled",
"false": "Disabled"
}
},
"infinity_expiration": {
"name": "+Infinity expiration date"
},
"imei": {
"name": "IMEI"
}
Expand Down
31 changes: 18 additions & 13 deletions custom_components/zcsmower/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,24 @@
}
},
"sensor": {
"connect_expiration": {
"name": "Connect data di scadenza",
"state_attributes": {
"infinity_state": {
"name": "+Infinity stato",
"state": {
"true": "Abilitato",
"false": "Disabili"
}
},
"infinity_expiration": {
"name": "+Infinity data di scadenza"
},
"imei": {
"name": "IMEI"
}
}
},
"state": {
"name": "State",
"state": {
Expand All @@ -496,19 +514,6 @@
"mapping_ended": "Mappatura completata"
},
"state_attributes": {
"connect_expiration": {
"name": "Connect data di scadenza"
},
"infinity_state": {
"name": "+Infinity stato",
"state": {
"true": "Abilitato",
"false": "Disabili"
}
},
"infinity_expiration": {
"name": "+Infinity data di scadenza"
},
"imei": {
"name": "IMEI"
}
Expand Down
4 changes: 3 additions & 1 deletion info.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,12 @@ _These entities are disabled by default. You have to activate it if you want to
| mapping_started | Mapping started |
| mapping_ended | Mapping ended |

* connect_expiration

Attributes:

```text
connect_expiration, infinity_state, infinity_expiration
infinity_state, infinity_expiration
```

### Switch
Expand Down

0 comments on commit 00581d0

Please sign in to comment.