Skip to content

Commit

Permalink
set integration title as {make_and_model} ({hostname})
Browse files Browse the repository at this point in the history
  • Loading branch information
elad-bar committed Jun 21, 2024
1 parent 4a94ad0 commit 1ebc046
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 142 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.0.4

- Fix update data when printer goes online [#161](https://github.com/elad-bar/ha-hpprinter/issues/161)
- Set integration title as `{make_and_model} ({hostname})`

## 2.0.3

Expand Down
4 changes: 4 additions & 0 deletions custom_components/hpprinter/common/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@

NUMERIC_UNITS_OF_MEASUREMENT = [UNIT_OF_MEASUREMENT_PAGES, UNIT_OF_MEASUREMENT_REFILLS]

MODEL_PROPERTY = "make_and_model"

PRODUCT_STATUS_ENDPOINT = "/DevMgmt/ProductStatusDyn.xml"
PRODUCT_MAIN_ENDPOINT = "/DevMgmt/ProductConfigDyn.xml"

PRODUCT_STATUS_OFFLINE_PAYLOAD = {
"ProductStatusDyn": {"Status": [{"StatusCategory": "off"}]}
}
Expand Down
45 changes: 28 additions & 17 deletions custom_components/hpprinter/managers/flow_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowHandler

from ..common.consts import DATA_KEYS, DEFAULT_NAME
from ..common.consts import (
DATA_KEYS,
DEFAULT_NAME,
MODEL_PROPERTY,
PRINTER_MAIN_DEVICE,
PRODUCT_MAIN_ENDPOINT,
)
from ..models.config_data import ConfigData
from ..models.exceptions import IntegrationAPIError, IntegrationParameterError
from .ha_config_manager import HAConfigManager
from .rest_api import RestAPIv2

Expand Down Expand Up @@ -58,29 +63,35 @@ async def async_step(self, user_input: dict | None = None):

api = RestAPIv2(self._hass, self._config_manager)

await api.initialize(True)
await api.initialize()

_LOGGER.debug("User inputs are valid")
title = DEFAULT_NAME
if api.is_online:
_LOGGER.debug("User inputs are valid")

if self._entry is None:
data = copy(user_input)
await api.update([PRODUCT_MAIN_ENDPOINT])

else:
data = await self.remap_entry_data(user_input)
title = self._entry.title
main_device = api.data.get(PRINTER_MAIN_DEVICE, {})
model = main_device.get(MODEL_PROPERTY, DEFAULT_NAME)
title = f"{model} ({api.config_data.hostname})"

return self._flow_handler.async_create_entry(title=title, data=data)
if self._entry is None:
data = copy(user_input)

except IntegrationParameterError as ipex:
form_errors = {"base": "error_400"}
else:
data = await self.remap_entry_data(user_input)
title = self._entry.title

return self._flow_handler.async_create_entry(title=title, data=data)

_LOGGER.warning(f"Failed to setup integration, Error: {ipex}")
else:
form_errors = {"base": "error_404"}

_LOGGER.warning("Failed to setup integration")

except IntegrationAPIError as iapiex:
form_errors = {"base": "error_404"}
except Exception as ex:
form_errors = {"base": "error_400"}

_LOGGER.warning(f"Failed to setup integration, Error: {iapiex}")
_LOGGER.error(f"Failed to setup integration, Error: {ex}")

schema = ConfigData.default_schema(user_input)

Expand Down
5 changes: 3 additions & 2 deletions custom_components/hpprinter/managers/ha_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from ..common.consts import (
DOMAIN,
MODEL_PROPERTY,
PRINTER_MAIN_DEVICE,
SIGNAL_HA_DEVICE_CREATED,
SIGNAL_HA_DEVICE_DISCOVERED,
Expand Down Expand Up @@ -115,7 +116,7 @@ def create_main_device(
self._main_device_data = device_data
self._main_device_id = device_key

model = device_data.get("make_and_model")
model = device_data.get(MODEL_PROPERTY)
serial_number = device_data.get("serial_number")
manufacturer = device_data.get("manufacturer_name")

Expand Down Expand Up @@ -147,7 +148,7 @@ def create_sub_unit_device(
self, device_key: str, device_data: dict, device_config: dict
):
try:
model = self._main_device_data.get("make_and_model")
model = self._main_device_data.get(MODEL_PROPERTY)
serial_number = self._main_device_data.get("serial_number")
manufacturer = self._main_device_data.get("manufacturer_name")

Expand Down
Loading

0 comments on commit 1ebc046

Please sign in to comment.