Skip to content

Commit

Permalink
Fix #47 #45 - disabled entities and offline printer issues
Browse files Browse the repository at this point in the history
  • Loading branch information
elad-bar committed Apr 26, 2020
1 parent b5e3bde commit e199937
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**Fixed bugs:**

- Fix disabled entities are getting enabled after periodic update (update interval)
- Fix offline printer is not updating entities correctly and after restart [\#45](https://github.com/elad-bar/ha-hpprinter/issues/45) [\#47](https://github.com/elad-bar/ha-hpprinter/issues/47)

## 2020-04-24 #6

Expand Down
23 changes: 16 additions & 7 deletions custom_components/hpprinter/managers/HPDeviceData.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from custom_components.hpprinter.api.HPPrinterAPI import *

from ..models.config_data import ConfigData
from .storage_manager import StorageManager

_LOGGER = logging.getLogger(__name__)

Expand All @@ -9,8 +10,11 @@ class HPDeviceData:
device_data: dict

def __init__(self, hass, config_manager: ConfigManager):
self._hass = hass
self._config_manager = config_manager

self._storage_manager = StorageManager(self._hass, self._config_manager)

self._usage_data_manager = ProductUsageDynPrinterDataAPI(
hass, self._config_manager
)
Expand All @@ -24,14 +28,11 @@ def __init__(self, hass, config_manager: ConfigManager):
hass, self._config_manager
)

self._hass = hass

self._usage_data = None
self._consumable_data = None
self._product_config_data = None
self._product_status_data = None

self.device_data = {HP_DEVICE_IS_ONLINE: False}
self.device_data = {}

@property
def config_data(self) -> ConfigData:
Expand All @@ -45,6 +46,15 @@ def name(self):
def host(self):
return self.config_data.host

async def initialize(self):
self.device_data = await self._storage_manager.async_load_from_store()

if self.device_data is None:
self.device_data = {}

self.device_data[PRINTER_CURRENT_STATUS] = PRINTER_STATUS[""]
self.device_data[HP_DEVICE_IS_ONLINE] = False

async def update(self):
try:
self.device_data["Name"] = self.config_data.name
Expand Down Expand Up @@ -76,9 +86,8 @@ async def update(self):

self.device_data[HP_DEVICE_IS_ONLINE] = is_online

json_data = json.dumps(self.device_data)

self._usage_data_manager.save_file("json", json_data, "final")
if is_online:
await self._storage_manager.async_save_to_store(self.device_data)

except Exception as ex:
exc_type, exc_obj, tb = sys.exc_info()
Expand Down
17 changes: 7 additions & 10 deletions custom_components/hpprinter/managers/entity_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,18 @@ def set_entity(self, domain, name, data: EntityData):

async def create_components(self):
cartridges_data = self.data.get(HP_DEVICE_CARTRIDGES)
is_online = self.is_online()

self.create_status_binary_sensor()
self.create_status_sensor()
self.create_printer_sensor()
self.create_scanner_sensor()

if is_online:
self.create_printer_sensor()
self.create_scanner_sensor()

if cartridges_data is not None:
for key in cartridges_data:
cartridge = cartridges_data.get(key)
if cartridges_data is not None:
for key in cartridges_data:
cartridge = cartridges_data.get(key)

if cartridge is not None:
self.create_cartridge_sensor(cartridge, key)
if cartridge is not None:
self.create_cartridge_sensor(cartridge, key)

def update(self):
self.hass.async_create_task(self._async_update())
Expand Down
2 changes: 2 additions & 0 deletions custom_components/hpprinter/managers/home_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ async def async_init(self, entry: ConfigEntry):
self._entity_manager = EntityManager(self._hass, self)
self._device_manager = DeviceManager(self._hass, self)

await self._data_manager.initialize()

def internal_async_init(now):
self._hass.async_create_task(self._async_init(now))

Expand Down
49 changes: 49 additions & 0 deletions custom_components/hpprinter/managers/storage_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Storage handers."""
import logging

from homeassistant.helpers.json import JSONEncoder
from homeassistant.helpers.storage import Store
from homeassistant.util import slugify

from ..helpers.const import *
from ..models.config_data import ConfigData
from .configuration_manager import ConfigManager

STORAGE_VERSION = 1

_LOGGER = logging.getLogger(__name__)


class StorageManager:
def __init__(self, hass, config_manager: ConfigManager):
self._hass = hass
self._config_manager = config_manager

@property
def config_data(self) -> ConfigData:
config_data = None

if self._config_manager is not None:
config_data = self._config_manager.data

return config_data

@property
def file_name(self):
file_name = f".{DOMAIN}.{slugify(self.config_data.name)}"

return file_name

async def async_load_from_store(self):
"""Load the retained data from store and return de-serialized data."""
store = Store(self._hass, STORAGE_VERSION, self.file_name, encoder=JSONEncoder)

data = await store.async_load()

return data

async def async_save_to_store(self, data):
"""Generate dynamic data to store and save it to the filesystem."""
store = Store(self._hass, STORAGE_VERSION, self.file_name, encoder=JSONEncoder)

await store.async_save(data)

0 comments on commit e199937

Please sign in to comment.