Skip to content

Commit

Permalink
Update code to reflect internal changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jcwillox committed Jul 1, 2022
1 parent 2c81e0b commit fd3ae05
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 52 deletions.
24 changes: 3 additions & 21 deletions custom_components/auto_backup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):

auto_backup = AutoBackup(hass, options, handler)
hass.data[DOMAIN][DATA_AUTO_BACKUP] = auto_backup
hass.data[DOMAIN][UNSUB_LISTENER] = entry.add_update_listener(
auto_backup.update_listener
)
entry.async_on_unload(entry.add_update_listener(auto_backup.update_listener))

await auto_backup.load_snapshots_expiry()

Expand Down Expand Up @@ -204,32 +202,16 @@ async def async_service_handler(call: ServiceCallType):
for service, schema in MAP_SERVICES.items():
hass.services.async_register(DOMAIN, service, async_service_handler, schema)

# load the auto backup sensor.
for component in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, component)
)

hass.config_entries.async_setup_platforms(entry, PLATFORMS)
return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, component)
for component in PLATFORMS
]
)
)

hass.data[DOMAIN][UNSUB_LISTENER]()

for service in MAP_SERVICES.keys():
hass.services.async_remove(DOMAIN, service)

return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)


class AutoBackup:
Expand Down
4 changes: 4 additions & 0 deletions custom_components/auto_backup/const.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from homeassistant.const import Platform

DOMAIN = "auto_backup"
DATA_AUTO_BACKUP = "auto_backup"
UNSUB_LISTENER = "unsub_listener"
Expand All @@ -12,3 +14,5 @@
EVENT_BACKUP_START = f"{DOMAIN}.backup_start"
EVENT_BACKUP_FAILED = f"{DOMAIN}.backup_failed"
EVENT_BACKUPS_PURGED = f"{DOMAIN}.purged_backups"

PLATFORMS = [Platform.SENSOR]
63 changes: 32 additions & 31 deletions custom_components/auto_backup/sensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_NAME
from homeassistant.core import callback
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType, EventType

from . import AutoBackup
Expand All @@ -18,29 +21,35 @@
ATTR_PURGEABLE = "purgeable_backups"
ATTR_MONITORED = "monitored_backups"

DEFAULT_ICON = "mdi:package-variant-closed"
DEFAULT_NAME = "Auto Backup"


async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
):
"""Set up Auto Backup sensors based on a config entry."""
auto_backup = hass.data[DOMAIN][DATA_AUTO_BACKUP]
async_add_entities([AutoBackupSensor(auto_backup)])
async_add_entities([AutoBackupSensor(entry, auto_backup)])


class AutoBackupSensor(SensorEntity):
entity_description = SensorEntityDescription(
key="backups",
icon="mdi:package-variant-closed",
name="Auto Backup",
native_unit_of_measurement="pending backup(s)",
)

class AutoBackupSensor(Entity):
_attr_name = DEFAULT_NAME
_attr_unique_id = "sensor-auto-backup"
_attr_icon = DEFAULT_ICON
_attr_unit_of_measurement = "pending backup(s)"
_attr_extra_state_attributes = {}
_attr_state = None

def __init__(self, auto_backup: AutoBackup):
def __init__(self, entry: ConfigEntry, auto_backup: AutoBackup):
self._auto_backup = auto_backup
self._listeners = []
self._attr_device_info = DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
configuration_url="https://jcwillox.github.io/hass-auto-backup",
manufacturer="Auto Backup",
identifiers={(DOMAIN, entry.entry_id)},
name=entry.title,
)

async def async_added_to_hass(self):
"""Run when entity about to be added."""
Expand All @@ -52,33 +61,25 @@ def update(_):
self.async_schedule_update_ha_state(True)

@callback
def backup_failed(event: EventType):
def backup_failed(event_: EventType):
"""Store last failed and update sensor"""
self._attr_extra_state_attributes[ATTR_LAST_FAILURE] = event.data.get(
self._attr_extra_state_attributes[ATTR_LAST_FAILURE] = event_.data.get(
ATTR_NAME
)
self.async_schedule_update_ha_state(True)

self._listeners = [
self.hass.bus.async_listen(event, update)
for event in (
EVENT_BACKUP_START,
EVENT_BACKUP_SUCCESSFUL,
EVENT_BACKUPS_PURGED,
)
]
self._listeners.append(
for event in (
EVENT_BACKUP_START,
EVENT_BACKUP_SUCCESSFUL,
EVENT_BACKUPS_PURGED,
):
self.async_on_remove(self.hass.bus.async_listen(event, update))
self.async_on_remove(
self.hass.bus.async_listen(EVENT_BACKUP_FAILED, backup_failed)
)

async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""
await super().async_will_remove_from_hass()
for remove in self._listeners:
remove()

@property
def state(self):
def native_value(self):
"""Return the state of the entity."""
return self._auto_backup.state

Expand Down

0 comments on commit fd3ae05

Please sign in to comment.