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.
This commit is contained in:
Fabio Manganiello 2022-04-07 22:11:31 +02:00
parent db7c2095ea
commit 947b50b937
Signed by: blacklight
GPG key ID: D90FBA7F76362774
4 changed files with 12 additions and 9 deletions

View file

@ -35,6 +35,7 @@ class Entity(Base):
type = Column(String, nullable=False, index=True) type = Column(String, nullable=False, index=True)
plugin = Column(String, nullable=False) plugin = Column(String, nullable=False)
data = Column(JSON, default=dict) data = Column(JSON, default=dict)
meta = Column(JSON, default=dict)
created_at = Column( created_at = Column(
DateTime(timezone=False), default=datetime.utcnow(), nullable=False DateTime(timezone=False), default=datetime.utcnow(), nullable=False
) )
@ -51,9 +52,8 @@ class Entity(Base):
'polymorphic_on': type, 'polymorphic_on': type,
} }
@classmethod
@property @property
def meta(cls) -> Mapping: def _meta(self) -> dict:
return { return {
'icon_class': 'fa-solid fa-circle-question', 'icon_class': 'fa-solid fa-circle-question',
} }
@ -75,7 +75,10 @@ class Entity(Base):
def to_json(self) -> dict: def to_json(self) -> dict:
return { return {
**{col.key: self._serialize_value(col) for col in self.columns}, **{col.key: self._serialize_value(col) for col in self.columns},
'meta': self.meta, 'meta': {
**self._meta,
**(self.meta or {}),
},
} }
def get_plugin(self): def get_plugin(self):

View file

@ -12,9 +12,9 @@ class Device(Entity):
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
@classmethod
@property @property
def meta(cls): def _meta(self):
return { return {
**super()._meta,
'icon_class': 'fa-solid fa-gear', 'icon_class': 'fa-solid fa-gear',
} }

View file

@ -12,9 +12,9 @@ class Light(Device):
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
@classmethod
@property @property
def meta(cls): def _meta(self):
return { return {
**super()._meta,
'icon_class': 'fa-solid fa-lightbulb', 'icon_class': 'fa-solid fa-lightbulb',
} }

View file

@ -13,10 +13,10 @@ class Switch(Device):
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
@classmethod
@property @property
def meta(cls): def _meta(self):
return { return {
**super()._meta,
'icon_class': 'fa-solid fa-light-switch', 'icon_class': 'fa-solid fa-light-switch',
} }