How is expected value in entropy derived?
Here is the definition of the expectation of a discrete random variable $Y$:
$$
\mathbb{E}[Y] = -\sum_y \Pr[Y = y] \cdot y.
$$
In your case, $Y = \log P(X)$, where $X \sim P$. Therefore
$$
\mathbb{E}[X] = \sum_y \Pr[\log P(X) = y] \cdot y.
$$
Notice that
$$
\Pr[-\log P(X) = y] = \sum_{x\colon \log P(x)=y} \Pr[X = x] \cdot y = \sum_{x\colon \log P(x)=y} \Pr[X = x] \cdot \log P(x).
$$
Therefore
$$
\mathbb{E}[X] = \sum_y \sum_{x\colon \log P(x) = y} \Pr[X = x] \cdot \log P(x) = \sum_x \Pr[X = x] \log P(x) = \sum_x P(x) \log P(x).
$$