From 947b50b9378245c872e704dee95f1a6a87986760 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 7 Apr 2022 22:11:31 +0200 Subject: [PATCH] Added meta as a JSON field on the Entity table Metadata attributes can now be defined and overridden on the object itself, as well as on the database. Note that db settings will always take priority in case of value conflicts. --- platypush/entities/_base.py | 9 ++++++--- platypush/entities/devices.py | 4 ++-- platypush/entities/lights.py | 4 ++-- platypush/entities/switches.py | 4 ++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/platypush/entities/_base.py b/platypush/entities/_base.py index 3de25334c..aa589261e 100644 --- a/platypush/entities/_base.py +++ b/platypush/entities/_base.py @@ -35,6 +35,7 @@ class Entity(Base): type = Column(String, nullable=False, index=True) plugin = Column(String, nullable=False) data = Column(JSON, default=dict) + meta = Column(JSON, default=dict) created_at = Column( DateTime(timezone=False), default=datetime.utcnow(), nullable=False ) @@ -51,9 +52,8 @@ class Entity(Base): 'polymorphic_on': type, } - @classmethod @property - def meta(cls) -> Mapping: + def _meta(self) -> dict: return { 'icon_class': 'fa-solid fa-circle-question', } @@ -75,7 +75,10 @@ class Entity(Base): def to_json(self) -> dict: return { **{col.key: self._serialize_value(col) for col in self.columns}, - 'meta': self.meta, + 'meta': { + **self._meta, + **(self.meta or {}), + }, } def get_plugin(self): diff --git a/platypush/entities/devices.py b/platypush/entities/devices.py index f5d596cb9..bf26c2acb 100644 --- a/platypush/entities/devices.py +++ b/platypush/entities/devices.py @@ -12,9 +12,9 @@ class Device(Entity): 'polymorphic_identity': __tablename__, } - @classmethod @property - def meta(cls): + def _meta(self): return { + **super()._meta, 'icon_class': 'fa-solid fa-gear', } diff --git a/platypush/entities/lights.py b/platypush/entities/lights.py index a571e8698..4df4c51cb 100644 --- a/platypush/entities/lights.py +++ b/platypush/entities/lights.py @@ -12,9 +12,9 @@ class Light(Device): 'polymorphic_identity': __tablename__, } - @classmethod @property - def meta(cls): + def _meta(self): return { + **super()._meta, 'icon_class': 'fa-solid fa-lightbulb', } diff --git a/platypush/entities/switches.py b/platypush/entities/switches.py index 8f01350b6..f26ca6ef6 100644 --- a/platypush/entities/switches.py +++ b/platypush/entities/switches.py @@ -13,10 +13,10 @@ class Switch(Device): 'polymorphic_identity': __tablename__, } - @classmethod @property - def meta(cls): + def _meta(self): return { + **super()._meta, 'icon_class': 'fa-solid fa-light-switch', }