Skip to content

Commit

Permalink
- rolled back the self-written translation for entity names
Browse files Browse the repository at this point in the history
- make use of the build-in translation features for integrations
  • Loading branch information
marq24 committed Oct 19, 2023
1 parent 513d5dd commit 7f56af1
Show file tree
Hide file tree
Showing 10 changed files with 2,316 additions and 727 deletions.
24 changes: 4 additions & 20 deletions custom_components/senec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
)

_LOGGER = logging.getLogger(__name__)
_LANG = None
SCAN_INTERVAL = timedelta(seconds=60)
CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA)

Expand All @@ -83,10 +82,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
DEFAULT_SCAN_INTERVAL_SENECV2)))

_LOGGER.info("Starting " + str(config_entry.data.get(CONF_NAME)) + " with interval: " + str(SCAN_INTERVAL))
load_translation(hass)
session = async_get_clientsession(hass)

coordinator = SenecDataUpdateCoordinator(hass, session, config_entry, langDict=_LANG)
coordinator = SenecDataUpdateCoordinator(hass, session, config_entry)
await coordinator.async_refresh()
if not coordinator.last_update_success:
raise ConfigEntryNotReady
Expand Down Expand Up @@ -115,7 +113,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
coordinator._device_type = SYSTYPE_NAME_WEBAPI
coordinator._device_model = f"{coordinator.senec.product_name} | SENEC.Num: {coordinator.senec.senec_num}"
coordinator._device_serial = coordinator.senec.serial_number
coordinator._device_version = None # senec_web_client.firmwareVersion
coordinator._device_version = None # senec_web_client.firmwareVersion

hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][config_entry.entry_id] = coordinator
Expand All @@ -127,23 +125,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
return True


def load_translation(hass):
"""Load correct language file or default to english"""
global _LANG # pylint: disable=global-statement
basepath = __file__[:-11]
file = f"{basepath}translations/senec.{hass.config.language.lower()}.json"
try:
with open(file) as f: # pylint: disable=unspecified-encoding,invalid-name
_LANG = json.load(f)
except: # pylint: disable=unspecified-encoding,bare-except,invalid-name
with open(f"{basepath}translations/senec.en.json") as f:
_LANG = json.load(f)


class SenecDataUpdateCoordinator(DataUpdateCoordinator):
"""Define an object to hold Senec data."""

def __init__(self, hass: HomeAssistant, session, config_entry, langDict=None):
def __init__(self, hass: HomeAssistant, session, config_entry):
"""Initialize."""
# Build-In INVERTER
if CONF_TYPE in config_entry.data and config_entry.data[CONF_TYPE] == CONF_SYSTYPE_INVERTER:
Expand Down Expand Up @@ -245,7 +230,6 @@ def __init__(self, hass: HomeAssistant, session, config_entry, langDict=None):
lang=hass.config.language.lower(), options=opt)

self.name = config_entry.title
self._langDict = langDict
self._config_entry = config_entry

self._device_type = None
Expand Down Expand Up @@ -323,7 +307,7 @@ def device_info(self) -> dict:
dmodel = self.coordinator._device_model
if dmodel is None:
dmodel = self.coordinator._config_entry.options.get(CONF_DEV_MODEL,
self.coordinator._config_entry.data.get(CONF_DEV_MODEL))
self.coordinator._config_entry.data.get(CONF_DEV_MODEL))

dserial = self.coordinator._device_serial
if dserial is None:
Expand Down
14 changes: 5 additions & 9 deletions custom_components/senec/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
ExtBinarySensorEntityDescription

_LOGGER = logging.getLogger(__name__)
_LANG = None


async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities):
Expand All @@ -23,9 +22,6 @@ async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry,
elif CONF_TYPE in config_entry.data and config_entry.data[CONF_TYPE] == CONF_SYSTYPE_WEB:
_LOGGER.info("No binary_sensors for WebPortal...")
else:
global _LANG
_LANG = coordinator._langDict

entities = []
for description in MAIN_BIN_SENSOR_TYPES:
entity = SenecBinarySensor(coordinator, description)
Expand All @@ -52,11 +48,11 @@ def __init__(
self._attr_icon = self.entity_description.icon
self._attr_icon_off = self.entity_description.icon_off
self.entity_id = f"binary_sensor.{slugify(title)}_{key}"
if key in _LANG:
self._attr_name = _LANG[key]
else:
_LOGGER.info(str(key)+" BinarySensor not found in translation")
self._attr_name = f"{title} {name}"

# we use the "key" also as our internal translation-key - and EXTREMELY important we have
# to set the '_attr_has_entity_name' to trigger the calls to the localization framework!
self._attr_translation_key = key
self._attr_has_entity_name = True

@property
def is_on(self) -> bool | None:
Expand Down
14 changes: 5 additions & 9 deletions custom_components/senec/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
)

_LOGGER = logging.getLogger(__name__)
_LANG = None


async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities):
"""Initialize sensor platform from config entry."""
coordinator = hass.data[DOMAIN][config_entry.entry_id]
global _LANG
_LANG = coordinator._langDict

entities = []
# Take care that CONF_TYPE = CONF_SYSTEYPE_WEB, since the implementation works with the web API
if CONF_TYPE in config_entry.data and config_entry.data[CONF_TYPE] == CONF_SYSTYPE_WEB:
Expand Down Expand Up @@ -51,11 +47,11 @@ def __init__(
key = self.entity_description.key.lower()
name = self.entity_description.name
self.entity_id = f"number.{slugify(title)}_{key}"
if key in _LANG:
self._attr_name = _LANG[key]
else:
_LOGGER.info(str(key)+" Number not found in translation")
self._attr_name = f"{title} {name}"

# we use the "key" also as our internal translation-key - and EXTREMELY important we have
# to set the '_attr_has_entity_name' to trigger the calls to the localization framework!
self._attr_translation_key = key
self._attr_has_entity_name = True

@property
def state(self) -> int:
Expand Down
15 changes: 5 additions & 10 deletions custom_components/senec/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@
)

_LOGGER = logging.getLogger(__name__)
_LANG = None


async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities):
"""Initialize sensor platform from config entry."""
coordinator = hass.data[DOMAIN][config_entry.entry_id]
global _LANG
_LANG = coordinator._langDict

entities = []
if CONF_TYPE in config_entry.data and config_entry.data[CONF_TYPE] == CONF_SYSTYPE_INVERTER:
for description in INVERTER_SENSOR_TYPES:
Expand Down Expand Up @@ -83,11 +78,11 @@ def __init__(
key = self.entity_description.key.lower()
name = self.entity_description.name
self.entity_id = f"sensor.{slugify(title)}_{key}"
if key in _LANG:
self._attr_name = _LANG[key]
else:
_LOGGER.info(str(key)+" Sensor not found in translation")
self._attr_name = f"{title} {name}"

# we use the "key" also as our internal translation-key - and EXTREMELY important we have
# to set the '_attr_has_entity_name' to trigger the calls to the localization framework!
self._attr_translation_key = key
self._attr_has_entity_name = True

@property
def state(self):
Expand Down
Loading

0 comments on commit 7f56af1

Please sign in to comment.